strip_nonsemantic_info_pass.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_nonsemantic_info_pass.h"
  15. #include <vector>
  16. #include "source/opt/instruction.h"
  17. #include "source/opt/ir_context.h"
  18. #include "source/util/string_utils.h"
  19. namespace spvtools {
  20. namespace opt {
  21. Pass::Status StripNonSemanticInfoPass::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 spv::Op::OpDecorateStringGOOGLE:
  28. if (spv::Decoration(inst.GetSingleWordInOperand(1)) ==
  29. spv::Decoration::HlslSemanticGOOGLE ||
  30. spv::Decoration(inst.GetSingleWordInOperand(1)) ==
  31. spv::Decoration::UserTypeGOOGLE) {
  32. to_remove.push_back(&inst);
  33. } else {
  34. other_uses_for_decorate_string = true;
  35. }
  36. break;
  37. case spv::Op::OpMemberDecorateStringGOOGLE:
  38. if (spv::Decoration(inst.GetSingleWordInOperand(2)) ==
  39. spv::Decoration::HlslSemanticGOOGLE ||
  40. spv::Decoration(inst.GetSingleWordInOperand(2)) ==
  41. spv::Decoration::UserTypeGOOGLE) {
  42. to_remove.push_back(&inst);
  43. } else {
  44. other_uses_for_decorate_string = true;
  45. }
  46. break;
  47. case spv::Op::OpDecorateId:
  48. if (spv::Decoration(inst.GetSingleWordInOperand(1)) ==
  49. spv::Decoration::HlslCounterBufferGOOGLE) {
  50. to_remove.push_back(&inst);
  51. }
  52. break;
  53. default:
  54. break;
  55. }
  56. }
  57. for (auto& inst : context()->module()->extensions()) {
  58. const std::string ext_name = inst.GetInOperand(0).AsString();
  59. if (ext_name == "SPV_GOOGLE_hlsl_functionality1") {
  60. to_remove.push_back(&inst);
  61. } else if (ext_name == "SPV_GOOGLE_user_type") {
  62. to_remove.push_back(&inst);
  63. } else if (!other_uses_for_decorate_string &&
  64. ext_name == "SPV_GOOGLE_decorate_string") {
  65. to_remove.push_back(&inst);
  66. } else if (ext_name == "SPV_KHR_non_semantic_info") {
  67. to_remove.push_back(&inst);
  68. }
  69. }
  70. // remove any extended inst imports that are non semantic
  71. std::unordered_set<uint32_t> non_semantic_sets;
  72. for (auto& inst : context()->module()->ext_inst_imports()) {
  73. assert(inst.opcode() == spv::Op::OpExtInstImport &&
  74. "Expecting an import of an extension's instruction set.");
  75. const std::string extension_name = inst.GetInOperand(0).AsString();
  76. if (spvtools::utils::starts_with(extension_name, "NonSemantic.")) {
  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 (spvIsExtendedInstruction(inst->opcode())) {
  87. if (non_semantic_sets.find(inst->GetSingleWordInOperand(0)) !=
  88. non_semantic_sets.end()) {
  89. to_remove.push_back(inst);
  90. }
  91. }
  92. },
  93. true);
  94. }
  95. for (auto* inst : to_remove) {
  96. modified = true;
  97. context()->KillInst(inst);
  98. }
  99. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  100. }
  101. } // namespace opt
  102. } // namespace spvtools