Debian Codegraph @

third_party/spirv-tools/source/opt/ir_loader.cpp

raw
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/ir_loader.h"
16
17 #include <utility>
18
19 #include "DebugInfo.h"
20 #include "OpenCLDebugInfo100.h"
21 #include "source/ext_inst.h"
22 #include "source/opt/log.h"
23 #include "source/opt/reflect.h"
24 #include "source/util/make_unique.h"
25
26 static const uint32_t kExtInstSetIndex = 4;
27 static const uint32_t kLexicalScopeIndex = 5;
28 static const uint32_t kInlinedAtIndex = 6;
29
30 namespace spvtools {
31 namespace opt {
32
33 IrLoader::IrLoader(const MessageConsumer& consumer, Module* m)
34 : consumer_(consumer),
35 module_(m),
36 source_("<instruction>"),
37 inst_index_(0),
38 last_dbg_scope_(kNoDebugScope, kNoInlinedAt) {}
39
40 bool IrLoader::AddInstruction(const spv_parsed_instruction_t* inst) {
41 ++inst_index_;
42 const auto opcode = static_cast<SpvOp>(inst->opcode);
43 if (IsDebugLineInst(opcode)) {
44 last_line_inst_.reset();
45 dbg_line_info_.push_back(
46 Instruction(module()->context(), *inst, last_dbg_scope_));
47 return true;
48 }
49
50 // If it is a DebugScope or DebugNoScope of debug extension, we do not
51 // create a new instruction, but simply keep the information in
52 // struct DebugScope.
53 if (opcode == SpvOpExtInst && spvExtInstIsDebugInfo(inst->ext_inst_type)) {
54 const uint32_t ext_inst_index = inst->words[kExtInstSetIndex];
55 if (inst->ext_inst_type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100) {
56 const OpenCLDebugInfo100Instructions ext_inst_key =
57 OpenCLDebugInfo100Instructions(ext_inst_index);
58 if (ext_inst_key == OpenCLDebugInfo100DebugScope) {
59 uint32_t inlined_at = 0;
60 if (inst->num_words > kInlinedAtIndex)
61 inlined_at = inst->words[kInlinedAtIndex];
62 last_dbg_scope_ =
63 DebugScope(inst->words[kLexicalScopeIndex], inlined_at);
64 module()->SetContainsDebugScope();
65 return true;
66 }
67 if (ext_inst_key == OpenCLDebugInfo100DebugNoScope) {
68 last_dbg_scope_ = DebugScope(kNoDebugScope, kNoInlinedAt);
69 module()->SetContainsDebugScope();
70 return true;
71 }
72 } else {
73 const DebugInfoInstructions ext_inst_key =
74 DebugInfoInstructions(ext_inst_index);
75 if (ext_inst_key == DebugInfoDebugScope) {
76 uint32_t inlined_at = 0;
77 if (inst->num_words > kInlinedAtIndex)
78 inlined_at = inst->words[kInlinedAtIndex];
79 last_dbg_scope_ =
80 DebugScope(inst->words[kLexicalScopeIndex], inlined_at);
81 module()->SetContainsDebugScope();
82 return true;
83 }
84 if (ext_inst_key == DebugInfoDebugNoScope) {
85 last_dbg_scope_ = DebugScope(kNoDebugScope, kNoInlinedAt);
86 module()->SetContainsDebugScope();
87 return true;
88 }
89 }
90 }
91
92 std::unique_ptr<Instruction> spv_inst(
93 new Instruction(module()->context(), *inst, std::move(dbg_line_info_)));
94 if (!spv_inst->dbg_line_insts().empty()) {
95 if (spv_inst->dbg_line_insts().back().opcode() != SpvOpNoLine) {
96 last_line_inst_ = std::unique_ptr<Instruction>(
97 spv_inst->dbg_line_insts().back().Clone(module()->context()));
98 }
99 dbg_line_info_.clear();
100 } else if (last_line_inst_ != nullptr) {
101 last_line_inst_->SetDebugScope(last_dbg_scope_);
102 spv_inst->dbg_line_insts().push_back(*last_line_inst_);
103 }
104
105 const char* src = source_.c_str();
106 spv_position_t loc = {inst_index_, 0, 0};
107
108 // Handle function and basic block boundaries first, then normal
109 // instructions.
110 if (opcode == SpvOpFunction) {
111 if (function_ != nullptr) {
112 Error(consumer_, src, loc, "function inside function");
113 return false;
114 }
115 function_ = MakeUnique<Function>(std::move(spv_inst));
116 } else if (opcode == SpvOpFunctionEnd) {
117 if (function_ == nullptr) {
118 Error(consumer_, src, loc,
119 "OpFunctionEnd without corresponding OpFunction");
120 return false;
121 }
122 if (block_ != nullptr) {
123 Error(consumer_, src, loc, "OpFunctionEnd inside basic block");
124 return false;
125 }
126 function_->SetFunctionEnd(std::move(spv_inst));
127 module_->AddFunction(std::move(function_));
128 function_ = nullptr;
129 } else if (opcode == SpvOpLabel) {
130 if (function_ == nullptr) {
131 Error(consumer_, src, loc, "OpLabel outside function");
132 return false;
133 }
134 if (block_ != nullptr) {
135 Error(consumer_, src, loc, "OpLabel inside basic block");
136 return false;
137 }
138 block_ = MakeUnique<BasicBlock>(std::move(spv_inst));
139 } else if (IsTerminatorInst(opcode)) {
140 if (function_ == nullptr) {
141 Error(consumer_, src, loc, "terminator instruction outside function");
142 return false;
143 }
144 if (block_ == nullptr) {
145 Error(consumer_, src, loc, "terminator instruction outside basic block");
146 return false;
147 }
148 if (last_dbg_scope_.GetLexicalScope() != kNoDebugScope)
149 spv_inst->SetDebugScope(last_dbg_scope_);
150 block_->AddInstruction(std::move(spv_inst));
151 function_->AddBasicBlock(std::move(block_));
152 block_ = nullptr;
153 last_dbg_scope_ = DebugScope(kNoDebugScope, kNoInlinedAt);
154 last_line_inst_.reset();
155 dbg_line_info_.clear();
156 } else {
157 if (function_ == nullptr) { // Outside function definition
158 SPIRV_ASSERT(consumer_, block_ == nullptr);
159 if (opcode == SpvOpCapability) {
160 module_->AddCapability(std::move(spv_inst));
161 } else if (opcode == SpvOpExtension) {
162 module_->AddExtension(std::move(spv_inst));
163 } else if (opcode == SpvOpExtInstImport) {
164 module_->AddExtInstImport(std::move(spv_inst));
165 } else if (opcode == SpvOpMemoryModel) {
166 module_->SetMemoryModel(std::move(spv_inst));
167 } else if (opcode == SpvOpEntryPoint) {
168 module_->AddEntryPoint(std::move(spv_inst));
169 } else if (opcode == SpvOpExecutionMode) {
170 module_->AddExecutionMode(std::move(spv_inst));
171 } else if (IsDebug1Inst(opcode)) {
172 module_->AddDebug1Inst(std::move(spv_inst));
173 } else if (IsDebug2Inst(opcode)) {
174 module_->AddDebug2Inst(std::move(spv_inst));
175 } else if (IsDebug3Inst(opcode)) {
176 module_->AddDebug3Inst(std::move(spv_inst));
177 } else if (IsAnnotationInst(opcode)) {
178 module_->AddAnnotationInst(std::move(spv_inst));
179 } else if (IsTypeInst(opcode)) {
180 module_->AddType(std::move(spv_inst));
181 } else if (IsConstantInst(opcode) || opcode == SpvOpVariable ||
182 opcode == SpvOpUndef) {
183 module_->AddGlobalValue(std::move(spv_inst));
184 } else if (opcode == SpvOpExtInst &&
185 spvExtInstIsDebugInfo(inst->ext_inst_type)) {
186 module_->AddExtInstDebugInfo(std::move(spv_inst));
187 } else if (opcode == SpvOpExtInst &&
188 spvExtInstIsNonSemantic(inst->ext_inst_type)) {
189 // If there are no functions, add the non-semantic instructions to the
190 // global values. Otherwise append it to the list of the last function.
191 auto func_begin = module_->begin();
192 auto func_end = module_->end();
193 if (func_begin == func_end) {
194 module_->AddGlobalValue(std::move(spv_inst));
195 } else {
196 (--func_end)->AddNonSemanticInstruction(std::move(spv_inst));
197 }
198 } else {
199 Errorf(consumer_, src, loc,
200 "Unhandled inst type (opcode: %d) found outside function "
201 "definition.",
202 opcode);
203 return false;
204 }
205 } else {
206 if (opcode == SpvOpLoopMerge || opcode == SpvOpSelectionMerge)
207 last_dbg_scope_ = DebugScope(kNoDebugScope, kNoInlinedAt);
208 if (last_dbg_scope_.GetLexicalScope() != kNoDebugScope)
209 spv_inst->SetDebugScope(last_dbg_scope_);
210 if (opcode == SpvOpExtInst &&
211 spvExtInstIsDebugInfo(inst->ext_inst_type)) {
212 const uint32_t ext_inst_index = inst->words[kExtInstSetIndex];
213 if (inst->ext_inst_type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100) {
214 const OpenCLDebugInfo100Instructions ext_inst_key =
215 OpenCLDebugInfo100Instructions(ext_inst_index);
216 switch (ext_inst_key) {
217 case OpenCLDebugInfo100DebugDeclare: {
218 if (block_ == nullptr) // Inside function but outside blocks
219 function_->AddDebugInstructionInHeader(std::move(spv_inst));
220 else
221 block_->AddInstruction(std::move(spv_inst));
222 break;
223 }
224 case OpenCLDebugInfo100DebugValue: {
225 if (block_ == nullptr) // Inside function but outside blocks
226 function_->AddDebugInstructionInHeader(std::move(spv_inst));
227 else
228 block_->AddInstruction(std::move(spv_inst));
229 break;
230 }
231 default: {
232 Errorf(consumer_, src, loc,
233 "Debug info extension instruction other than DebugScope, "
234 "DebugNoScope, DebugDeclare, and DebugValue found inside "
235 "function",
236 opcode);
237 return false;
238 }
239 }
240 } else {
241 const DebugInfoInstructions ext_inst_key =
242 DebugInfoInstructions(ext_inst_index);
243 switch (ext_inst_key) {
244 case DebugInfoDebugDeclare: {
245 if (block_ == nullptr) // Inside function but outside blocks
246 function_->AddDebugInstructionInHeader(std::move(spv_inst));
247 else
248 block_->AddInstruction(std::move(spv_inst));
249 break;
250 }
251 case DebugInfoDebugValue: {
252 if (block_ == nullptr) // Inside function but outside blocks
253 function_->AddDebugInstructionInHeader(std::move(spv_inst));
254 else
255 block_->AddInstruction(std::move(spv_inst));
256 break;
257 }
258 default: {
259 Errorf(consumer_, src, loc,
260 "Debug info extension instruction other than DebugScope, "
261 "DebugNoScope, DebugDeclare, and DebugValue found inside "
262 "function",
263 opcode);
264 return false;
265 }
266 }
267 }
268 } else {
269 if (block_ == nullptr) { // Inside function but outside blocks
270 if (opcode != SpvOpFunctionParameter) {
271 Errorf(consumer_, src, loc,
272 "Non-OpFunctionParameter (opcode: %d) found inside "
273 "function but outside basic block",
274 opcode);
275 return false;
276 }
277 function_->AddParameter(std::move(spv_inst));
278 } else {
279 block_->AddInstruction(std::move(spv_inst));
280 }
281 }
282 }
283 }
284 return true;
285 }
286
287 // Resolves internal references among the module, functions, basic blocks, etc.
288 // This function should be called after adding all instructions.
289 void IrLoader::EndModule() {
290 if (block_ && function_) {
291 // We're in the middle of a basic block, but the terminator is missing.
292 // Register the block anyway. This lets us write tests with less
293 // boilerplate.
294 function_->AddBasicBlock(std::move(block_));
295 block_ = nullptr;
296 }
297 if (function_) {
298 // We're in the middle of a function, but the OpFunctionEnd is missing.
299 // Register the function anyway. This lets us write tests with less
300 // boilerplate.
301 module_->AddFunction(std::move(function_));
302 function_ = nullptr;
303 }
304 for (auto& function : *module_) {
305 for (auto& bb : function) bb.SetParent(&function);
306 }
307
308 // Copy any trailing Op*Line instruction into the module
309 module_->SetTrailingDbgLineInfo(std::move(dbg_line_info_));
310 }
311
312 } // namespace opt
313 } // namespace spvtools