Coverage Report

Created: 2026-01-09 07:38

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
14.3k
uint32_t Module::TakeNextIdBound() {
29
14.3k
  if (context()) {
30
14.3k
    if (id_bound() >= context()->max_id_bound()) {
31
0
      return 0;
32
0
    }
33
14.3k
  } else if (id_bound() >= kDefaultMaxIdBound) {
34
0
    return 0;
35
0
  }
36
37
14.3k
  return header_.bound++;
38
14.3k
}
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
298
std::vector<const Instruction*> Module::GetTypes() const {
49
298
  std::vector<const Instruction*> type_insts;
50
13.0k
  for (auto& inst : types_values_) {
51
13.0k
    if (IsTypeInst(inst.opcode())) type_insts.push_back(&inst);
52
13.0k
  }
53
298
  return type_insts;
54
298
}
55
56
228
std::vector<Instruction*> Module::GetConstants() {
57
228
  std::vector<Instruction*> const_insts;
58
9.14k
  for (auto& inst : types_values_) {
59
9.14k
    if (IsConstantInst(inst.opcode())) const_insts.push_back(&inst);
60
9.14k
  }
61
228
  return const_insts;
62
228
}
63
64
298
std::vector<const Instruction*> Module::GetConstants() const {
65
298
  std::vector<const Instruction*> const_insts;
66
13.0k
  for (auto& inst : types_values_) {
67
13.0k
    if (IsConstantInst(inst.opcode())) const_insts.push_back(&inst);
68
13.0k
  }
69
298
  return const_insts;
70
298
}
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
1.17k
                         bool run_on_debug_line_insts) {
88
14.0k
#define DELEGATE(list) list.ForEachInst(f, run_on_debug_line_insts)
89
1.17k
  DELEGATE(capabilities_);
90
1.17k
  DELEGATE(extensions_);
91
1.17k
  DELEGATE(ext_inst_imports_);
92
1.17k
  if (memory_model_) memory_model_->ForEachInst(f, run_on_debug_line_insts);
93
1.17k
  if (sampled_image_address_mode_)
94
0
    sampled_image_address_mode_->ForEachInst(f, run_on_debug_line_insts);
95
1.17k
  DELEGATE(entry_points_);
96
1.17k
  DELEGATE(graph_entry_points_);
97
1.17k
  DELEGATE(execution_modes_);
98
1.17k
  DELEGATE(debugs1_);
99
1.17k
  DELEGATE(debugs2_);
100
1.17k
  DELEGATE(debugs3_);
101
1.17k
  DELEGATE(ext_inst_debuginfo_);
102
1.17k
  DELEGATE(annotations_);
103
1.17k
  DELEGATE(types_values_);
104
2.09k
  for (auto& i : functions_) {
105
2.09k
    i->ForEachInst(f, run_on_debug_line_insts,
106
2.09k
                   /* run_on_non_semantic_insts = */ true);
107
2.09k
  }
108
1.17k
  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
1.17k
#undef DELEGATE
113
1.17k
}
114
115
void Module::ForEachInst(const std::function<void(const Instruction*)>& f,
116
296
                         bool run_on_debug_line_insts) const {
117
17.4k
#define DELEGATE(i) i.ForEachInst(f, run_on_debug_line_insts)
118
896
  for (auto& i : capabilities_) DELEGATE(i);
119
296
  for (auto& i : extensions_) DELEGATE(i);
120
304
  for (auto& i : ext_inst_imports_) DELEGATE(i);
121
296
  if (memory_model_)
122
296
    static_cast<const Instruction*>(memory_model_.get())
123
296
        ->ForEachInst(f, run_on_debug_line_insts);
124
296
  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
296
  for (auto& i : entry_points_) DELEGATE(i);
128
296
  for (auto& i : execution_modes_) DELEGATE(i);
129
296
  for (auto& i : debugs1_) DELEGATE(i);
130
296
  for (auto& i : debugs2_) DELEGATE(i);
131
296
  for (auto& i : debugs3_) DELEGATE(i);
132
3.39k
  for (auto& i : annotations_) DELEGATE(i);
133
8.50k
  for (auto& i : types_values_) DELEGATE(i);
134
296
  for (auto& i : ext_inst_debuginfo_) DELEGATE(i);
135
296
  for (auto& i : functions_) {
136
296
    static_cast<const Function*>(i.get())->ForEachInst(
137
296
        f, run_on_debug_line_insts,
138
296
        /* run_on_non_semantic_insts = */ true);
139
296
  }
140
296
  for (auto& i : graph_entry_points_) DELEGATE(i);
141
296
  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
296
  if (run_on_debug_line_insts) {
147
296
    for (auto& i : trailing_dbg_line_info_) DELEGATE(i);
148
296
  }
149
296
#undef DELEGATE
150
296
}
151
152
148
void Module::ToBinary(std::vector<uint32_t>* binary, bool skip_nop) const {
153
148
  binary->push_back(header_.magic_number);
154
148
  binary->push_back(header_.version);
155
  // TODO(antiagainst): should we change the generator number?
156
148
  binary->push_back(header_.generator);
157
148
  binary->push_back(header_.bound);
158
148
  binary->push_back(header_.schema);
159
160
148
  size_t bound_idx = binary->size() - 2;
161
148
  DebugScope last_scope(kNoDebugScope, kNoInlinedAt);
162
148
  const Instruction* last_line_inst = nullptr;
163
148
  bool between_merge_and_branch = false;
164
148
  bool between_label_and_phi_var = false;
165
148
  auto write_inst = [binary, skip_nop, &last_scope, &last_line_inst,
166
148
                     &between_merge_and_branch, &between_label_and_phi_var,
167
29.5k
                     this](const Instruction* i) {
168
    // Skip emitting line instructions between merge and branch instructions.
169
29.5k
    auto opcode = i->opcode();
170
29.5k
    if (between_merge_and_branch && i->IsLineInst()) {
171
0
      return;
172
0
    }
173
29.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 = context()
191
0
                                     ->get_feature_mgr()
192
0
                                     ->GetExtInstImportId_Shader100DebugInfo();
193
0
        if (shader_set_id != 0) {
194
0
          binary->push_back((5 << 16) |
195
0
                            static_cast<uint16_t>(spv::Op::OpExtInst));
196
0
          binary->push_back(context()->get_type_mgr()->GetVoidTypeId());
197
0
          binary->push_back(context()->TakeNextId());
198
0
          binary->push_back(shader_set_id);
199
0
          binary->push_back(NonSemanticShaderDebugInfo100DebugNoLine);
200
0
        } else {
201
0
          binary->push_back((1 << 16) |
202
0
                            static_cast<uint16_t>(spv::Op::OpNoLine));
203
0
        }
204
0
        last_line_inst = nullptr;
205
0
      }
206
0
    }
