feature_manager.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (c) 2017 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/feature_manager.h"
  15. #include <queue>
  16. #include <stack>
  17. #include <string>
  18. #include "source/enum_string_mapping.h"
  19. namespace spvtools {
  20. namespace opt {
  21. void FeatureManager::Analyze(Module* module) {
  22. AddExtensions(module);
  23. AddCapabilities(module);
  24. AddExtInstImportIds(module);
  25. }
  26. void FeatureManager::AddExtensions(Module* module) {
  27. for (auto ext : module->extensions()) {
  28. AddExtension(&ext);
  29. }
  30. }
  31. void FeatureManager::AddExtension(Instruction* ext) {
  32. assert(ext->opcode() == SpvOpExtension &&
  33. "Expecting an extension instruction.");
  34. const std::string name =
  35. reinterpret_cast<const char*>(ext->GetInOperand(0u).words.data());
  36. Extension extension;
  37. if (GetExtensionFromString(name.c_str(), &extension)) {
  38. extensions_.Add(extension);
  39. }
  40. }
  41. void FeatureManager::AddCapability(SpvCapability cap) {
  42. if (capabilities_.Contains(cap)) return;
  43. capabilities_.Add(cap);
  44. spv_operand_desc desc = {};
  45. if (SPV_SUCCESS ==
  46. grammar_.lookupOperand(SPV_OPERAND_TYPE_CAPABILITY, cap, &desc)) {
  47. CapabilitySet(desc->numCapabilities, desc->capabilities)
  48. .ForEach([this](SpvCapability c) { AddCapability(c); });
  49. }
  50. }
  51. void FeatureManager::AddCapabilities(Module* module) {
  52. for (Instruction& inst : module->capabilities()) {
  53. AddCapability(static_cast<SpvCapability>(inst.GetSingleWordInOperand(0)));
  54. }
  55. }
  56. void FeatureManager::AddExtInstImportIds(Module* module) {
  57. extinst_importid_GLSLstd450_ = module->GetExtInstImportId("GLSL.std.450");
  58. }
  59. bool operator==(const FeatureManager& a, const FeatureManager& b) {
  60. // We check that the addresses of the grammars are the same because they
  61. // are large objects, and this is faster. It can be changed if needed as a
  62. // later time.
  63. if (&a.grammar_ != &b.grammar_) {
  64. return false;
  65. }
  66. if (a.capabilities_ != b.capabilities_) {
  67. return false;
  68. }
  69. if (a.extensions_ != b.extensions_) {
  70. return false;
  71. }
  72. if (a.extinst_importid_GLSLstd450_ != b.extinst_importid_GLSLstd450_) {
  73. return false;
  74. }
  75. return true;
  76. }
  77. } // namespace opt
  78. } // namespace spvtools