2
0

strip_reflect_info_pass.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. }
  60. }
  61. for (auto* inst : to_remove) {
  62. modified = true;
  63. context()->KillInst(inst);
  64. }
  65. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  66. }
  67. } // namespace opt
  68. } // namespace spvtools