xref: /aosp_15_r20/external/angle/third_party/spirv-tools/src/source/val/construct.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 // Copyright (c) 2015-2016 The Khronos Group 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 #include "source/val/construct.h"
16 
17 #include <cassert>
18 #include <cstddef>
19 
20 #include "source/val/function.h"
21 #include "source/val/validation_state.h"
22 
23 namespace spvtools {
24 namespace val {
25 
Construct(ConstructType construct_type,BasicBlock * entry,BasicBlock * exit,std::vector<Construct * > constructs)26 Construct::Construct(ConstructType construct_type, BasicBlock* entry,
27                      BasicBlock* exit, std::vector<Construct*> constructs)
28     : type_(construct_type),
29       corresponding_constructs_(constructs),
30       entry_block_(entry),
31       exit_block_(exit) {}
32 
type() const33 ConstructType Construct::type() const { return type_; }
34 
corresponding_constructs() const35 const std::vector<Construct*>& Construct::corresponding_constructs() const {
36   return corresponding_constructs_;
37 }
corresponding_constructs()38 std::vector<Construct*>& Construct::corresponding_constructs() {
39   return corresponding_constructs_;
40 }
41 
ValidateConstructSize(ConstructType type,size_t size)42 bool ValidateConstructSize(ConstructType type, size_t size) {
43   switch (type) {
44     case ConstructType::kSelection:
45       return size == 0;
46     case ConstructType::kContinue:
47       return size == 1;
48     case ConstructType::kLoop:
49       return size == 1;
50     case ConstructType::kCase:
51       return size >= 1;
52     default:
53       assert(1 == 0 && "Type not defined");
54   }
55   return false;
56 }
57 
set_corresponding_constructs(std::vector<Construct * > constructs)58 void Construct::set_corresponding_constructs(
59     std::vector<Construct*> constructs) {
60   assert(ValidateConstructSize(type_, constructs.size()));
61   corresponding_constructs_ = constructs;
62 }
63 
entry_block() const64 const BasicBlock* Construct::entry_block() const { return entry_block_; }
entry_block()65 BasicBlock* Construct::entry_block() { return entry_block_; }
66 
exit_block() const67 const BasicBlock* Construct::exit_block() const { return exit_block_; }
exit_block()68 BasicBlock* Construct::exit_block() { return exit_block_; }
69 
set_exit(BasicBlock * block)70 void Construct::set_exit(BasicBlock* block) { exit_block_ = block; }
71 
blocks(Function *) const72 Construct::ConstructBlockSet Construct::blocks(Function* /*function*/) const {
73   const auto header = entry_block();
74   const auto exit = exit_block();
75   const bool is_continue = type() == ConstructType::kContinue;
76   const bool is_loop = type() == ConstructType::kLoop;
77   const BasicBlock* continue_header = nullptr;
78   if (is_loop) {
79     // The only corresponding construct for a loop is the continue.
80     continue_header = (*corresponding_constructs().begin())->entry_block();
81   }
82   std::vector<BasicBlock*> stack;
83   stack.push_back(const_cast<BasicBlock*>(header));
84   ConstructBlockSet construct_blocks;
85   while (!stack.empty()) {
86     auto* block = stack.back();
87     stack.pop_back();
88 
89     if (header->structurally_dominates(*block)) {
90       bool include = false;
91       if (is_continue && exit->structurally_postdominates(*block)) {
92         // Continue construct include blocks dominated by the continue target
93         // and post-dominated by the back-edge block.
94         include = true;
95       } else if (!exit->structurally_dominates(*block)) {
96         // Selection and loop constructs include blocks dominated by the header
97         // and not dominated by the merge.
98         include = true;
99         if (is_loop && continue_header->structurally_dominates(*block)) {
100           // Loop constructs have an additional constraint that they do not
101           // include blocks dominated by the continue construct. Since all
102           // blocks in the continue construct are dominated by the continue
103           // target, we just test for dominance by continue target.
104           include = false;
105         }
106       }
107       if (include) {
108         if (!construct_blocks.insert(block).second) continue;
109 
110         for (auto succ : *block->structural_successors()) {
111           stack.push_back(succ);
112         }
113       }
114     }
115   }
116 
117   return construct_blocks;
118 }
119 
IsStructuredExit(ValidationState_t & _,BasicBlock * dest) const120 bool Construct::IsStructuredExit(ValidationState_t& _, BasicBlock* dest) const {
121   // Structured Exits:
122   // - Selection:
123   //  - branch to its merge
124   //  - branch to nearest enclosing loop merge or continue
125   //  - branch to nearest enclosing switch selection merge
126   // - Loop:
127   //  - branch to its merge
128   //  - branch to its continue
129   // - Continue:
130   //  - branch to loop header
131   //  - branch to loop merge
132   //
133   // Note: we will never see a case construct here.
134   assert(type() != ConstructType::kCase);
135   if (type() == ConstructType::kLoop) {
136     auto header = entry_block();
137     auto terminator = header->terminator();
138     auto index = terminator - &_.ordered_instructions()[0];
139     auto merge_inst = &_.ordered_instructions()[index - 1];
140     auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
141     auto continue_block_id = merge_inst->GetOperandAs<uint32_t>(1u);
142     if (dest->id() == merge_block_id || dest->id() == continue_block_id) {
143       return true;
144     }
145   } else if (type() == ConstructType::kContinue) {
146     auto loop_construct = corresponding_constructs()[0];
147     auto header = loop_construct->entry_block();
148     auto terminator = header->terminator();
149     auto index = terminator - &_.ordered_instructions()[0];
150     auto merge_inst = &_.ordered_instructions()[index - 1];
151     auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
152     if (dest == header || dest->id() == merge_block_id) {
153       return true;
154     }
155   } else {
156     assert(type() == ConstructType::kSelection);
157     if (dest == exit_block()) {
158       return true;
159     }
160 
161     // The next block in the traversal is either:
162     //  i.  The header block that declares |block| as its merge block.
163     //  ii. The immediate dominator of |block|.
164     auto NextBlock = [](const BasicBlock* block) -> const BasicBlock* {
165       for (auto& use : block->label()->uses()) {
166         if ((use.first->opcode() == spv::Op::OpLoopMerge ||
167              use.first->opcode() == spv::Op::OpSelectionMerge) &&
168             use.second == 1 &&
169             use.first->block()->structurally_dominates(*block) &&
170             // A header likely declared itself as its merge.
171             use.first->block() != block) {
172           return use.first->block();
173         }
174       }
175       return block->immediate_structural_dominator();
176     };
177 
178     bool seen_switch = false;
179     auto header = entry_block();
180     auto block = NextBlock(header);
181     while (block) {
182       auto terminator = block->terminator();
183       auto index = terminator - &_.ordered_instructions()[0];
184       auto merge_inst = &_.ordered_instructions()[index - 1];
185       if (merge_inst->opcode() == spv::Op::OpLoopMerge ||
186           (header->terminator()->opcode() != spv::Op::OpSwitch &&
187            merge_inst->opcode() == spv::Op::OpSelectionMerge &&
188            terminator->opcode() == spv::Op::OpSwitch)) {
189         auto merge_target = merge_inst->GetOperandAs<uint32_t>(0u);
190         auto merge_block = merge_inst->function()->GetBlock(merge_target).first;
191         if (merge_block->structurally_dominates(*header)) {
192           block = NextBlock(block);
193           continue;
194         }
195 
196         if ((!seen_switch || merge_inst->opcode() == spv::Op::OpLoopMerge) &&
197             dest->id() == merge_target) {
198           return true;
199         } else if (merge_inst->opcode() == spv::Op::OpLoopMerge) {
200           auto continue_target = merge_inst->GetOperandAs<uint32_t>(1u);
201           if (dest->id() == continue_target) {
202             return true;
203           }
204         }
205 
206         if (terminator->opcode() == spv::Op::OpSwitch) {
207           seen_switch = true;
208         }
209 
210         // Hit an enclosing loop and didn't break or continue.
211         if (merge_inst->opcode() == spv::Op::OpLoopMerge) return false;
212       }
213 
214       block = NextBlock(block);
215     }
216   }
217 
218   return false;
219 }
220 
221 }  // namespace val
222 }  // namespace spvtools
223