Coverage Report

Created: 2025-11-23 07:07

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
84.0k
uint32_t Module::TakeNextIdBound() {
29
84.0k
  if (context()) {
30
84.0k
    if (id_bound() >= context()->max_id_bound()) {
31
0
      return 0;
32
0
    }
33
84.0k
  } else if (id_bound() >= kDefaultMaxIdBound) {
34
0
    return 0;
35
0
  }
36
37
84.0k
  return header_.bound++;
38
84.0k
}
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
1.82k
std::vector<const Instruction*> Module::GetTypes() const {
49
1.82k
  std::vector<const Instruction*> type_insts;
50
67.7k
  for (auto& inst : types_values_) {
51
67.7k
    if (IsTypeInst(inst.opcode())) type_insts.push_back(&inst);
52
67.7k
  }
53
1.82k
  return type_insts;
54
1.82k
}
55
56
1.55k
std::vector<Instruction*> Module::GetConstants() {
57
1.55k
  std::vector<Instruction*> const_insts;
58
54.7k
  for (auto& inst : types_values_) {
59
54.7k
    if (IsConstantInst(inst.opcode())) const_insts.push_back(&inst);
60
54.7k
  }
61
1.55k
  return const_insts;
62
1.55k
}
63
64
1.82k
std::vector<const Instruction*> Module::GetConstants() const {
65
1.82k
  std::vector<const Instruction*> const_insts;
66
67.7k
  for (auto& inst : types_values_) {
67
67.7k
    if (IsConstantInst(inst.opcode())) const_insts.push_back(&inst);
68
67.7k
  }
69
1.82k
  return const_insts;
70
1.82k
}
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
6.95k
                         bool run_on_debug_line_insts) {
88
83.4k
#define DELEGATE(list) list.ForEachInst(f, run_on_debug_line_insts)
89
6.95k
  DELEGATE(capabilities_);
90
6.95k
  DELEGATE(extensions_);
91
6.95k
  DELEGATE(ext_inst_imports_);
92
6.95k
  if (memory_model_) memory_model_->ForEachInst(f, run_on_debug_line_insts);
93
6.95k
  if (sampled_image_address_mode_)
94
0
    sampled_image_address_mode_->ForEachInst(f, run_on_debug_line_insts);
95
6.95k
  DELEGATE(entry_points_);
96
6.95k
  DELEGATE(graph_entry_points_);
97
6.95k
  DELEGATE(execution_modes_);
98
6.95k
  DELEGATE(debugs1_);
99
6.95k
  DELEGATE(debugs2_);
100
6.95k
  DELEGATE(debugs3_);
101
6.95k
  DELEGATE(ext_inst_debuginfo_);
102
6.95k
  DELEGATE(annotations_);
103
6.95k
  DELEGATE(types_values_);
104
11.5k
  for (auto& i : functions_) {
105
11.5k
    i->ForEachInst(f, run_on_debug_line_insts,
106
11.5k
                   /* run_on_non_semantic_insts = */ true);
107
11.5k
  }
108
6.95k
  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
6.95k
#undef DELEGATE
113
6.95k
}
114
115
void Module::ForEachInst(const std::function<void(const Instruction*)>& f,
116
2.02k
                         bool run_on_debug_line_insts) const {
117
98.9k
#define DELEGATE(i) i.ForEachInst(f, run_on_debug_line_insts)
118
3.65k
  for (auto& i : capabilities_) DELEGATE(i);
119
2.02k
  for (auto& i : extensions_) DELEGATE(i);
120
2.14k
  for (auto& i : ext_inst_imports_) DELEGATE(i);
121
2.02k
  if (memory_model_)
122
2.02k
    static_cast<const Instruction*>(memory_model_.get())
123
2.02k
        ->ForEachInst(f, run_on_debug_line_insts);
124
2.02k
  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
2.02k
  for (auto& i : entry_points_) DELEGATE(i);
128
2.02k
  for (auto& i : execution_modes_) DELEGATE(i);
129
2.02k
  for (auto& i : debugs1_) DELEGATE(i);
130
2.02k
  for (auto& i : debugs2_) DELEGATE(i);
131
2.02k
  for (auto& i : debugs3_) DELEGATE(i);
132
19.4k
  for (auto& i : annotations_) DELEGATE(i);
133
43.6k
  for (auto& i : types_values_) DELEGATE(i);
134
2.02k
  for (auto& i : ext_inst_debuginfo_) DELEGATE(i);
135
2.02k
  for (auto& i : functions_) {
136
2.02k
    static_cast<const Function*>(i.get())->ForEachInst(
137
2.02k
        f, run_on_debug_line_insts,
138
2.02k
        /* run_on_non_semantic_insts = */ true);
139
2.02k
  }
140
2.02k
  for (auto& i : graph_entry_points_) DELEGATE(i);
141
2.02k
  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
2.02k
  if (run_on_debug_line_insts) {
147
2.02k
    for (auto& i : trailing_dbg_line_info_) DELEGATE(i);
148
2.02k
  }
149
2.02k
#undef DELEGATE
150
2.02k
}
151
152
1.01k
void Module::ToBinary(std::vector<uint32_t>* binary, bool skip_nop) const {
153
1.01k
  binary->push_back(header_.magic_number);
154
1.01k
  binary->push_back(header_.version);
155
  // TODO(antiagainst): should we change the generator number?
156
1.01k
  binary->push_back(header_.generator);
157
1.01k
  binary->push_back(header_.bound);
158
1.01k
  binary->push_back(header_.schema);
159
160
1.01k
  size_t bound_idx = binary->size() - 2;
161
1.01k
  DebugScope last_scope(kNoDebugScope, kNoInlinedAt);
162
1.01k
  const Instruction* last_line_inst = nullptr;
163
1.01k
  bool between_merge_and_branch = false;
164
1.01k
  bool between_label_and_phi_var = false;
165
1.01k
  auto write_inst = [binary, skip_nop, &last_scope, &last_line_inst,
166
1.01k
                     &between_merge_and_branch, &between_label_and_phi_var,
167
125k
                     this](const Instruction* i) {
168
    // Skip emitting line instructions between merge and branch instructions.
169
125k
    auto opcode = i->opcode();
170
125k
    if (between_merge_and_branch && i->IsLineInst()) {
171
0
      return;
172
0
    }
173
125k
    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
125k
    if (opcode == spv::Op::OpLabel) {
209
11.5k
      between_label_and_phi_var = true;
210
114k
    } else if (opcode != spv::Op::OpVariable && opcode != spv::Op::OpPhi &&
211
106k
               !spvtools::opt::IsOpLineInst(opcode)) {
212
106k
      between_label_and_phi_var = false;
213
106k
    }
214
215
125k
    if (!(skip_nop && i->IsNop())) {
216
125k
      const auto& scope = i->GetDebugScope();
217
125k
      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
125k
      i->ToBinaryWithoutAttachedDebugInsts(binary);
234
125k
    }
235
    // Update the last line instruction.
236
125k
    between_merge_and_branch = false;
237
125k
    if (spvOpcodeIsBlockTerminator(opcode) || i->IsNoLine()) {
238
11.5k
      last_line_inst = nullptr;
239
114k
    } else if (opcode == spv::Op::OpLoopMerge ||
240
113k
               opcode == spv::Op::OpSelectionMerge) {
241
3.88k
      between_merge_and_branch = true;
242
3.88k
      last_line_inst = nullptr;
243
110k
    } else if (i->IsLine()) {
244
0
      last_line_inst = i;
245
0
    }
246
125k
  };
247
1.01k
  ForEachInst(write_inst, true);
248
249
  // We create new instructions for DebugScope and DebugNoLine. The bound must
250
  // be updated.
251
1.01k
  binary->data()[bound_idx] = header_.bound;
252
1.01k
}
253
254
1.00k
uint32_t Module::ComputeIdBound() const {
255
1.00k
  uint32_t highest = 0;
256
257
1.00k
  ForEachInst(
258
124k
      [&highest](const Instruction* inst) {
259
394k
        for (const auto& operand : *inst) {
260
394k
          if (spvIsIdType(operand.type)) {
261
321k
            highest = std::max(highest, operand.words[0]);
262
321k
          }
263
394k
        }
264
124k
      },
265
1.00k
      true /* scan debug line insts as well */);
266
267
1.00k
  return highest + 1;
268
1.00k
}
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
3.04k
uint32_t Module::GetExtInstImportId(const char* extstr) {
281
3.04k
  for (auto& ei : ext_inst_imports_)
282
3.16k
    if (!ei.GetInOperand(0).AsString().compare(extstr)) return ei.result_id();
283
2.03k
  return 0;
284
3.04k
}
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