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 
18 namespace spvtools {
19 namespace opt {
20 
Process()21 Pass::Status StripDebugInfoPass::Process() {
22   bool uses_non_semantic_info = false;
23   for (auto& inst : context()->module()->extensions()) {
24     const char* ext_name =
25         reinterpret_cast<const char*>(&inst.GetInOperand(0).words[0]);
26     if (0 == std::strcmp(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 SpvOpString: {
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() == SpvOpExtInst) {
47                   auto ext_inst_set =
48                       def_use->GetDef(use->GetSingleWordInOperand(0u));
49                   const char* extension_name = reinterpret_cast<const char*>(
50                       &ext_inst_set->GetInOperand(0).words[0]);
51                   if (0 == std::strncmp(extension_name, "NonSemantic.", 12)) {
52                     // found a non-semantic use, return false as we cannot
53                     // remove this OpString
54                     return false;
55                   }
56                 }
57 
58                 // other instructions can't be a non-semantic use
59                 return true;
60               });
61 
62           if (no_nonsemantic_use) to_kill.push_back(&inst);
63 
64           break;
65         }
66 
67         default:
68           to_kill.push_back(&inst);
69           break;
70       }
71     }
72   } else {
73     for (auto& dbg : context()->debugs1()) to_kill.push_back(&dbg);
74   }
75 
76   for (auto& dbg : context()->debugs2()) to_kill.push_back(&dbg);
77   for (auto& dbg : context()->debugs3()) to_kill.push_back(&dbg);
78   for (auto& dbg : context()->ext_inst_debuginfo()) to_kill.push_back(&dbg);
79 
80   // OpName must come first, since they may refer to other debug instructions.
81   // If they are after the instructions that refer to, then they will be killed
82   // when that instruction is killed, which will lead to a double kill.
83   std::sort(to_kill.begin(), to_kill.end(),
84             [](Instruction* lhs, Instruction* rhs) -> bool {
85               if (lhs->opcode() == SpvOpName && rhs->opcode() != SpvOpName)
86                 return true;
87               return false;
88             });
89 
90   bool modified = !to_kill.empty();
91 
92   for (auto* inst : to_kill) context()->KillInst(inst);
93 
94   // clear OpLine information
95   context()->module()->ForEachInst([&modified](Instruction* inst) {
96     modified |= !inst->dbg_line_insts().empty();
97     inst->dbg_line_insts().clear();
98   });
99 
100   if (!get_module()->trailing_dbg_line_info().empty()) {
101     modified = true;
102     get_module()->trailing_dbg_line_info().clear();
103   }
104 
105   return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
106 }
107 
108 }  // namespace opt
109 }  // namespace spvtools
110