2018-01-25 23:32:01 +00:00
|
|
|
// Copyright (c) 2018 LunarG 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.
|
|
|
|
|
|
|
|
// Validates correctness of the intra-block preconditions of SPIR-V
|
|
|
|
// instructions.
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2018-07-11 14:27:34 +00:00
|
|
|
#include "source/val/instruction.h"
|
2023-03-28 18:18:19 +00:00
|
|
|
#include "source/val/validate.h"
|
2018-07-11 14:27:34 +00:00
|
|
|
#include "source/val/validation_state.h"
|
2018-01-25 23:32:01 +00:00
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
namespace spvtools {
|
2018-07-10 03:18:44 +00:00
|
|
|
namespace val {
|
2018-01-25 23:32:01 +00:00
|
|
|
|
2018-09-27 15:09:47 +00:00
|
|
|
enum {
|
2018-09-28 17:45:43 +00:00
|
|
|
// Status right after meeting OpFunction.
|
2018-09-27 15:09:47 +00:00
|
|
|
IN_NEW_FUNCTION,
|
2018-09-28 17:45:43 +00:00
|
|
|
// Status right after meeting the entry block.
|
2018-09-27 15:09:47 +00:00
|
|
|
IN_ENTRY_BLOCK,
|
2018-09-28 17:45:43 +00:00
|
|
|
// Status right after meeting non-entry blocks.
|
2018-09-27 15:09:47 +00:00
|
|
|
PHI_VALID,
|
2018-09-28 17:45:43 +00:00
|
|
|
// Status right after meeting non-OpVariable instructions in the entry block
|
|
|
|
// or non-OpPhi instructions in non-entry blocks, except OpLine.
|
|
|
|
PHI_AND_VAR_INVALID,
|
2018-09-27 15:09:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
spv_result_t ValidateAdjacency(ValidationState_t& _) {
|
2018-01-25 23:32:01 +00:00
|
|
|
const auto& instructions = _.ordered_instructions();
|
2018-09-28 17:45:43 +00:00
|
|
|
int adjacency_status = PHI_AND_VAR_INVALID;
|
2018-08-02 14:00:52 +00:00
|
|
|
|
2018-09-27 15:09:47 +00:00
|
|
|
for (size_t i = 0; i < instructions.size(); ++i) {
|
|
|
|
const auto& inst = instructions[i];
|
|
|
|
switch (inst.opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpFunction:
|
|
|
|
case spv::Op::OpFunctionParameter:
|
2018-09-28 17:45:43 +00:00
|
|
|
adjacency_status = IN_NEW_FUNCTION;
|
2018-09-27 15:09:47 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpLabel:
|
2018-09-28 17:45:43 +00:00
|
|
|
adjacency_status =
|
|
|
|
adjacency_status == IN_NEW_FUNCTION ? IN_ENTRY_BLOCK : PHI_VALID;
|
2018-09-27 15:09:47 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpExtInst:
|
2024-06-06 10:17:51 +00:00
|
|
|
case spv::Op::OpExtInstWithForwardRefsKHR:
|
2020-01-23 22:04:30 +00:00
|
|
|
// If it is a debug info instruction, we do not change the status to
|
|
|
|
// allow debug info instructions before OpVariable in a function.
|
|
|
|
// TODO(https://gitlab.khronos.org/spirv/SPIR-V/issues/533): We need
|
|
|
|
// to discuss the location of DebugScope, DebugNoScope, DebugDeclare,
|
|
|
|
// and DebugValue.
|
2021-08-23 21:38:26 +00:00
|
|
|
// NOTE: This does not apply to the non-semantic vulkan debug info.
|
|
|
|
if (!spvExtInstIsDebugInfo(inst.ext_inst_type()) ||
|
|
|
|
inst.ext_inst_type() ==
|
2021-09-15 18:38:53 +00:00
|
|
|
SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100) {
|
2020-01-23 22:04:30 +00:00
|
|
|
adjacency_status = PHI_AND_VAR_INVALID;
|
|
|
|
}
|
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpPhi:
|
2018-09-28 17:45:43 +00:00
|
|
|
if (adjacency_status != PHI_VALID) {
|
2018-09-27 15:09:47 +00:00
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, &inst)
|
|
|
|
<< "OpPhi must appear within a non-entry block before all "
|
|
|
|
<< "non-OpPhi instructions "
|
|
|
|
<< "(except for OpLine, which can be mixed with OpPhi).";
|
2018-01-25 23:32:01 +00:00
|
|
|
}
|
2018-09-27 15:09:47 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpLine:
|
|
|
|
case spv::Op::OpNoLine:
|
2018-09-27 15:09:47 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpLoopMerge:
|
2018-09-28 17:45:43 +00:00
|
|
|
adjacency_status = PHI_AND_VAR_INVALID;
|
2018-09-27 15:09:47 +00:00
|
|
|
if (i != (instructions.size() - 1)) {
|
|
|
|
switch (instructions[i + 1].opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpBranch:
|
|
|
|
case spv::Op::OpBranchConditional:
|
2018-09-27 15:09:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, &inst)
|
|
|
|
<< "OpLoopMerge must immediately precede either an "
|
|
|
|
<< "OpBranch or OpBranchConditional instruction. "
|
|
|
|
<< "OpLoopMerge must be the second-to-last instruction in "
|
|
|
|
<< "its block.";
|
|
|
|
}
|
2018-01-25 23:32:01 +00:00
|
|
|
}
|
2018-09-27 15:09:47 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpSelectionMerge:
|
2018-09-28 17:45:43 +00:00
|
|
|
adjacency_status = PHI_AND_VAR_INVALID;
|
2018-09-27 15:09:47 +00:00
|
|
|
if (i != (instructions.size() - 1)) {
|
|
|
|
switch (instructions[i + 1].opcode()) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpBranchConditional:
|
|
|
|
case spv::Op::OpSwitch:
|
2018-09-27 15:09:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, &inst)
|
|
|
|
<< "OpSelectionMerge must immediately precede either an "
|
|
|
|
<< "OpBranchConditional or OpSwitch instruction. "
|
|
|
|
<< "OpSelectionMerge must be the second-to-last "
|
|
|
|
<< "instruction in its block.";
|
|
|
|
}
|
2018-01-25 23:32:01 +00:00
|
|
|
}
|
2018-09-27 15:09:47 +00:00
|
|
|
break;
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpVariable:
|
|
|
|
if (inst.GetOperandAs<spv::StorageClass>(2) ==
|
|
|
|
spv::StorageClass::Function &&
|
2018-09-28 17:45:43 +00:00
|
|
|
adjacency_status != IN_ENTRY_BLOCK) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, &inst)
|
|
|
|
<< "All OpVariable instructions in a function must be the "
|
|
|
|
"first instructions in the first block.";
|
|
|
|
}
|
|
|
|
break;
|
2024-07-17 18:51:37 +00:00
|
|
|
case spv::Op::OpUntypedVariableKHR:
|
|
|
|
if (inst.GetOperandAs<spv::StorageClass>(2) ==
|
|
|
|
spv::StorageClass::Function &&
|
|
|
|
adjacency_status != IN_ENTRY_BLOCK) {
|
|
|
|
return _.diag(SPV_ERROR_INVALID_DATA, &inst)
|
|
|
|
<< "All OpUntypedVariableKHR instructions in a function must "
|
|
|
|
"be the first instructions in the first block.";
|
|
|
|
}
|
|
|
|
break;
|
2018-09-27 15:09:47 +00:00
|
|
|
default:
|
2018-09-28 17:45:43 +00:00
|
|
|
adjacency_status = PHI_AND_VAR_INVALID;
|
2018-09-27 15:09:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-01-25 23:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return SPV_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-07-10 03:18:44 +00:00
|
|
|
} // namespace val
|
2018-07-07 13:38:00 +00:00
|
|
|
} // namespace spvtools
|