strip_debug_info_pass.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 modified = !context()->debugs1().empty() ||
  20. !context()->debugs2().empty() ||
  21. !context()->debugs3().empty();
  22. std::vector<Instruction*> to_kill;
  23. for (auto& dbg : context()->debugs1()) to_kill.push_back(&dbg);
  24. for (auto& dbg : context()->debugs2()) to_kill.push_back(&dbg);
  25. for (auto& dbg : context()->debugs3()) to_kill.push_back(&dbg);
  26. // OpName must come first, since they may refer to other debug instructions.
  27. // If they are after the instructions that refer to, then they will be killed
  28. // when that instruction is killed, which will lead to a double kill.
  29. std::sort(to_kill.begin(), to_kill.end(),
  30. [](Instruction* lhs, Instruction* rhs) -> bool {
  31. if (lhs->opcode() == SpvOpName && rhs->opcode() != SpvOpName)
  32. return true;
  33. return false;
  34. });
  35. for (auto* inst : to_kill) context()->KillInst(inst);
  36. context()->module()->ForEachInst([&modified](Instruction* inst) {
  37. modified |= !inst->dbg_line_insts().empty();
  38. inst->dbg_line_insts().clear();
  39. });
  40. if (!get_module()->trailing_dbg_line_info().empty()) {
  41. modified = true;
  42. get_module()->trailing_dbg_line_info().clear();
  43. }
  44. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  45. }
  46. } // namespace opt
  47. } // namespace spvtools