/src/spirv-tools/source/opt/workaround1209.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2018 Google 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/opt/workaround1209.h" |
16 | | |
17 | | #include <list> |
18 | | #include <memory> |
19 | | #include <stack> |
20 | | #include <utility> |
21 | | |
22 | | namespace spvtools { |
23 | | namespace opt { |
24 | | |
25 | 0 | Pass::Status Workaround1209::Process() { |
26 | 0 | bool modified = false; |
27 | 0 | modified = RemoveOpUnreachableInLoops(); |
28 | 0 | return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange); |
29 | 0 | } |
30 | | |
31 | 0 | bool Workaround1209::RemoveOpUnreachableInLoops() { |
32 | 0 | bool modified = false; |
33 | 0 | for (auto& func : *get_module()) { |
34 | 0 | std::list<BasicBlock*> structured_order; |
35 | 0 | cfg()->ComputeStructuredOrder(&func, &*func.begin(), &structured_order); |
36 | | |
37 | | // Keep track of the loop merges. The top of the stack will always be the |
38 | | // loop merge for the loop that immediately contains the basic block being |
39 | | // processed. |
40 | 0 | std::stack<uint32_t> loop_merges; |
41 | 0 | for (BasicBlock* bb : structured_order) { |
42 | 0 | if (!loop_merges.empty() && bb->id() == loop_merges.top()) { |
43 | 0 | loop_merges.pop(); |
44 | 0 | } |
45 | |
|
46 | 0 | if (bb->tail()->opcode() == spv::Op::OpUnreachable) { |
47 | 0 | if (!loop_merges.empty()) { |
48 | | // We found an OpUnreachable inside a loop. |
49 | | // Replace it with an unconditional branch to the loop merge. |
50 | 0 | context()->KillInst(&*bb->tail()); |
51 | 0 | std::unique_ptr<Instruction> new_branch( |
52 | 0 | new Instruction(context(), spv::Op::OpBranch, 0, 0, |
53 | 0 | {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, |
54 | 0 | {loop_merges.top()}}})); |
55 | 0 | context()->AnalyzeDefUse(&*new_branch); |
56 | 0 | bb->AddInstruction(std::move(new_branch)); |
57 | 0 | modified = true; |
58 | 0 | } |
59 | 0 | } else { |
60 | 0 | if (bb->GetLoopMergeInst()) { |
61 | 0 | loop_merges.push(bb->MergeBlockIdIfAny()); |
62 | 0 | } |
63 | 0 | } |
64 | 0 | } |
65 | 0 | } |
66 | 0 | return modified; |
67 | 0 | } |
68 | | } // namespace opt |
69 | | } // namespace spvtools |