feature_manager.h 3.6 KB

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