2016-05-22 18:17:39 +00:00
|
|
|
// Copyright (c) 2016 Google Inc.
|
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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
|
2016-05-22 18:17:39 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-05-22 18:17:39 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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.
|
2016-05-22 18:17:39 +00:00
|
|
|
|
|
|
|
#include "ir_loader.h"
|
|
|
|
|
2016-08-20 03:17:44 +00:00
|
|
|
#include "log.h"
|
2016-05-22 18:17:39 +00:00
|
|
|
#include "reflect.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace ir {
|
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
IrLoader::IrLoader(const MessageConsumer& consumer, Module* m)
|
2016-09-21 14:53:15 +00:00
|
|
|
: consumer_(consumer),
|
2017-11-14 19:11:50 +00:00
|
|
|
module_(m),
|
2016-09-21 14:53:15 +00:00
|
|
|
source_("<instruction>"),
|
|
|
|
inst_index_(0) {}
|
|
|
|
|
|
|
|
bool IrLoader::AddInstruction(const spv_parsed_instruction_t* inst) {
|
|
|
|
++inst_index_;
|
2016-05-22 18:17:39 +00:00
|
|
|
const auto opcode = static_cast<SpvOp>(inst->opcode);
|
|
|
|
if (IsDebugLineInst(opcode)) {
|
2017-11-14 19:11:50 +00:00
|
|
|
dbg_line_info_.push_back(Instruction(module()->context(), *inst));
|
2016-09-21 14:53:15 +00:00
|
|
|
return true;
|
2016-05-22 18:17:39 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 23:20:35 +00:00
|
|
|
std::unique_ptr<Instruction> spv_inst(
|
2017-11-14 19:11:50 +00:00
|
|
|
new Instruction(module()->context(), *inst, std::move(dbg_line_info_)));
|
2016-05-22 18:17:39 +00:00
|
|
|
dbg_line_info_.clear();
|
2016-09-21 14:53:15 +00:00
|
|
|
|
|
|
|
const char* src = source_.c_str();
|
|
|
|
spv_position_t loc = {inst_index_, 0, 0};
|
|
|
|
|
2016-05-22 18:17:39 +00:00
|
|
|
// Handle function and basic block boundaries first, then normal
|
|
|
|
// instructions.
|
|
|
|
if (opcode == SpvOpFunction) {
|
2016-09-21 14:53:15 +00:00
|
|
|
if (function_ != nullptr) {
|
|
|
|
Error(consumer_, src, loc, "function inside function");
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-22 18:17:39 +00:00
|
|
|
function_.reset(new Function(std::move(spv_inst)));
|
|
|
|
} else if (opcode == SpvOpFunctionEnd) {
|
2016-09-21 14:53:15 +00:00
|
|
|
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;
|
|
|
|
}
|
2016-08-20 13:47:00 +00:00
|
|
|
function_->SetFunctionEnd(std::move(spv_inst));
|
2016-08-09 23:20:35 +00:00
|
|
|
module_->AddFunction(std::move(function_));
|
2016-05-22 18:17:39 +00:00
|
|
|
function_ = nullptr;
|
|
|
|
} else if (opcode == SpvOpLabel) {
|
2016-09-21 14:53:15 +00:00
|
|
|
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;
|
|
|
|
}
|
2016-05-22 18:17:39 +00:00
|
|
|
block_.reset(new BasicBlock(std::move(spv_inst)));
|
|
|
|
} else if (IsTerminatorInst(opcode)) {
|
2016-09-21 14:53:15 +00:00
|
|
|
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;
|
|
|
|
}
|
2016-05-22 18:17:39 +00:00
|
|
|
block_->AddInstruction(std::move(spv_inst));
|
2016-08-09 23:20:35 +00:00
|
|
|
function_->AddBasicBlock(std::move(block_));
|
2016-05-22 18:17:39 +00:00
|
|
|
block_ = nullptr;
|
|
|
|
} else {
|
|
|
|
if (function_ == nullptr) { // Outside function definition
|
2016-08-20 03:17:44 +00:00
|
|
|
SPIRV_ASSERT(consumer_, block_ == nullptr);
|
2016-05-22 18:17:39 +00:00
|
|
|
if (opcode == SpvOpCapability) {
|
|
|
|
module_->AddCapability(std::move(spv_inst));
|
|
|
|
} else if (opcode == SpvOpExtension) {
|
|
|
|
module_->AddExtension(std::move(spv_inst));
|
|
|
|
} else if (opcode == SpvOpExtInstImport) {
|
|
|
|
module_->AddExtInstImport(std::move(spv_inst));
|
|
|
|
} else if (opcode == SpvOpMemoryModel) {
|
|
|
|
module_->SetMemoryModel(std::move(spv_inst));
|
|
|
|
} else if (opcode == SpvOpEntryPoint) {
|
|
|
|
module_->AddEntryPoint(std::move(spv_inst));
|
|
|
|
} else if (opcode == SpvOpExecutionMode) {
|
|
|
|
module_->AddExecutionMode(std::move(spv_inst));
|
2017-07-13 00:16:51 +00:00
|
|
|
} else if (IsDebug1Inst(opcode)) {
|
|
|
|
module_->AddDebug1Inst(std::move(spv_inst));
|
|
|
|
} else if (IsDebug2Inst(opcode)) {
|
|
|
|
module_->AddDebug2Inst(std::move(spv_inst));
|
2017-10-20 22:04:20 +00:00
|
|
|
} else if (IsDebug3Inst(opcode)) {
|
|
|
|
module_->AddDebug3Inst(std::move(spv_inst));
|
2016-05-22 18:17:39 +00:00
|
|
|
} else if (IsAnnotationInst(opcode)) {
|
|
|
|
module_->AddAnnotationInst(std::move(spv_inst));
|
|
|
|
} else if (IsTypeInst(opcode)) {
|
|
|
|
module_->AddType(std::move(spv_inst));
|
2016-08-12 18:59:56 +00:00
|
|
|
} else if (IsConstantInst(opcode) || opcode == SpvOpVariable ||
|
|
|
|
opcode == SpvOpUndef) {
|
|
|
|
module_->AddGlobalValue(std::move(spv_inst));
|
2016-05-22 18:17:39 +00:00
|
|
|
} else {
|
2016-08-20 03:17:44 +00:00
|
|
|
SPIRV_UNIMPLEMENTED(consumer_,
|
2017-12-03 20:27:21 +00:00
|
|
|
"unhandled inst type outside function definition");
|
2016-05-22 18:17:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (block_ == nullptr) { // Inside function but outside blocks
|
2016-09-21 14:53:15 +00:00
|
|
|
if (opcode != SpvOpFunctionParameter) {
|
|
|
|
Errorf(consumer_, src, loc,
|
|
|
|
"Non-OpFunctionParameter (opcode: %d) found inside "
|
|
|
|
"function but outside basic block",
|
|
|
|
opcode);
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-22 18:17:39 +00:00
|
|
|
function_->AddParameter(std::move(spv_inst));
|
|
|
|
} else {
|
|
|
|
block_->AddInstruction(std::move(spv_inst));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-21 14:53:15 +00:00
|
|
|
return true;
|
2016-05-22 18:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resolves internal references among the module, functions, basic blocks, etc.
|
|
|
|
// This function should be called after adding all instructions.
|
|
|
|
void IrLoader::EndModule() {
|
2016-08-25 20:42:36 +00:00
|
|
|
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;
|
|
|
|
}
|
2016-08-10 14:02:28 +00:00
|
|
|
for (auto& function : *module_) {
|
|
|
|
for (auto& bb : function) bb.SetParent(&function);
|
|
|
|
function.SetParent(module_);
|
2016-05-22 18:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ir
|
|
|
|
} // namespace spvtools
|