SPIRV-Tools/source/opt/ir_loader.cpp

371 lines
15 KiB
C++
Raw Normal View History

// Copyright (c) 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "source/opt/ir_loader.h"
#include <utility>
#include "DebugInfo.h"
#include "OpenCLDebugInfo100.h"
2019-12-18 23:10:29 +00:00
#include "source/ext_inst.h"
#include "source/opt/ir_context.h"
#include "source/opt/log.h"
#include "source/opt/reflect.h"
#include "source/util/make_unique.h"
namespace spvtools {
namespace opt {
namespace {
constexpr uint32_t kExtInstSetIndex = 4;
constexpr uint32_t kLexicalScopeIndex = 5;
constexpr uint32_t kInlinedAtIndex = 6;
} // namespace
Adding an unique id to Instruction generated by IRContext Each instruction is given an unique id that can be used for ordering purposes. The ids are generated via the IRContext. Major changes: * Instructions now contain a uint32_t for unique id and a cached context pointer * Most constructors have been modified to take a context as input * unfortunately I cannot remove the default and copy constructors, but developers should avoid these * Added accessors to parents of basic block and function * Removed the copy constructors for BasicBlock and Function and replaced them with Clone functions * Reworked BuildModule to return an IRContext owning the built module * Since all instructions require a context, the context now becomes the basic unit for IR * Added a constructor to context to create an owned module internally * Replaced uses of Instruction's copy constructor with Clone whereever I found them * Reworked the linker functionality to perform clones into a different context instead of moves * Updated many tests to be consistent with the above changes * Still need to add new tests to cover added functionality * Added comparison operators to Instruction * Added an internal option to LinkerOptions to verify merged ids are unique * Added a test for the linker to verify merged ids are unique * Updated MergeReturnPass to supply a context * Updated DecorationManager to supply a context for cloned decorations * Reworked several portions of the def use tests in anticipation of next set of changes
2017-11-14 19:11:50 +00:00
IrLoader::IrLoader(const MessageConsumer& consumer, Module* m)
: consumer_(consumer),
Adding an unique id to Instruction generated by IRContext Each instruction is given an unique id that can be used for ordering purposes. The ids are generated via the IRContext. Major changes: * Instructions now contain a uint32_t for unique id and a cached context pointer * Most constructors have been modified to take a context as input * unfortunately I cannot remove the default and copy constructors, but developers should avoid these * Added accessors to parents of basic block and function * Removed the copy constructors for BasicBlock and Function and replaced them with Clone functions * Reworked BuildModule to return an IRContext owning the built module * Since all instructions require a context, the context now becomes the basic unit for IR * Added a constructor to context to create an owned module internally * Replaced uses of Instruction's copy constructor with Clone whereever I found them * Reworked the linker functionality to perform clones into a different context instead of moves * Updated many tests to be consistent with the above changes * Still need to add new tests to cover added functionality * Added comparison operators to Instruction * Added an internal option to LinkerOptions to verify merged ids are unique * Added a test for the linker to verify merged ids are unique * Updated MergeReturnPass to supply a context * Updated DecorationManager to supply a context for cloned decorations * Reworked several portions of the def use tests in anticipation of next set of changes
2017-11-14 19:11:50 +00:00
module_(m),
source_("<instruction>"),
inst_index_(0),
last_dbg_scope_(kNoDebugScope, kNoInlinedAt) {}
bool IsLineInst(const spv_parsed_instruction_t* inst) {
const auto opcode = static_cast<spv::Op>(inst->opcode);
if (IsOpLineInst(opcode)) return true;
if (opcode != spv::Op::OpExtInst) return false;
if (inst->ext_inst_type != SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100)
return false;
const uint32_t ext_inst_index = inst->words[kExtInstSetIndex];
const NonSemanticShaderDebugInfo100Instructions ext_inst_key =
NonSemanticShaderDebugInfo100Instructions(ext_inst_index);
return ext_inst_key == NonSemanticShaderDebugInfo100DebugLine ||
ext_inst_key == NonSemanticShaderDebugInfo100DebugNoLine;
}
bool IrLoader::AddInstruction(const spv_parsed_instruction_t* inst) {
++inst_index_;
if (IsLineInst(inst)) {
module()->SetContainsDebugInfo();
Propagate OpLine to all applied instructions in spirv-opt (#3951) Based on the OpLine spec, an OpLine instruction must be applied to the instructions physically following it up to the first occurrence of the next end of block, the next OpLine instruction, or the next OpNoLine instruction. ``` OpLine %file 0 0 OpNoLine OpLine %file 1 1 OpStore %foo %int_1 %value = OpLoad %int %foo OpLine %file 2 2 ``` For the above code, the current spirv-opt keeps three line instructions `OpLine %file 0 0`, `OpNoLine`, and `OpLine %file 1 1` in `std::vector<Instruction> dbg_line_insts_` of Instruction class for `OpStore %foo %int_1`. It does not put any line instruction to `std::vector<Instruction> dbg_line_insts_` of `%value = OpLoad %int %foo` even though `OpLine %file 1 1` must be applied to `%value = OpLoad %int %foo` based on the spec. This results in the missing line information for `%value = OpLoad %int %foo` while each spirv-opt pass optimizes the code. We have to put `OpLine %file 1 1` to `std::vector<Instruction> dbg_line_insts_` of both `%value = OpLoad %int %foo` and `OpStore %foo %int_1`. This commit conducts the line instruction propagation and skips emitting the eliminated line instructions at the end, which are the same with PropagateLineInfoPass and RedundantLineInfoElimPass. This commit removes PropagateLineInfoPass and RedundantLineInfoElimPass. KhronosGroup/glslang#2440 is a related PR that stop using PropagateLineInfoPass and RedundantLineInfoElimPass from glslang. When the code in this PR applied, the glslang tests will pass.
2020-10-29 17:06:30 +00:00
last_line_inst_.reset();
dbg_line_info_.emplace_back(module()->context(), *inst, last_dbg_scope_);
return true;
}
// If it is a DebugScope or DebugNoScope of debug extension, we do not
// create a new instruction, but simply keep the information in
// struct DebugScope.
const auto opcode = static_cast<spv::Op>(inst->opcode);
if (opcode == spv::Op::OpExtInst &&
spvExtInstIsDebugInfo(inst->ext_inst_type)) {
const uint32_t ext_inst_index = inst->words[kExtInstSetIndex];
if (inst->ext_inst_type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100 ||
inst->ext_inst_type ==
SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100) {
const CommonDebugInfoInstructions ext_inst_key =
CommonDebugInfoInstructions(ext_inst_index);
if (ext_inst_key == CommonDebugInfoDebugScope) {
uint32_t inlined_at = 0;
if (inst->num_words > kInlinedAtIndex)
inlined_at = inst->words[kInlinedAtIndex];
last_dbg_scope_ =
DebugScope(inst->words[kLexicalScopeIndex], inlined_at);
module()->SetContainsDebugInfo();
return true;
}
if (ext_inst_key == CommonDebugInfoDebugNoScope) {
last_dbg_scope_ = DebugScope(kNoDebugScope, kNoInlinedAt);
module()->SetContainsDebugInfo();
return true;
}
} else {
const DebugInfoInstructions ext_inst_key =
DebugInfoInstructions(ext_inst_index);
if (ext_inst_key == DebugInfoDebugScope) {
uint32_t inlined_at = 0;
if (inst->num_words > kInlinedAtIndex)
inlined_at = inst->words[kInlinedAtIndex];
last_dbg_scope_ =
DebugScope(inst->words[kLexicalScopeIndex], inlined_at);
module()->SetContainsDebugInfo();
return true;
}
if (ext_inst_key == DebugInfoDebugNoScope) {
last_dbg_scope_ = DebugScope(kNoDebugScope, kNoInlinedAt);
module()->SetContainsDebugInfo();
return true;
}
}
}
std::unique_ptr<Instruction> spv_inst(
Adding an unique id to Instruction generated by IRContext Each instruction is given an unique id that can be used for ordering purposes. The ids are generated via the IRContext. Major changes: * Instructions now contain a uint32_t for unique id and a cached context pointer * Most constructors have been modified to take a context as input * unfortunately I cannot remove the default and copy constructors, but developers should avoid these * Added accessors to parents of basic block and function * Removed the copy constructors for BasicBlock and Function and replaced them with Clone functions * Reworked BuildModule to return an IRContext owning the built module * Since all instructions require a context, the context now becomes the basic unit for IR * Added a constructor to context to create an owned module internally * Replaced uses of Instruction's copy constructor with Clone whereever I found them * Reworked the linker functionality to perform clones into a different context instead of moves * Updated many tests to be consistent with the above changes * Still need to add new tests to cover added functionality * Added comparison operators to Instruction * Added an internal option to LinkerOptions to verify merged ids are unique * Added a test for the linker to verify merged ids are unique * Updated MergeReturnPass to supply a context * Updated DecorationManager to supply a context for cloned decorations * Reworked several portions of the def use tests in anticipation of next set of changes
2017-11-14 19:11:50 +00:00
new Instruction(module()->context(), *inst, std::move(dbg_line_info_)));
Propagate OpLine to all applied instructions in spirv-opt (#3951) Based on the OpLine spec, an OpLine instruction must be applied to the instructions physically following it up to the first occurrence of the next end of block, the next OpLine instruction, or the next OpNoLine instruction. ``` OpLine %file 0 0 OpNoLine OpLine %file 1 1 OpStore %foo %int_1 %value = OpLoad %int %foo OpLine %file 2 2 ``` For the above code, the current spirv-opt keeps three line instructions `OpLine %file 0 0`, `OpNoLine`, and `OpLine %file 1 1` in `std::vector<Instruction> dbg_line_insts_` of Instruction class for `OpStore %foo %int_1`. It does not put any line instruction to `std::vector<Instruction> dbg_line_insts_` of `%value = OpLoad %int %foo` even though `OpLine %file 1 1` must be applied to `%value = OpLoad %int %foo` based on the spec. This results in the missing line information for `%value = OpLoad %int %foo` while each spirv-opt pass optimizes the code. We have to put `OpLine %file 1 1` to `std::vector<Instruction> dbg_line_insts_` of both `%value = OpLoad %int %foo` and `OpStore %foo %int_1`. This commit conducts the line instruction propagation and skips emitting the eliminated line instructions at the end, which are the same with PropagateLineInfoPass and RedundantLineInfoElimPass. This commit removes PropagateLineInfoPass and RedundantLineInfoElimPass. KhronosGroup/glslang#2440 is a related PR that stop using PropagateLineInfoPass and RedundantLineInfoElimPass from glslang. When the code in this PR applied, the glslang tests will pass.
2020-10-29 17:06:30 +00:00
if (!spv_inst->dbg_line_insts().empty()) {
if (extra_line_tracking_ &&
(!spv_inst->dbg_line_insts().back().IsNoLine())) {
Propagate OpLine to all applied instructions in spirv-opt (#3951) Based on the OpLine spec, an OpLine instruction must be applied to the instructions physically following it up to the first occurrence of the next end of block, the next OpLine instruction, or the next OpNoLine instruction. ``` OpLine %file 0 0 OpNoLine OpLine %file 1 1 OpStore %foo %int_1 %value = OpLoad %int %foo OpLine %file 2 2 ``` For the above code, the current spirv-opt keeps three line instructions `OpLine %file 0 0`, `OpNoLine`, and `OpLine %file 1 1` in `std::vector<Instruction> dbg_line_insts_` of Instruction class for `OpStore %foo %int_1`. It does not put any line instruction to `std::vector<Instruction> dbg_line_insts_` of `%value = OpLoad %int %foo` even though `OpLine %file 1 1` must be applied to `%value = OpLoad %int %foo` based on the spec. This results in the missing line information for `%value = OpLoad %int %foo` while each spirv-opt pass optimizes the code. We have to put `OpLine %file 1 1` to `std::vector<Instruction> dbg_line_insts_` of both `%value = OpLoad %int %foo` and `OpStore %foo %int_1`. This commit conducts the line instruction propagation and skips emitting the eliminated line instructions at the end, which are the same with PropagateLineInfoPass and RedundantLineInfoElimPass. This commit removes PropagateLineInfoPass and RedundantLineInfoElimPass. KhronosGroup/glslang#2440 is a related PR that stop using PropagateLineInfoPass and RedundantLineInfoElimPass from glslang. When the code in this PR applied, the glslang tests will pass.
2020-10-29 17:06:30 +00:00
last_line_inst_ = std::unique_ptr<Instruction>(
spv_inst->dbg_line_insts().back().Clone(module()->context()));
if (last_line_inst_->IsDebugLineInst())
last_line_inst_->SetResultId(module()->context()->TakeNextId());
Propagate OpLine to all applied instructions in spirv-opt (#3951) Based on the OpLine spec, an OpLine instruction must be applied to the instructions physically following it up to the first occurrence of the next end of block, the next OpLine instruction, or the next OpNoLine instruction. ``` OpLine %file 0 0 OpNoLine OpLine %file 1 1 OpStore %foo %int_1 %value = OpLoad %int %foo OpLine %file 2 2 ``` For the above code, the current spirv-opt keeps three line instructions `OpLine %file 0 0`, `OpNoLine`, and `OpLine %file 1 1` in `std::vector<Instruction> dbg_line_insts_` of Instruction class for `OpStore %foo %int_1`. It does not put any line instruction to `std::vector<Instruction> dbg_line_insts_` of `%value = OpLoad %int %foo` even though `OpLine %file 1 1` must be applied to `%value = OpLoad %int %foo` based on the spec. This results in the missing line information for `%value = OpLoad %int %foo` while each spirv-opt pass optimizes the code. We have to put `OpLine %file 1 1` to `std::vector<Instruction> dbg_line_insts_` of both `%value = OpLoad %int %foo` and `OpStore %foo %int_1`. This commit conducts the line instruction propagation and skips emitting the eliminated line instructions at the end, which are the same with PropagateLineInfoPass and RedundantLineInfoElimPass. This commit removes PropagateLineInfoPass and RedundantLineInfoElimPass. KhronosGroup/glslang#2440 is a related PR that stop using PropagateLineInfoPass and RedundantLineInfoElimPass from glslang. When the code in this PR applied, the glslang tests will pass.
2020-10-29 17:06:30 +00:00
}
dbg_line_info_.clear();
} else if (last_line_inst_ != nullptr) {
last_line_inst_->SetDebugScope(last_dbg_scope_);
spv_inst->dbg_line_insts().push_back(*last_line_inst_);
last_line_inst_ = std::unique_ptr<Instruction>(
spv_inst->dbg_line_insts().back().Clone(module()->context()));
if (last_line_inst_->IsDebugLineInst())
last_line_inst_->SetResultId(module()->context()->TakeNextId());
Propagate OpLine to all applied instructions in spirv-opt (#3951) Based on the OpLine spec, an OpLine instruction must be applied to the instructions physically following it up to the first occurrence of the next end of block, the next OpLine instruction, or the next OpNoLine instruction. ``` OpLine %file 0 0 OpNoLine OpLine %file 1 1 OpStore %foo %int_1 %value = OpLoad %int %foo OpLine %file 2 2 ``` For the above code, the current spirv-opt keeps three line instructions `OpLine %file 0 0`, `OpNoLine`, and `OpLine %file 1 1` in `std::vector<Instruction> dbg_line_insts_` of Instruction class for `OpStore %foo %int_1`. It does not put any line instruction to `std::vector<Instruction> dbg_line_insts_` of `%value = OpLoad %int %foo` even though `OpLine %file 1 1` must be applied to `%value = OpLoad %int %foo` based on the spec. This results in the missing line information for `%value = OpLoad %int %foo` while each spirv-opt pass optimizes the code. We have to put `OpLine %file 1 1` to `std::vector<Instruction> dbg_line_insts_` of both `%value = OpLoad %int %foo` and `OpStore %foo %int_1`. This commit conducts the line instruction propagation and skips emitting the eliminated line instructions at the end, which are the same with PropagateLineInfoPass and RedundantLineInfoElimPass. This commit removes PropagateLineInfoPass and RedundantLineInfoElimPass. KhronosGroup/glslang#2440 is a related PR that stop using PropagateLineInfoPass and RedundantLineInfoElimPass from glslang. When the code in this PR applied, the glslang tests will pass.
2020-10-29 17:06:30 +00:00
}
const char* src = source_.c_str();
spv_position_t loc = {inst_index_, 0, 0};
// Handle function and basic block boundaries first, then normal
// instructions.
if (opcode == spv::Op::OpFunction) {
if (function_ != nullptr) {
Error(consumer_, src, loc, "function inside function");
return false;
}
function_ = MakeUnique<Function>(std::move(spv_inst));
} else if (opcode == spv::Op::OpFunctionEnd) {
if (function_ == nullptr) {
Error(consumer_, src, loc,
"OpFunctionEnd without corresponding OpFunction");
return false;
}
if (block_ != nullptr) {
Error(consumer_, src, loc, "OpFunctionEnd inside basic block");
return false;
}
function_->SetFunctionEnd(std::move(spv_inst));
module_->AddFunction(std::move(function_));
function_ = nullptr;
} else if (opcode == spv::Op::OpLabel) {
if (function_ == nullptr) {
Error(consumer_, src, loc, "OpLabel outside function");
return false;
}
if (block_ != nullptr) {
Error(consumer_, src, loc, "OpLabel inside basic block");
return false;
}
block_ = MakeUnique<BasicBlock>(std::move(spv_inst));
} else if (spvOpcodeIsBlockTerminator(opcode)) {
if (function_ == nullptr) {
Error(consumer_, src, loc, "terminator instruction outside function");
return false;
}
if (block_ == nullptr) {
Error(consumer_, src, loc, "terminator instruction outside basic block");
return false;
}
if (last_dbg_scope_.GetLexicalScope() != kNoDebugScope)
spv_inst->SetDebugScope(last_dbg_scope_);
block_->AddInstruction(std::move(spv_inst));
function_->AddBasicBlock(std::move(block_));
block_ = nullptr;
last_dbg_scope_ = DebugScope(kNoDebugScope, kNoInlinedAt);
Propagate OpLine to all applied instructions in spirv-opt (#3951) Based on the OpLine spec, an OpLine instruction must be applied to the instructions physically following it up to the first occurrence of the next end of block, the next OpLine instruction, or the next OpNoLine instruction. ``` OpLine %file 0 0 OpNoLine OpLine %file 1 1 OpStore %foo %int_1 %value = OpLoad %int %foo OpLine %file 2 2 ``` For the above code, the current spirv-opt keeps three line instructions `OpLine %file 0 0`, `OpNoLine`, and `OpLine %file 1 1` in `std::vector<Instruction> dbg_line_insts_` of Instruction class for `OpStore %foo %int_1`. It does not put any line instruction to `std::vector<Instruction> dbg_line_insts_` of `%value = OpLoad %int %foo` even though `OpLine %file 1 1` must be applied to `%value = OpLoad %int %foo` based on the spec. This results in the missing line information for `%value = OpLoad %int %foo` while each spirv-opt pass optimizes the code. We have to put `OpLine %file 1 1` to `std::vector<Instruction> dbg_line_insts_` of both `%value = OpLoad %int %foo` and `OpStore %foo %int_1`. This commit conducts the line instruction propagation and skips emitting the eliminated line instructions at the end, which are the same with PropagateLineInfoPass and RedundantLineInfoElimPass. This commit removes PropagateLineInfoPass and RedundantLineInfoElimPass. KhronosGroup/glslang#2440 is a related PR that stop using PropagateLineInfoPass and RedundantLineInfoElimPass from glslang. When the code in this PR applied, the glslang tests will pass.
2020-10-29 17:06:30 +00:00
last_line_inst_.reset();
dbg_line_info_.clear();
} else {
if (function_ == nullptr) { // Outside function definition
SPIRV_ASSERT(consumer_, block_ == nullptr);
if (opcode == spv::Op::OpCapability) {
module_->AddCapability(std::move(spv_inst));
} else if (opcode == spv::Op::OpExtension) {
module_->AddExtension(std::move(spv_inst));
} else if (opcode == spv::Op::OpExtInstImport) {
module_->AddExtInstImport(std::move(spv_inst));
} else if (opcode == spv::Op::OpMemoryModel) {
module_->SetMemoryModel(std::move(spv_inst));
} else if (opcode == spv::Op::OpSamplerImageAddressingModeNV) {
module_->SetSampledImageAddressMode(std::move(spv_inst));
} else if (opcode == spv::Op::OpEntryPoint) {
module_->AddEntryPoint(std::move(spv_inst));
} else if (opcode == spv::Op::OpExecutionMode ||
opcode == spv::Op::OpExecutionModeId) {
module_->AddExecutionMode(std::move(spv_inst));
} else if (IsDebug1Inst(opcode)) {
module_->AddDebug1Inst(std::move(spv_inst));
} else if (IsDebug2Inst(opcode)) {
module_->AddDebug2Inst(std::move(spv_inst));
} else if (IsDebug3Inst(opcode)) {
module_->AddDebug3Inst(std::move(spv_inst));
} else if (IsAnnotationInst(opcode)) {
module_->AddAnnotationInst(std::move(spv_inst));
} else if (IsTypeInst(opcode)) {
module_->AddType(std::move(spv_inst));
} else if (IsConstantInst(opcode) || opcode == spv::Op::OpVariable ||
opcode == spv::Op::OpUndef) {
module_->AddGlobalValue(std::move(spv_inst));
} else if (opcode == spv::Op::OpExtInst &&
spvExtInstIsDebugInfo(inst->ext_inst_type)) {
module_->AddExtInstDebugInfo(std::move(spv_inst));
} else if (opcode == spv::Op::OpExtInst &&
spvExtInstIsNonSemantic(inst->ext_inst_type)) {
// If there are no functions, add the non-semantic instructions to the
// global values. Otherwise append it to the list of the last function.
auto func_begin = module_->begin();
auto func_end = module_->end();
if (func_begin == func_end) {
module_->AddGlobalValue(std::move(spv_inst));
} else {
(--func_end)->AddNonSemanticInstruction(std::move(spv_inst));
}
} else {
Errorf(consumer_, src, loc,
2019-12-18 23:10:29 +00:00
"Unhandled inst type (opcode: %d) found outside function "
"definition.",
opcode);
return false;
}
} else {
if (opcode == spv::Op::OpLoopMerge || opcode == spv::Op::OpSelectionMerge)
last_dbg_scope_ = DebugScope(kNoDebugScope, kNoInlinedAt);
if (last_dbg_scope_.GetLexicalScope() != kNoDebugScope)
spv_inst->SetDebugScope(last_dbg_scope_);
if (opcode == spv::Op::OpExtInst &&
spvExtInstIsDebugInfo(inst->ext_inst_type)) {
const uint32_t ext_inst_index = inst->words[kExtInstSetIndex];
if (inst->ext_inst_type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100) {
const OpenCLDebugInfo100Instructions ext_inst_key =
OpenCLDebugInfo100Instructions(ext_inst_index);
switch (ext_inst_key) {
case OpenCLDebugInfo100DebugDeclare: {
if (block_ == nullptr) // Inside function but outside blocks
function_->AddDebugInstructionInHeader(std::move(spv_inst));
else
block_->AddInstruction(std::move(spv_inst));
break;
}
case OpenCLDebugInfo100DebugValue: {
if (block_ == nullptr) // Inside function but outside blocks
function_->AddDebugInstructionInHeader(std::move(spv_inst));
else
block_->AddInstruction(std::move(spv_inst));
break;
}
default: {
Errorf(consumer_, src, loc,
"Debug info extension instruction other than DebugScope, "
"DebugNoScope, DebugFunctionDefinition, DebugDeclare, and "
"DebugValue found inside function",
opcode);
return false;
}
}
} else if (inst->ext_inst_type ==
SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100) {
const NonSemanticShaderDebugInfo100Instructions ext_inst_key =
NonSemanticShaderDebugInfo100Instructions(ext_inst_index);
switch (ext_inst_key) {
case NonSemanticShaderDebugInfo100DebugDeclare:
case NonSemanticShaderDebugInfo100DebugValue:
case NonSemanticShaderDebugInfo100DebugScope:
case NonSemanticShaderDebugInfo100DebugNoScope:
case NonSemanticShaderDebugInfo100DebugFunctionDefinition: {
if (block_ == nullptr) { // Inside function but outside blocks
Errorf(consumer_, src, loc,
"Debug info extension instruction found inside function "
"but outside block",
opcode);
} else {
block_->AddInstruction(std::move(spv_inst));
}
break;
}
default: {
Errorf(consumer_, src, loc,
"Debug info extension instruction other than DebugScope, "
"DebugNoScope, DebugDeclare, and DebugValue found inside "
"function",
opcode);
return false;
}
}
} else {
const DebugInfoInstructions ext_inst_key =
DebugInfoInstructions(ext_inst_index);
switch (ext_inst_key) {
case DebugInfoDebugDeclare: {
if (block_ == nullptr) // Inside function but outside blocks
function_->AddDebugInstructionInHeader(std::move(spv_inst));
else
block_->AddInstruction(std::move(spv_inst));
break;
}
case DebugInfoDebugValue: {
if (block_ == nullptr) // Inside function but outside blocks
function_->AddDebugInstructionInHeader(std::move(spv_inst));
else
block_->AddInstruction(std::move(spv_inst));
break;
}
default: {
Errorf(consumer_, src, loc,
"Debug info extension instruction other than DebugScope, "
"DebugNoScope, DebugDeclare, and DebugValue found inside "
"function",
opcode);
return false;
}
}
}
} else {
if (block_ == nullptr) { // Inside function but outside blocks
if (opcode != spv::Op::OpFunctionParameter) {
Errorf(consumer_, src, loc,
"Non-OpFunctionParameter (opcode: %d) found inside "
"function but outside basic block",
opcode);
return false;
}
function_->AddParameter(std::move(spv_inst));
} else {
block_->AddInstruction(std::move(spv_inst));
}
}
}
}
return true;
}
// Resolves internal references among the module, functions, basic blocks, etc.
// This function should be called after adding all instructions.
void IrLoader::EndModule() {
if (block_ && function_) {
// We're in the middle of a basic block, but the terminator is missing.
// Register the block anyway. This lets us write tests with less
// boilerplate.
function_->AddBasicBlock(std::move(block_));
block_ = nullptr;
}
if (function_) {
// We're in the middle of a function, but the OpFunctionEnd is missing.
// Register the function anyway. This lets us write tests with less
// boilerplate.
module_->AddFunction(std::move(function_));
function_ = nullptr;
}
for (auto& function : *module_) {
for (auto& bb : function) bb.SetParent(&function);
}
// Copy any trailing Op*Line instruction into the module
module_->SetTrailingDbgLineInfo(std::move(dbg_line_info_));
}
} // namespace opt
} // namespace spvtools