/src/spirv-tools/source/opt/strip_debug_info_pass.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2016 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/strip_debug_info_pass.h" |
16 | | #include "source/opt/ir_context.h" |
17 | | #include "source/util/string_utils.h" |
18 | | |
19 | | namespace spvtools { |
20 | | namespace opt { |
21 | | |
22 | 0 | Pass::Status StripDebugInfoPass::Process() { |
23 | 0 | bool uses_non_semantic_info = false; |
24 | 0 | for (auto& inst : context()->module()->extensions()) { |
25 | 0 | const std::string ext_name = inst.GetInOperand(0).AsString(); |
26 | 0 | if (ext_name == "SPV_KHR_non_semantic_info") { |
27 | 0 | uses_non_semantic_info = true; |
28 | 0 | } |
29 | 0 | } |
30 | |
|
31 | 0 | std::vector<Instruction*> to_kill; |
32 | | |
33 | | // if we use non-semantic info, it may reference OpString. Do a more |
34 | | // expensive pass checking the uses of the OpString to see if any are |
35 | | // OpExtInst on a non-semantic instruction set. If we're not using the |
36 | | // extension then we can do a simpler pass and kill all debug1 instructions |
37 | 0 | if (uses_non_semantic_info) { |
38 | 0 | for (auto& inst : context()->module()->debugs1()) { |
39 | 0 | switch (inst.opcode()) { |
40 | 0 | case spv::Op::OpString: { |
41 | 0 | analysis::DefUseManager* def_use = context()->get_def_use_mgr(); |
42 | | |
43 | | // see if this string is used anywhere by a non-semantic instruction |
44 | 0 | bool no_nonsemantic_use = |
45 | 0 | def_use->WhileEachUser(&inst, [def_use](Instruction* use) { |
46 | 0 | if (spvIsExtendedInstruction(use->opcode())) { |
47 | 0 | auto ext_inst_set = |
48 | 0 | def_use->GetDef(use->GetSingleWordInOperand(0u)); |
49 | 0 | const std::string extension_name = |
50 | 0 | ext_inst_set->GetInOperand(0).AsString(); |
51 | 0 | if (spvtools::utils::starts_with(extension_name, |
52 | 0 | "NonSemantic.")) { |
53 | | // found a non-semantic use, return false as we cannot |
54 | | // remove this OpString |
55 | 0 | return false; |
56 | 0 | } |
57 | 0 | } |
58 | | |
59 | | // other instructions can't be a non-semantic use |
60 | 0 | return true; |
61 | 0 | }); |
62 | |
|
63 | 0 | if (no_nonsemantic_use) to_kill.push_back(&inst); |
64 | |
|
65 | 0 | break; |
66 | 0 | } |
67 | | |
68 | 0 | default: |
69 | 0 | to_kill.push_back(&inst); |
70 | 0 | break; |
71 | 0 | } |
72 | 0 | } |
73 | 0 | } else { |
74 | 0 | for (auto& dbg : context()->debugs1()) to_kill.push_back(&dbg); |
75 | 0 | } |
76 | | |
77 | 0 | for (auto& dbg : context()->debugs2()) to_kill.push_back(&dbg); |
78 | 0 | for (auto& dbg : context()->debugs3()) to_kill.push_back(&dbg); |
79 | 0 | for (auto& dbg : context()->ext_inst_debuginfo()) to_kill.push_back(&dbg); |
80 | | |
81 | | // OpName must come first, since they may refer to other debug instructions. |
82 | | // If they are after the instructions that refer to, then they will be killed |
83 | | // when that instruction is killed, which will lead to a double kill. |
84 | 0 | std::sort(to_kill.begin(), to_kill.end(), |
85 | 0 | [](Instruction* lhs, Instruction* rhs) -> bool { |
86 | 0 | if (lhs->opcode() == spv::Op::OpName && |
87 | 0 | rhs->opcode() != spv::Op::OpName) |
88 | 0 | return true; |
89 | 0 | return false; |
90 | 0 | }); |
91 | |
|
92 | 0 | bool modified = !to_kill.empty(); |
93 | |
|
94 | 0 | for (auto* inst : to_kill) context()->KillInst(inst); |
95 | | |
96 | | // clear OpLine information |
97 | 0 | context()->module()->ForEachInst([&modified](Instruction* inst) { |
98 | 0 | modified |= !inst->dbg_line_insts().empty(); |
99 | 0 | inst->dbg_line_insts().clear(); |
100 | 0 | }); |
101 | |
|
102 | 0 | if (!get_module()->trailing_dbg_line_info().empty()) { |
103 | 0 | modified = true; |
104 | 0 | get_module()->trailing_dbg_line_info().clear(); |
105 | 0 | } |
106 | |
|
107 | 0 | return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange; |
108 | 0 | } |
109 | | |
110 | | } // namespace opt |
111 | | } // namespace spvtools |