feature_manager.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #ifndef SOURCE_OPT_FEATURE_MANAGER_H_
  15. #define SOURCE_OPT_FEATURE_MANAGER_H_
  16. #include "source/assembly_grammar.h"
  17. #include "source/extensions.h"
  18. #include "source/opt/module.h"
  19. namespace spvtools {
  20. namespace opt {
  21. // Tracks features enabled by a module. The IRContext has a FeatureManager.
  22. class FeatureManager {
  23. public:
  24. explicit FeatureManager(const AssemblyGrammar& grammar) : grammar_(grammar) {}
  25. // Returns true if |ext| is an enabled extension in the module.
  26. bool HasExtension(Extension ext) const { return extensions_.Contains(ext); }
  27. // Removes the given |extension| from the current FeatureManager.
  28. void RemoveExtension(Extension extension);
  29. // Returns true if |cap| is an enabled capability in the module.
  30. bool HasCapability(spv::Capability cap) const {
  31. return capabilities_.Contains(cap);
  32. }
  33. // Removes the given |capability| from the current FeatureManager.
  34. void RemoveCapability(spv::Capability capability);
  35. // Analyzes |module| and records enabled extensions and capabilities.
  36. void Analyze(Module* module);
  37. CapabilitySet* GetCapabilities() { return &capabilities_; }
  38. const CapabilitySet* GetCapabilities() const { return &capabilities_; }
  39. uint32_t GetExtInstImportId_GLSLstd450() const {
  40. return extinst_importid_GLSLstd450_;
  41. }
  42. uint32_t GetExtInstImportId_OpenCL100DebugInfo() const {
  43. return extinst_importid_OpenCL100DebugInfo_;
  44. }
  45. uint32_t GetExtInstImportId_Shader100DebugInfo() const {
  46. return extinst_importid_Shader100DebugInfo_;
  47. }
  48. friend bool operator==(const FeatureManager& a, const FeatureManager& b);
  49. friend bool operator!=(const FeatureManager& a, const FeatureManager& b) {
  50. return !(a == b);
  51. }
  52. // Adds the given |capability| and all implied capabilities into the current
  53. // FeatureManager.
  54. void AddCapability(spv::Capability capability);
  55. // Add the extension |ext| to the feature manager.
  56. void AddExtension(Instruction* ext);
  57. // Analyzes |module| and records imported external instruction sets.
  58. void AddExtInstImportIds(Module* module);
  59. private:
  60. // Analyzes |module| and records enabled extensions.
  61. void AddExtensions(Module* module);
  62. // Analyzes |module| and records enabled capabilities.
  63. void AddCapabilities(Module* module);
  64. // Auxiliary object for querying SPIR-V grammar facts.
  65. const AssemblyGrammar& grammar_;
  66. // The enabled extensions.
  67. ExtensionSet extensions_;
  68. // The enabled capabilities.
  69. CapabilitySet capabilities_;
  70. // Common external instruction import ids, cached for performance.
  71. uint32_t extinst_importid_GLSLstd450_ = 0;
  72. // Common OpenCL100DebugInfo external instruction import ids, cached
  73. // for performance.
  74. uint32_t extinst_importid_OpenCL100DebugInfo_ = 0;
  75. // Common NonSemanticShader100DebugInfo external instruction import ids,
  76. // cached for performance.
  77. uint32_t extinst_importid_Shader100DebugInfo_ = 0;
  78. };
  79. } // namespace opt
  80. } // namespace spvtools
  81. #endif // SOURCE_OPT_FEATURE_MANAGER_H_