2016-01-07 18:44:22 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
2015-05-22 17:26:19 +00:00
|
|
|
//
|
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
|
2015-05-22 17:26:19 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-05-22 17:26:19 +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.
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2018-07-11 14:27:34 +00:00
|
|
|
#include "source/val/validate.h"
|
2016-06-02 22:51:05 +00:00
|
|
|
|
2016-01-15 16:25:11 +00:00
|
|
|
#include <cassert>
|
2016-06-02 22:51:05 +00:00
|
|
|
|
2016-08-06 17:25:02 +00:00
|
|
|
#include <algorithm>
|
2015-05-22 17:26:19 +00:00
|
|
|
#include <iostream>
|
2018-03-30 14:00:32 +00:00
|
|
|
#include <iterator>
|
2017-11-23 17:11:15 +00:00
|
|
|
#include <stack>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <string>
|
2016-08-06 17:29:33 +00:00
|
|
|
#include <unordered_set>
|
|
|
|
#include <utility>
|
2015-05-22 17:26:19 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2018-07-11 14:27:34 +00:00
|
|
|
#include "source/diagnostic.h"
|
|
|
|
#include "source/instruction.h"
|
|
|
|
#include "source/opcode.h"
|
|
|
|
#include "source/operand.h"
|
|
|
|
#include "source/spirv_validator_options.h"
|
|
|
|
#include "source/val/function.h"
|
|
|
|
#include "source/val/validation_state.h"
|
2016-02-17 19:44:00 +00:00
|
|
|
#include "spirv-tools/libspirv.h"
|
2015-11-11 17:45:23 +00:00
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
namespace spvtools {
|
2018-07-10 03:18:44 +00:00
|
|
|
namespace val {
|
2016-07-08 13:44:10 +00:00
|
|
|
|
2018-08-02 14:00:52 +00:00
|
|
|
spv_result_t UpdateIdUse(ValidationState_t& _, const Instruction* inst) {
|
|
|
|
for (auto& operand : inst->operands()) {
|
|
|
|
const spv_operand_type_t& type = operand.type;
|
|
|
|
const uint32_t operand_id = inst->word(operand.offset);
|
|
|
|
if (spvIsIdType(type) && type != SPV_OPERAND_TYPE_RESULT_ID) {
|
|
|
|
if (auto def = _.FindDef(operand_id))
|
|
|
|
def->RegisterUse(inst, operand.offset);
|
2016-08-06 17:29:33 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-02 14:00:52 +00:00
|
|
|
|
2016-08-06 17:29:33 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-07-13 22:57:52 +00:00
|
|
|
/// This function checks all ID definitions dominate their use in the CFG.
|
|
|
|
///
|
|
|
|
/// This function will iterate over all ID definitions that are defined in the
|
|
|
|
/// functions of a module and make sure that the definitions appear in a
|
|
|
|
/// block that dominates their use.
|
|
|
|
///
|
|
|
|
/// NOTE: This function does NOT check module scoped functions which are
|
|
|
|
/// checked during the initial binary parse in the IdPass below
|
2018-10-03 19:59:40 +00:00
|
|
|
spv_result_t CheckIdDefinitionDominateUse(ValidationState_t& _) {
|
2018-08-23 17:56:41 +00:00
|
|
|
std::vector<const Instruction*> phi_instructions;
|
|
|
|
std::unordered_set<uint32_t> phi_ids;
|
|
|
|
for (const auto& inst : _.ordered_instructions()) {
|
|
|
|
if (inst.id() == 0) continue;
|
|
|
|
if (const Function* func = inst.function()) {
|
|
|
|
if (const BasicBlock* block = inst.block()) {
|
2016-07-13 22:57:52 +00:00
|
|
|
// If the Id is defined within a block then make sure all references to
|
|
|
|
// that Id appear in a blocks that are dominated by the defining block
|
2018-08-23 17:56:41 +00:00
|
|
|
for (auto& use_index_pair : inst.uses()) {
|
2016-08-06 17:29:33 +00:00
|
|
|
const Instruction* use = use_index_pair.first;
|
|
|
|
if (const BasicBlock* use_block = use->block()) {
|
2016-08-06 17:40:01 +00:00
|
|
|
if (use_block->reachable() == false) continue;
|
|
|
|
if (use->opcode() == SpvOpPhi) {
|
2018-08-23 17:56:41 +00:00
|
|
|
if (phi_ids.insert(use->id()).second) {
|
|
|
|
phi_instructions.push_back(use);
|
|
|
|
}
|
2016-08-06 17:40:01 +00:00
|
|
|
} else if (!block->dominates(*use->block())) {
|
2018-08-01 18:55:20 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, use_block->label())
|
2018-08-23 17:56:41 +00:00
|
|
|
<< "ID " << _.getIdName(inst.id()) << " defined in block "
|
|
|
|
<< _.getIdName(block->id())
|
2016-08-06 17:29:33 +00:00
|
|
|
<< " does not dominate its use in block "
|
|
|
|
<< _.getIdName(use_block->id());
|
|
|
|
}
|
2016-07-13 22:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If the Ids defined within a function but not in a block(i.e. function
|
|
|
|
// parameters, block ids), then make sure all references to that Id
|
|
|
|
// appear within the same function
|
2018-08-23 17:56:41 +00:00
|
|
|
for (auto use : inst.uses()) {
|
|
|
|
const Instruction* user = use.first;
|
|
|
|
if (user->function() && user->function() != func) {
|
2018-08-01 18:55:20 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, _.FindDef(func->id()))
|
2018-08-23 17:56:41 +00:00
|
|
|
<< "ID " << _.getIdName(inst.id()) << " used in function "
|
|
|
|
<< _.getIdName(user->function()->id())
|
2016-07-13 22:57:52 +00:00
|
|
|
<< " is used outside of it's defining function "
|
|
|
|
<< _.getIdName(func->id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// NOTE: Ids defined outside of functions must appear before they are used
|
|
|
|
// This check is being performed in the IdPass function
|
|
|
|
}
|
2016-08-06 17:40:01 +00:00
|
|
|
|
|
|
|
// Check all OpPhi parent blocks are dominated by the variable's defining
|
|
|
|
// blocks
|
|
|
|
for (const Instruction* phi : phi_instructions) {
|
|
|
|
if (phi->block()->reachable() == false) continue;
|
|
|
|
for (size_t i = 3; i < phi->operands().size(); i += 2) {
|
2016-08-09 22:23:40 +00:00
|
|
|
const Instruction* variable = _.FindDef(phi->word(i));
|
2016-08-06 17:40:01 +00:00
|
|
|
const BasicBlock* parent =
|
2016-08-09 22:23:40 +00:00
|
|
|
phi->function()->GetBlock(phi->word(i + 1)).first;
|
2018-03-23 14:10:10 +00:00
|
|
|
if (variable->block() && parent->reachable() &&
|
|
|
|
!variable->block()->dominates(*parent)) {
|
2018-08-01 18:55:20 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, phi)
|
2016-08-06 17:40:01 +00:00
|
|
|
<< "In OpPhi instruction " << _.getIdName(phi->id()) << ", ID "
|
|
|
|
<< _.getIdName(variable->id())
|
|
|
|
<< " definition does not dominate its parent "
|
|
|
|
<< _.getIdName(parent->id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 22:57:52 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-07-08 13:44:10 +00:00
|
|
|
// Performs SSA validation on the IDs of an instruction. The
|
|
|
|
// can_have_forward_declared_ids functor should return true if the
|
|
|
|
// instruction operand's ID can be forward referenced.
|
2018-07-31 13:54:25 +00:00
|
|
|
spv_result_t IdPass(ValidationState_t& _, Instruction* inst) {
|
2016-07-08 13:44:10 +00:00
|
|
|
auto can_have_forward_declared_ids =
|
2020-01-23 22:04:30 +00:00
|
|
|
inst->opcode() == SpvOpExtInst &&
|
|
|
|
spvExtInstIsDebugInfo(inst->ext_inst_type())
|
|
|
|
? spvDbgInfoExtOperandCanBeForwardDeclaredFunction(
|
|
|
|
inst->ext_inst_type(), inst->word(4))
|
|
|
|
: spvOperandCanBeForwardDeclaredFunction(inst->opcode());
|
2016-07-08 13:44:10 +00:00
|
|
|
|
2016-09-14 15:04:19 +00:00
|
|
|
// Keep track of a result id defined by this instruction. 0 means it
|
|
|
|
// does not define an id.
|
|
|
|
uint32_t result_id = 0;
|
|
|
|
|
2018-07-31 13:54:25 +00:00
|
|
|
for (unsigned i = 0; i < inst->operands().size(); i++) {
|
|
|
|
const spv_parsed_operand_t& operand = inst->operand(i);
|
2016-07-08 13:44:10 +00:00
|
|
|
const spv_operand_type_t& type = operand.type;
|
2016-09-14 15:04:19 +00:00
|
|
|
// We only care about Id operands, which are a single word.
|
2018-07-31 13:54:25 +00:00
|
|
|
const uint32_t operand_word = inst->word(operand.offset);
|
2016-07-08 13:44:10 +00:00
|
|
|
|
|
|
|
auto ret = SPV_ERROR_INTERNAL;
|
|
|
|
switch (type) {
|
|
|
|
case SPV_OPERAND_TYPE_RESULT_ID:
|
2016-09-14 15:04:19 +00:00
|
|
|
// NOTE: Multiple Id definitions are being checked by the binary parser.
|
|
|
|
//
|
|
|
|
// Defer undefined-forward-reference removal until after we've analyzed
|
|
|
|
// the remaining operands to this instruction. Deferral only matters
|
2018-07-31 13:54:25 +00:00
|
|
|
// for OpPhi since it's the only case where it defines its own forward
|
2016-09-14 15:04:19 +00:00
|
|
|
// reference. Other instructions that can have forward references
|
|
|
|
// either don't define a value or the forward reference is to a function
|
|
|
|
// Id (and hence defined outside of a function body).
|
|
|
|
result_id = operand_word;
|
|
|
|
// NOTE: The result Id is added (in RegisterInstruction) *after* all of
|
|
|
|
// the other Ids have been checked to avoid premature use in the same
|
|
|
|
// instruction.
|
2016-07-08 13:44:10 +00:00
|
|
|
ret = SPV_SUCCESS;
|
|
|
|
break;
|
|
|
|
case SPV_OPERAND_TYPE_ID:
|
|
|
|
case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
|
|
|
|
case SPV_OPERAND_TYPE_SCOPE_ID:
|
2018-12-03 16:03:52 +00:00
|
|
|
if (const auto def = _.FindDef(operand_word)) {
|
|
|
|
const auto opcode = inst->opcode();
|
|
|
|
if (spvOpcodeGeneratesType(def->opcode()) &&
|
|
|
|
!spvOpcodeGeneratesType(opcode) && !spvOpcodeIsDebug(opcode) &&
|
Add validation rules for OpenCL.DebugInfo.100 extension (#3133)
Add validation rules for DebugCompilationUnit, DebugSource,
DebugTypeBasic, DebugTypeVector, DebugTypeArray, DebugTypedef,
DebugTypeFunction, DebugTypeEnum, DebugTypeComposite,
DebugTypeMember, DebugTypeInheritance, DebugFunction,
DebugFunctionDeclaration, DebugLexicalBlock, DebugScope,
DebugLocalVariable, DebugDeclare, DebugExpression.
2020-02-25 21:47:03 +00:00
|
|
|
!inst->IsDebugInfo() && !inst->IsNonSemantic() &&
|
|
|
|
!spvOpcodeIsDecoration(opcode) && opcode != SpvOpFunction &&
|
2019-02-25 22:43:11 +00:00
|
|
|
opcode != SpvOpCooperativeMatrixLengthNV &&
|
|
|
|
!(opcode == SpvOpSpecConstantOp &&
|
|
|
|
inst->word(3) == SpvOpCooperativeMatrixLengthNV)) {
|
2018-12-03 16:03:52 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Operand " << _.getIdName(operand_word)
|
|
|
|
<< " cannot be a type";
|
2019-01-03 18:55:43 +00:00
|
|
|
} else if (def->type_id() == 0 && !spvOpcodeGeneratesType(opcode) &&
|
Add validation rules for OpenCL.DebugInfo.100 extension (#3133)
Add validation rules for DebugCompilationUnit, DebugSource,
DebugTypeBasic, DebugTypeVector, DebugTypeArray, DebugTypedef,
DebugTypeFunction, DebugTypeEnum, DebugTypeComposite,
DebugTypeMember, DebugTypeInheritance, DebugFunction,
DebugFunctionDeclaration, DebugLexicalBlock, DebugScope,
DebugLocalVariable, DebugDeclare, DebugExpression.
2020-02-25 21:47:03 +00:00
|
|
|
!spvOpcodeIsDebug(opcode) && !inst->IsDebugInfo() &&
|
|
|
|
!inst->IsNonSemantic() && !spvOpcodeIsDecoration(opcode) &&
|
2019-01-03 18:55:43 +00:00
|
|
|
!spvOpcodeIsBranch(opcode) && opcode != SpvOpPhi &&
|
|
|
|
opcode != SpvOpExtInst && opcode != SpvOpExtInstImport &&
|
|
|
|
opcode != SpvOpSelectionMerge &&
|
2019-02-25 22:43:11 +00:00
|
|
|
opcode != SpvOpLoopMerge && opcode != SpvOpFunction &&
|
|
|
|
opcode != SpvOpCooperativeMatrixLengthNV &&
|
|
|
|
!(opcode == SpvOpSpecConstantOp &&
|
|
|
|
inst->word(3) == SpvOpCooperativeMatrixLengthNV)) {
|
2019-01-03 18:55:43 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Operand " << _.getIdName(operand_word)
|
|
|
|
<< " requires a type";
|
2019-12-18 23:10:29 +00:00
|
|
|
} else if (def->IsNonSemantic() && !inst->IsNonSemantic()) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Operand " << _.getIdName(operand_word)
|
|
|
|
<< " in semantic instruction cannot be a non-semantic "
|
|
|
|
"instruction";
|
2018-12-03 16:03:52 +00:00
|
|
|
} else {
|
|
|
|
ret = SPV_SUCCESS;
|
|
|
|
}
|
2016-07-08 13:44:10 +00:00
|
|
|
} else if (can_have_forward_declared_ids(i)) {
|
2020-12-08 13:46:47 +00:00
|
|
|
if (spvOpcodeGeneratesType(inst->opcode()) &&
|
2019-09-25 22:11:22 +00:00
|
|
|
!_.IsForwardPointer(operand_word)) {
|
|
|
|
ret = _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "Operand " << _.getIdName(operand_word)
|
|
|
|
<< " requires a previous definition";
|
|
|
|
} else {
|
|
|
|
ret = _.ForwardDeclareId(operand_word);
|
|
|
|
}
|
2016-07-08 13:44:10 +00:00
|
|
|
} else {
|
2018-08-01 18:55:20 +00:00
|
|
|
ret = _.diag(SPV_ERROR_INVALID_ID, inst)
|
2017-11-08 17:40:02 +00:00
|
|
|
<< "ID " << _.getIdName(operand_word)
|
|
|
|
<< " has not been defined";
|
2016-07-08 13:44:10 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-08-28 03:45:32 +00:00
|
|
|
case SPV_OPERAND_TYPE_TYPE_ID:
|
|
|
|
if (_.IsDefinedId(operand_word)) {
|
|
|
|
auto* def = _.FindDef(operand_word);
|
|
|
|
if (!spvOpcodeGeneratesType(def->opcode())) {
|
|
|
|
ret = _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "ID " << _.getIdName(operand_word) << " is not a type id";
|
|
|
|
} else {
|
|
|
|
ret = SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = _.diag(SPV_ERROR_INVALID_ID, inst)
|
|
|
|
<< "ID " << _.getIdName(operand_word)
|
|
|
|
<< " has not been defined";
|
|
|
|
}
|
|
|
|
break;
|
2016-07-08 13:44:10 +00:00
|
|
|
default:
|
|
|
|
ret = SPV_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
2018-07-31 13:54:25 +00:00
|
|
|
if (SPV_SUCCESS != ret) return ret;
|
2016-09-14 15:04:19 +00:00
|
|
|
}
|
2018-07-31 13:54:25 +00:00
|
|
|
if (result_id) _.RemoveIfForwardDeclared(result_id);
|
|
|
|
|
2016-07-08 13:44:10 +00:00
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
2018-07-07 13:38:00 +00:00
|
|
|
|
2018-07-10 03:18:44 +00:00
|
|
|
} // namespace val
|
|
|
|
} // namespace spvtools
|