strip_debug_info_pass.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #include "source/util/string_utils.h"
  17. namespace spvtools {
  18. namespace opt {
  19. Pass::Status StripDebugInfoPass::Process() {
  20. bool uses_non_semantic_info = false;
  21. for (auto& inst : context()->module()->extensions()) {
  22. const std::string ext_name = inst.GetInOperand(0).AsString();
  23. if (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 spv::Op::OpString: {
  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 (spvIsExtendedInstruction(use->opcode())) {
  41. auto ext_inst_set =
  42. def_use->GetDef(use->GetSingleWordInOperand(0u));
  43. const std::string extension_name =
  44. ext_inst_set->GetInOperand(0).AsString();
  45. if (spvtools::utils::starts_with(extension_name,
  46. "NonSemantic.")) {
  47. // found a non-semantic use, return false as we cannot
  48. // remove this OpString
  49. return false;
  50. }
  51. }
  52. // other instructions can't be a non-semantic use
  53. return true;
  54. });
  55. if (no_nonsemantic_use) to_kill.push_back(&inst);
  56. break;
  57. }
  58. default:
  59. to_kill.push_back(&inst);
  60. break;
  61. }
  62. }
  63. } else {
  64. for (auto& dbg : context()->debugs1()) to_kill.push_back(&dbg);
  65. }
  66. for (auto& dbg : context()->debugs2()) to_kill.push_back(&dbg);
  67. for (auto& dbg : context()->debugs3()) to_kill.push_back(&dbg);
  68. for (auto& dbg : context()->ext_inst_debuginfo()) to_kill.push_back(&dbg);
  69. // OpName must come first, since they may refer to other debug instructions.
  70. // If they are after the instructions that refer to, then they will be killed
  71. // when that instruction is killed, which will lead to a double kill.
  72. std::sort(to_kill.begin(), to_kill.end(),
  73. [](Instruction* lhs, Instruction* rhs) -> bool {
  74. if (lhs->opcode() == spv::Op::OpName &&
  75. rhs->opcode() != spv::Op::OpName)
  76. return true;
  77. return false;
  78. });
  79. bool modified = !to_kill.empty();
  80. for (auto* inst : to_kill) context()->KillInst(inst);
  81. // clear OpLine information
  82. context()->module()->ForEachInst([&modified](Instruction* inst) {
  83. modified |= !inst->dbg_line_insts().empty();
  84. inst->dbg_line_insts().clear();
  85. });
  86. if (!get_module()->trailing_dbg_line_info().empty()) {
  87. modified = true;
  88. get_module()->trailing_dbg_line_info().clear();
  89. }
  90. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  91. }
  92. } // namespace opt
  93. } // namespace spvtools