strip_reflect_info_pass.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (c) 2018 Google LLC
  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_reflect_info_pass.h"
  15. #include <cstring>
  16. #include <vector>
  17. #include "source/opt/instruction.h"
  18. #include "source/opt/ir_context.h"
  19. namespace spvtools {
  20. namespace opt {
  21. Pass::Status StripReflectInfoPass::Process() {
  22. bool modified = false;
  23. std::vector<Instruction*> to_remove;
  24. bool other_uses_for_decorate_string = false;
  25. for (auto& inst : context()->module()->annotations()) {
  26. switch (inst.opcode()) {
  27. case SpvOpDecorateStringGOOGLE:
  28. if (inst.GetSingleWordInOperand(1) == SpvDecorationHlslSemanticGOOGLE) {
  29. to_remove.push_back(&inst);
  30. } else {
  31. other_uses_for_decorate_string = true;
  32. }
  33. break;
  34. case SpvOpMemberDecorateStringGOOGLE:
  35. if (inst.GetSingleWordInOperand(2) == SpvDecorationHlslSemanticGOOGLE) {
  36. to_remove.push_back(&inst);
  37. } else {
  38. other_uses_for_decorate_string = true;
  39. }
  40. break;
  41. case SpvOpDecorateId:
  42. if (inst.GetSingleWordInOperand(1) ==
  43. SpvDecorationHlslCounterBufferGOOGLE) {
  44. to_remove.push_back(&inst);
  45. }
  46. break;
  47. default:
  48. break;
  49. }
  50. }
  51. for (auto& inst : context()->module()->extensions()) {
  52. const char* ext_name =
  53. reinterpret_cast<const char*>(&inst.GetInOperand(0).words[0]);
  54. if (0 == std::strcmp(ext_name, "SPV_GOOGLE_hlsl_functionality1")) {
  55. to_remove.push_back(&inst);
  56. } else if (!other_uses_for_decorate_string &&
  57. 0 == std::strcmp(ext_name, "SPV_GOOGLE_decorate_string")) {
  58. to_remove.push_back(&inst);
  59. } else if (0 == std::strcmp(ext_name, "SPV_KHR_non_semantic_info")) {
  60. to_remove.push_back(&inst);
  61. }
  62. }
  63. // clear all debug data now if it hasn't been cleared already, to remove any
  64. // remaining OpString that may have been referenced by non-semantic extinsts
  65. for (auto& dbg : context()->debugs1()) to_remove.push_back(&dbg);
  66. for (auto& dbg : context()->debugs2()) to_remove.push_back(&dbg);
  67. for (auto& dbg : context()->debugs3()) to_remove.push_back(&dbg);
  68. for (auto& dbg : context()->ext_inst_debuginfo()) to_remove.push_back(&dbg);
  69. // remove any extended inst imports that are non semantic
  70. std::unordered_set<uint32_t> non_semantic_sets;
  71. for (auto& inst : context()->module()->ext_inst_imports()) {
  72. assert(inst.opcode() == SpvOpExtInstImport &&
  73. "Expecting an import of an extension's instruction set.");
  74. const char* extension_name =
  75. reinterpret_cast<const char*>(&inst.GetInOperand(0).words[0]);
  76. if (0 == std::strncmp(extension_name, "NonSemantic.", 12)) {
  77. non_semantic_sets.insert(inst.result_id());
  78. to_remove.push_back(&inst);
  79. }
  80. }
  81. // if we removed some non-semantic sets, then iterate over the instructions in
  82. // the module to remove any OpExtInst that referenced those sets
  83. if (!non_semantic_sets.empty()) {
  84. context()->module()->ForEachInst(
  85. [&non_semantic_sets, &to_remove](Instruction* inst) {
  86. if (inst->opcode() == SpvOpExtInst) {
  87. if (non_semantic_sets.find(inst->GetSingleWordInOperand(0)) !=
  88. non_semantic_sets.end()) {
  89. to_remove.push_back(inst);
  90. }
  91. }
  92. });
  93. }
  94. // OpName must come first, since they may refer to other debug instructions.
  95. // If they are after the instructions that refer to, then they will be killed
  96. // when that instruction is killed, which will lead to a double kill.
  97. std::sort(to_remove.begin(), to_remove.end(),
  98. [](Instruction* lhs, Instruction* rhs) -> bool {
  99. if (lhs->opcode() == SpvOpName && rhs->opcode() != SpvOpName)
  100. return true;
  101. return false;
  102. });
  103. for (auto* inst : to_remove) {
  104. modified = true;
  105. context()->KillInst(inst);
  106. }
  107. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  108. }
  109. } // namespace opt
  110. } // namespace spvtools