strip_debug_info_pass.cpp 3.9 KB

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