207
208
29.5k
    if (opcode == spv::Op::OpLabel) {
209
3.09k
      between_label_and_phi_var = true;
210
26.4k
    } else if (opcode != spv::Op::OpVariable && opcode != spv::Op::OpPhi &&
211
25.1k
               !spvtools::opt::IsOpLineInst(opcode)) {
212
25.1k
      between_label_and_phi_var = false;
213
25.1k
    }
214
215
29.5k
    if (!(skip_nop && i->IsNop())) {
216
29.5k
      const auto& scope = i->GetDebugScope();
217
29.5k
      if (scope != last_scope && !between_merge_and_branch) {
218
        // Can only emit nonsemantic instructions after all phi instructions
219
        // in a block so don't emit scope instructions before phi instructions
220
        // for NonSemantic.Shader.DebugInfo.100.
221
0
        if (!between_label_and_phi_var ||
222
0
            context()
223
0
                ->get_feature_mgr()
224
0
                ->GetExtInstImportId_OpenCL100DebugInfo()) {
225
          // Emit DebugScope |scope| to |binary|.
226
0
          auto dbg_inst = ext_inst_debuginfo_.begin();
227
0
          scope.ToBinary(dbg_inst->type_id(), context()->TakeNextId(),
228
0
                         dbg_inst->GetSingleWordOperand(2), binary);
229
0
        }
230
0
        last_scope = scope;
231
0
      }
232
233
29.5k
      i->ToBinaryWithoutAttachedDebugInsts(binary);
234
29.5k
    }
235
    // Update the last line instruction.
236
29.5k
    between_merge_and_branch = false;
237
29.5k
    if (spvOpcodeIsBlockTerminator(opcode) || i->IsNoLine()) {
238
3.09k
      last_line_inst = nullptr;
239
26.4k
    } else if (opcode == spv::Op::OpLoopMerge ||
240
26.2k
               opcode == spv::Op::OpSelectionMerge) {
241
1.22k
      between_merge_and_branch = true;
242
1.22k
      last_line_inst = nullptr;
243
25.1k
    } else if (i->IsLine()) {
244
0
      last_line_inst = i;
245
0
    }
246
29.5k
  };
247
148
  ForEachInst(write_inst, true);
248
249
  // We create new instructions for DebugScope and DebugNoLine. The bound must
250
  // be updated.
251
148
  binary->data()[bound_idx] = header_.bound;
252
148
}
253
254
148
uint32_t Module::ComputeIdBound() const {
255
148
  uint32_t highest = 0;
256
257
148
  ForEachInst(
258
29.5k
      [&highest](const Instruction* inst) {
259
89.4k
        for (const auto& operand : *inst) {
260
89.4k
          if (spvIsIdType(operand.type)) {
261
76.0k
            highest = std::max(highest, operand.words[0]);
262
76.0k
          }
263
89.4k
        }
264
29.5k
      },
265
148
      true /* scan debug line insts as well */);
266
267
148
  return highest + 1;
268
148
}
269
270
0
bool Module::HasExplicitCapability(uint32_t cap) {
271
0
  for (auto& ci : capabilities_) {
272
0
    uint32_t tcap = ci.GetSingleWordOperand(0);
273
0
    if (tcap == cap) {
274
0
      return true;
275
0
    }
276
0
  }
277
0
  return false;
278
0
}
279
280
444
uint32_t Module::GetExtInstImportId(const char* extstr) {
281
444
  for (auto& ei : ext_inst_imports_)
282
452
    if (!ei.GetInOperand(0).AsString().compare(extstr)) return ei.result_id();
283
296
  return 0;
284
444
}
285
286
0
std::ostream& operator<<(std::ostream& str, const Module& module) {
287
0
  module.ForEachInst([&str](const Instruction* inst) {
288
0
    str << *inst;
289
0
    if (inst->opcode() != spv::Op::OpFunctionEnd) {
290
0
      str << std::endl;
291
0
    }
292
0
  });
293
0
  return str;
294
0
}
295
296
}  // namespace opt
297
}  // namespace spvtools