2016-01-14 04:25:11 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and/or associated documentation files (the
|
|
|
|
// "Materials"), to deal in the Materials without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Materials, and to
|
|
|
|
// permit persons to whom the Materials are furnished to do so, subject to
|
|
|
|
// the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Materials.
|
|
|
|
//
|
|
|
|
// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
|
|
|
|
// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
|
|
|
|
// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
|
|
|
|
// https://www.khronos.org/registry/
|
|
|
|
//
|
|
|
|
// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
|
|
|
|
|
2016-06-02 22:51:05 +00:00
|
|
|
#include "validate.h"
|
|
|
|
|
2016-02-22 20:25:50 +00:00
|
|
|
#include <functional>
|
2016-06-02 22:51:05 +00:00
|
|
|
|
2016-01-14 04:25:11 +00:00
|
|
|
#include "opcode.h"
|
2016-06-02 22:51:05 +00:00
|
|
|
#include "val/ValidationState.h"
|
2016-01-14 04:25:11 +00:00
|
|
|
|
|
|
|
using std::function;
|
|
|
|
using libspirv::ValidationState_t;
|
|
|
|
|
|
|
|
namespace {
|
2016-04-02 21:54:59 +00:00
|
|
|
// This function takes the opcode of an instruction and returns
|
2016-01-14 04:25:11 +00:00
|
|
|
// a function object that will return true if the index
|
|
|
|
// of the operand can be forwarad declared. This function will
|
|
|
|
// used in the SSA validation stage of the pipeline
|
|
|
|
function<bool(unsigned)> getCanBeForwardDeclaredFunction(SpvOp opcode) {
|
|
|
|
function<bool(unsigned index)> out;
|
|
|
|
switch (opcode) {
|
|
|
|
case SpvOpExecutionMode:
|
|
|
|
case SpvOpEntryPoint:
|
|
|
|
case SpvOpName:
|
|
|
|
case SpvOpMemberName:
|
|
|
|
case SpvOpSelectionMerge:
|
|
|
|
case SpvOpDecorate:
|
|
|
|
case SpvOpMemberDecorate:
|
|
|
|
case SpvOpBranch:
|
|
|
|
case SpvOpLoopMerge:
|
|
|
|
out = [](unsigned) { return true; };
|
|
|
|
break;
|
|
|
|
case SpvOpGroupDecorate:
|
|
|
|
case SpvOpGroupMemberDecorate:
|
|
|
|
case SpvOpBranchConditional:
|
|
|
|
case SpvOpSwitch:
|
|
|
|
out = [](unsigned index) { return index != 0; };
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SpvOpFunctionCall:
|
|
|
|
out = [](unsigned index) { return index == 2; };
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SpvOpPhi:
|
|
|
|
out = [](unsigned index) { return index > 1; };
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SpvOpEnqueueKernel:
|
|
|
|
out = [](unsigned index) { return index == 8; };
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SpvOpGetKernelNDrangeSubGroupCount:
|
|
|
|
case SpvOpGetKernelNDrangeMaxSubGroupSize:
|
|
|
|
out = [](unsigned index) { return index == 3; };
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SpvOpGetKernelWorkGroupSize:
|
|
|
|
case SpvOpGetKernelPreferredWorkGroupSizeMultiple:
|
|
|
|
out = [](unsigned index) { return index == 2; };
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
out = [](unsigned) { return false; };
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace libspirv {
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// TODO(umar): Use dominators to correctly validate SSA. For example, the result
|
|
|
|
// id from a 'then' block cannot dominate its usage in the 'else' block. This
|
2016-04-02 21:54:59 +00:00
|
|
|
// is not yet performed by this function.
|
2016-01-14 04:25:11 +00:00
|
|
|
spv_result_t SsaPass(ValidationState_t& _,
|
|
|
|
const spv_parsed_instruction_t* inst) {
|
|
|
|
auto can_have_forward_declared_ids =
|
2016-03-31 21:26:31 +00:00
|
|
|
getCanBeForwardDeclaredFunction(static_cast<SpvOp>(inst->opcode));
|
2016-01-14 04:25:11 +00:00
|
|
|
|
2016-02-22 20:25:50 +00:00
|
|
|
for (unsigned i = 0; i < inst->num_operands; i++) {
|
|
|
|
const spv_parsed_operand_t& operand = inst->operands[i];
|
|
|
|
const spv_operand_type_t& type = operand.type;
|
|
|
|
const uint32_t* operand_ptr = inst->words + operand.offset;
|
2016-01-14 04:25:11 +00:00
|
|
|
|
2016-02-22 20:25:50 +00:00
|
|
|
auto ret = SPV_ERROR_INTERNAL;
|
|
|
|
switch (type) {
|
|
|
|
case SPV_OPERAND_TYPE_RESULT_ID:
|
|
|
|
_.removeIfForwardDeclared(*operand_ptr);
|
|
|
|
ret = SPV_SUCCESS;
|
|
|
|
break;
|
|
|
|
case SPV_OPERAND_TYPE_ID:
|
|
|
|
case SPV_OPERAND_TYPE_TYPE_ID:
|
|
|
|
case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
|
|
|
|
case SPV_OPERAND_TYPE_SCOPE_ID:
|
|
|
|
if (_.isDefinedId(*operand_ptr)) {
|
2016-01-15 16:25:11 +00:00
|
|
|
ret = SPV_SUCCESS;
|
2016-02-22 20:25:50 +00:00
|
|
|
} else if (can_have_forward_declared_ids(i)) {
|
|
|
|
ret = _.forwardDeclareId(*operand_ptr);
|
|
|
|
} else {
|
|
|
|
ret = _.diag(SPV_ERROR_INVALID_ID) << "ID "
|
|
|
|
<< _.getIdName(*operand_ptr)
|
|
|
|
<< " has not been defined";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = SPV_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (SPV_SUCCESS != ret) {
|
|
|
|
return ret;
|
2016-01-14 04:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|