1 // Copyright (c) 2018 LunarG Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Validates correctness of the intra-block preconditions of SPIR-V
16 // instructions.
17
18 #include <string>
19
20 #include "source/val/instruction.h"
21 #include "source/val/validate.h"
22 #include "source/val/validation_state.h"
23
24 namespace spvtools {
25 namespace val {
26
27 enum {
28 // Status right after meeting OpFunction.
29 IN_NEW_FUNCTION,
30 // Status right after meeting the entry block.
31 IN_ENTRY_BLOCK,
32 // Status right after meeting non-entry blocks.
33 PHI_VALID,
34 // Status right after meeting non-OpVariable instructions in the entry block
35 // or non-OpPhi instructions in non-entry blocks, except OpLine.
36 PHI_AND_VAR_INVALID,
37 };
38
ValidateAdjacency(ValidationState_t & _)39 spv_result_t ValidateAdjacency(ValidationState_t& _) {
40 const auto& instructions = _.ordered_instructions();
41 int adjacency_status = PHI_AND_VAR_INVALID;
42
43 for (size_t i = 0; i < instructions.size(); ++i) {
44 const auto& inst = instructions[i];
45 switch (inst.opcode()) {
46 case spv::Op::OpFunction:
47 case spv::Op::OpFunctionParameter:
48 adjacency_status = IN_NEW_FUNCTION;
49 break;
50 case spv::Op::OpLabel:
51 adjacency_status =
52 adjacency_status == IN_NEW_FUNCTION ? IN_ENTRY_BLOCK : PHI_VALID;
53 break;
54 case spv::Op::OpExtInst:
55 // If it is a debug info instruction, we do not change the status to
56 // allow debug info instructions before OpVariable in a function.
57 // TODO(https://gitlab.khronos.org/spirv/SPIR-V/issues/533): We need
58 // to discuss the location of DebugScope, DebugNoScope, DebugDeclare,
59 // and DebugValue.
60 // NOTE: This does not apply to the non-semantic vulkan debug info.
61 if (!spvExtInstIsDebugInfo(inst.ext_inst_type()) ||
62 inst.ext_inst_type() ==
63 SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100) {
64 adjacency_status = PHI_AND_VAR_INVALID;
65 }
66 break;
67 case spv::Op::OpPhi:
68 if (adjacency_status != PHI_VALID) {
69 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
70 << "OpPhi must appear within a non-entry block before all "
71 << "non-OpPhi instructions "
72 << "(except for OpLine, which can be mixed with OpPhi).";
73 }
74 break;
75 case spv::Op::OpLine:
76 case spv::Op::OpNoLine:
77 break;
78 case spv::Op::OpLoopMerge:
79 adjacency_status = PHI_AND_VAR_INVALID;
80 if (i != (instructions.size() - 1)) {
81 switch (instructions[i + 1].opcode()) {
82 case spv::Op::OpBranch:
83 case spv::Op::OpBranchConditional:
84 break;
85 default:
86 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
87 << "OpLoopMerge must immediately precede either an "
88 << "OpBranch or OpBranchConditional instruction. "
89 << "OpLoopMerge must be the second-to-last instruction in "
90 << "its block.";
91 }
92 }
93 break;
94 case spv::Op::OpSelectionMerge:
95 adjacency_status = PHI_AND_VAR_INVALID;
96 if (i != (instructions.size() - 1)) {
97 switch (instructions[i + 1].opcode()) {
98 case spv::Op::OpBranchConditional:
99 case spv::Op::OpSwitch:
100 break;
101 default:
102 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
103 << "OpSelectionMerge must immediately precede either an "
104 << "OpBranchConditional or OpSwitch instruction. "
105 << "OpSelectionMerge must be the second-to-last "
106 << "instruction in its block.";
107 }
108 }
109 break;
110 case spv::Op::OpVariable:
111 if (inst.GetOperandAs<spv::StorageClass>(2) ==
112 spv::StorageClass::Function &&
113 adjacency_status != IN_ENTRY_BLOCK) {
114 return _.diag(SPV_ERROR_INVALID_DATA, &inst)
115 << "All OpVariable instructions in a function must be the "
116 "first instructions in the first block.";
117 }
118 break;
119 default:
120 adjacency_status = PHI_AND_VAR_INVALID;
121 break;
122 }
123 }
124
125 return SPV_SUCCESS;
126 }
127
128 } // namespace val
129 } // namespace spvtools
130