• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Process()22 Pass::Status StripDebugInfoPass::Process() {
23   bool uses_non_semantic_info = false;
24   for (auto& inst : context()->module()->extensions()) {
25     const std::string ext_name = inst.GetInOperand(0).AsString();
26     if (ext_name == "SPV_KHR_non_semantic_info") {
27       uses_non_semantic_info = true;
28     }
29   }
30 
31   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   if (uses_non_semantic_info) {
38     for (auto& inst : context()->module()->debugs1()) {
39       switch (inst.opcode()) {
40         case spv::Op::OpString: {
41           analysis::DefUseManager* def_use = context()->get_def_use_mgr();
42 
43           // see if this string is used anywhere by a non-semantic instruction
44           bool no_nonsemantic_use =
45               def_use->WhileEachUser(&inst, [def_use](Instruction* use) {
46                 if (use->opcode() == spv::Op::OpExtInst) {
47                   auto ext_inst_set =
48                       def_use->GetDef(use->GetSingleWordInOperand(0u));
49                   const std::string extension_name =
50                       ext_inst_set->GetInOperand(0).AsString();
51                   if (spvtools::utils::starts_with(extension_name,
52                                                    "NonSemantic.")) {
53                     // found a non-semantic use, return false as we cannot
54                     // remove this OpString
55                     return false;
56                   }
57                 }
58 
59                 // other instructions can't be a non-semantic use
60                 return true;
61               });
62 
63           if (no_nonsemantic_use) to_kill.push_back(&inst);
64 
65           break;
66         }
67 
68         default:
69           to_kill.push_back(&inst);
70           break;
71       }
72     }
73   } else {
74     for (auto& dbg : context()->debugs1()) to_kill.push_back(&dbg);
75   }
76 
77   for (auto& dbg : context()->debugs2()) to_kill.push_back(&dbg);
78   for (auto& dbg : context()->debugs3()) to_kill.push_back(&dbg);
79   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   std::sort(to_kill.begin(), to_kill.end(),
85             [](Instruction* lhs, Instruction* rhs) -> bool {
86               if (lhs->opcode() == spv::Op::OpName &&
87                   rhs->opcode() != spv::Op::OpName)
88                 return true;
89               return false;
90             });
91 
92   bool modified = !to_kill.empty();
93 
94   for (auto* inst : to_kill) context()->KillInst(inst);
95 
96   // clear OpLine information
97   context()->module()->ForEachInst([&modified](Instruction* inst) {
98     modified |= !inst->dbg_line_insts().empty();
99     inst->dbg_line_insts().clear();
100   });
101 
102   if (!get_module()->trailing_dbg_line_info().empty()) {
103     modified = true;
104     get_module()->trailing_dbg_line_info().clear();
105   }
106 
107   return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
108 }
109 
110 }  // namespace opt
111 }  // namespace spvtools
112