process_lines_pass.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (c) 2018 The Khronos Group Inc.
  2. // Copyright (c) 2018 Valve Corporation
  3. // Copyright (c) 2018 LunarG Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. #include "source/opt/process_lines_pass.h"
  17. #include <set>
  18. #include <unordered_set>
  19. #include <vector>
  20. namespace {
  21. // Input Operand Indices
  22. static const int kSpvLineFileInIdx = 0;
  23. static const int kSpvLineLineInIdx = 1;
  24. static const int kSpvLineColInIdx = 2;
  25. } // anonymous namespace
  26. namespace spvtools {
  27. namespace opt {
  28. Pass::Status ProcessLinesPass::Process() {
  29. bool modified = ProcessLines();
  30. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  31. }
  32. bool ProcessLinesPass::ProcessLines() {
  33. bool modified = false;
  34. uint32_t file_id = 0;
  35. uint32_t line = 0;
  36. uint32_t col = 0;
  37. // Process types, globals, constants
  38. for (Instruction& inst : get_module()->types_values())
  39. modified |= line_process_func_(&inst, &file_id, &line, &col);
  40. // Process functions
  41. for (Function& function : *get_module()) {
  42. modified |= line_process_func_(&function.DefInst(), &file_id, &line, &col);
  43. function.ForEachParam(
  44. [this, &modified, &file_id, &line, &col](Instruction* param) {
  45. modified |= line_process_func_(param, &file_id, &line, &col);
  46. });
  47. for (BasicBlock& block : function) {
  48. modified |=
  49. line_process_func_(block.GetLabelInst(), &file_id, &line, &col);
  50. for (Instruction& inst : block) {
  51. modified |= line_process_func_(&inst, &file_id, &line, &col);
  52. // Don't process terminal instruction if preceeded by merge
  53. if (inst.opcode() == SpvOpSelectionMerge ||
  54. inst.opcode() == SpvOpLoopMerge)
  55. break;
  56. }
  57. // Nullify line info after each block.
  58. file_id = 0;
  59. }
  60. modified |= line_process_func_(function.EndInst(), &file_id, &line, &col);
  61. }
  62. return modified;
  63. }
  64. bool ProcessLinesPass::PropagateLine(Instruction* inst, uint32_t* file_id,
  65. uint32_t* line, uint32_t* col) {
  66. bool modified = false;
  67. // only the last debug instruction needs to be considered
  68. auto line_itr = inst->dbg_line_insts().rbegin();
  69. // if no line instructions, propagate previous info
  70. if (line_itr == inst->dbg_line_insts().rend()) {
  71. // if no current line info, add OpNoLine, else OpLine
  72. if (*file_id == 0)
  73. inst->dbg_line_insts().push_back(Instruction(context(), SpvOpNoLine));
  74. else
  75. inst->dbg_line_insts().push_back(Instruction(
  76. context(), SpvOpLine, 0, 0,
  77. {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {*file_id}},
  78. {spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {*line}},
  79. {spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER, {*col}}}));
  80. modified = true;
  81. } else {
  82. // else pre-existing line instruction, so update source line info
  83. if (line_itr->opcode() == SpvOpNoLine) {
  84. *file_id = 0;
  85. } else {
  86. assert(line_itr->opcode() == SpvOpLine && "unexpected debug inst");
  87. *file_id = line_itr->GetSingleWordInOperand(kSpvLineFileInIdx);
  88. *line = line_itr->GetSingleWordInOperand(kSpvLineLineInIdx);
  89. *col = line_itr->GetSingleWordInOperand(kSpvLineColInIdx);
  90. }
  91. }
  92. return modified;
  93. }
  94. bool ProcessLinesPass::EliminateDeadLines(Instruction* inst, uint32_t* file_id,
  95. uint32_t* line, uint32_t* col) {
  96. // If no debug line instructions, return without modifying lines
  97. if (inst->dbg_line_insts().empty()) return false;
  98. // Only the last debug instruction needs to be considered; delete all others
  99. bool modified = inst->dbg_line_insts().size() > 1;
  100. Instruction last_inst = inst->dbg_line_insts().back();
  101. inst->dbg_line_insts().clear();
  102. // If last line is OpNoLine
  103. if (last_inst.opcode() == SpvOpNoLine) {
  104. // If no propagated line info, throw away redundant OpNoLine
  105. if (*file_id == 0) {
  106. modified = true;
  107. // Else replace OpNoLine and propagate no line info
  108. } else {
  109. inst->dbg_line_insts().push_back(last_inst);
  110. *file_id = 0;
  111. }
  112. } else {
  113. // Else last line is OpLine
  114. assert(last_inst.opcode() == SpvOpLine && "unexpected debug inst");
  115. // If propagated info matches last line, throw away last line
  116. if (*file_id == last_inst.GetSingleWordInOperand(kSpvLineFileInIdx) &&
  117. *line == last_inst.GetSingleWordInOperand(kSpvLineLineInIdx) &&
  118. *col == last_inst.GetSingleWordInOperand(kSpvLineColInIdx)) {
  119. modified = true;
  120. } else {
  121. // Else replace last line and propagate line info
  122. *file_id = last_inst.GetSingleWordInOperand(kSpvLineFileInIdx);
  123. *line = last_inst.GetSingleWordInOperand(kSpvLineLineInIdx);
  124. *col = last_inst.GetSingleWordInOperand(kSpvLineColInIdx);
  125. inst->dbg_line_insts().push_back(last_inst);
  126. }
  127. }
  128. return modified;
  129. }
  130. ProcessLinesPass::ProcessLinesPass(uint32_t func_id) {
  131. if (func_id == kLinesPropagateLines) {
  132. line_process_func_ = [this](Instruction* inst, uint32_t* file_id,
  133. uint32_t* line, uint32_t* col) {
  134. return PropagateLine(inst, file_id, line, col);
  135. };
  136. } else {
  137. assert(func_id == kLinesEliminateDeadLines && "unknown Lines param");
  138. line_process_func_ = [this](Instruction* inst, uint32_t* file_id,
  139. uint32_t* line, uint32_t* col) {
  140. return EliminateDeadLines(inst, file_id, line, col);
  141. };
  142. }
  143. }
  144. } // namespace opt
  145. } // namespace spvtools