FeatureManager.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //===---- FeatureManager.cpp - SPIR-V Version/Extension Manager -*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //===----------------------------------------------------------------------===//
  8. #include "clang/SPIRV/FeatureManager.h"
  9. #include <sstream>
  10. #include "llvm/ADT/StringSwitch.h"
  11. namespace clang {
  12. namespace spirv {
  13. FeatureManager::FeatureManager(DiagnosticsEngine &de,
  14. const EmitSPIRVOptions &opts)
  15. : diags(de) {
  16. allowedExtensions.resize(static_cast<unsigned>(Extension::Unknown) + 1);
  17. if (opts.allowedExtensions.empty()) {
  18. // If no explicit extension control from command line, use the default mode:
  19. // allowing all extensions.
  20. allowAllKnownExtensions();
  21. } else {
  22. for (auto ext : opts.allowedExtensions)
  23. allowExtension(ext);
  24. }
  25. if (opts.targetEnv == "vulkan1.0")
  26. targetEnv = SPV_ENV_VULKAN_1_0;
  27. else if (opts.targetEnv == "vulkan1.1")
  28. targetEnv = SPV_ENV_VULKAN_1_1;
  29. else {
  30. emitError("unknown SPIR-V target environment '%0'", {}) << opts.targetEnv;
  31. emitNote("allowed options are:\n vulkan1.0\n vulkan1.1", {});
  32. }
  33. }
  34. bool FeatureManager::allowExtension(llvm::StringRef name) {
  35. // Special case: If we are asked to allow "SPV_KHR" extension, it indicates
  36. // that we should allow using *all* KHR extensions.
  37. if (getExtensionSymbol(name) == Extension::KHR) {
  38. bool result = true;
  39. for (uint32_t i = 0; i < static_cast<uint32_t>(Extension::Unknown); ++i) {
  40. llvm::StringRef extName(getExtensionName(static_cast<Extension>(i)));
  41. if (isKHRExtension(extName))
  42. result = result && allowExtension(extName);
  43. }
  44. return result;
  45. }
  46. const auto symbol = getExtensionSymbol(name);
  47. if (symbol == Extension::Unknown) {
  48. emitError("unknown SPIR-V extension '%0'", {}) << name;
  49. emitNote("known extensions are\n%0", {})
  50. << getKnownExtensions("\n* ", "* ");
  51. return false;
  52. }
  53. allowedExtensions.set(static_cast<unsigned>(symbol));
  54. return true;
  55. }
  56. void FeatureManager::allowAllKnownExtensions() { allowedExtensions.set(); }
  57. bool FeatureManager::requestExtension(Extension ext, llvm::StringRef target,
  58. SourceLocation srcLoc) {
  59. if (allowedExtensions.test(static_cast<unsigned>(ext)))
  60. return true;
  61. emitError("SPIR-V extension '%0' required for %1 but not permitted to use",
  62. srcLoc)
  63. << getExtensionName(ext) << target;
  64. return false;
  65. }
  66. bool FeatureManager::requestTargetEnv(spv_target_env requestedEnv,
  67. llvm::StringRef target,
  68. SourceLocation srcLoc) {
  69. if (targetEnv == SPV_ENV_VULKAN_1_0 && requestedEnv == SPV_ENV_VULKAN_1_1) {
  70. emitError("Vulkan 1.1 is required for %0 but not permitted to use", srcLoc)
  71. << target;
  72. emitNote("please specify your target environment via command line option "
  73. "-fspv-target-env=",
  74. {});
  75. return false;
  76. }
  77. return true;
  78. }
  79. Extension FeatureManager::getExtensionSymbol(llvm::StringRef name) {
  80. return llvm::StringSwitch<Extension>(name)
  81. .Case("KHR", Extension::KHR)
  82. .Case("SPV_KHR_16bit_storage", Extension::KHR_16bit_storage)
  83. .Case("SPV_KHR_device_group", Extension::KHR_device_group)
  84. .Case("SPV_KHR_multiview", Extension::KHR_multiview)
  85. .Case("SPV_KHR_shader_draw_parameters",
  86. Extension::KHR_shader_draw_parameters)
  87. .Case("SPV_EXT_fragment_fully_covered",
  88. Extension::EXT_fragment_fully_covered)
  89. .Case("SPV_EXT_shader_stencil_export",
  90. Extension::EXT_shader_stencil_export)
  91. .Case("SPV_AMD_gpu_shader_half_float",
  92. Extension::AMD_gpu_shader_half_float)
  93. .Case("SPV_AMD_shader_explicit_vertex_parameter",
  94. Extension::AMD_shader_explicit_vertex_parameter)
  95. .Case("SPV_GOOGLE_hlsl_functionality1",
  96. Extension::GOOGLE_hlsl_functionality1)
  97. .Default(Extension::Unknown);
  98. }
  99. const char *FeatureManager::getExtensionName(Extension symbol) {
  100. switch (symbol) {
  101. case Extension::KHR:
  102. return "KHR";
  103. case Extension::KHR_16bit_storage:
  104. return "SPV_KHR_16bit_storage";
  105. case Extension::KHR_device_group:
  106. return "SPV_KHR_device_group";
  107. case Extension::KHR_multiview:
  108. return "SPV_KHR_multiview";
  109. case Extension::KHR_shader_draw_parameters:
  110. return "SPV_KHR_shader_draw_parameters";
  111. case Extension::EXT_fragment_fully_covered:
  112. return "SPV_EXT_fragment_fully_covered";
  113. case Extension::EXT_shader_stencil_export:
  114. return "SPV_EXT_shader_stencil_export";
  115. case Extension::AMD_gpu_shader_half_float:
  116. return "SPV_AMD_gpu_shader_half_float";
  117. case Extension::AMD_shader_explicit_vertex_parameter:
  118. return "SPV_AMD_shader_explicit_vertex_parameter";
  119. case Extension::GOOGLE_hlsl_functionality1:
  120. return "SPV_GOOGLE_hlsl_functionality1";
  121. default:
  122. break;
  123. }
  124. return "<unknown extension>";
  125. }
  126. bool FeatureManager::isKHRExtension(llvm::StringRef name) {
  127. return name.startswith_lower("spv_khr_");
  128. }
  129. std::string FeatureManager::getKnownExtensions(const char *delimiter,
  130. const char *prefix,
  131. const char *postfix) {
  132. std::ostringstream oss;
  133. oss << prefix;
  134. const auto numExtensions = static_cast<uint32_t>(Extension::Unknown);
  135. for (uint32_t i = 0; i < numExtensions; ++i) {
  136. oss << getExtensionName(static_cast<Extension>(i));
  137. if (i + 1 < numExtensions)
  138. oss << delimiter;
  139. }
  140. oss << postfix;
  141. return oss.str();
  142. }
  143. bool FeatureManager::isExtensionRequiredForTargetEnv(Extension ext) {
  144. bool required = true;
  145. if (targetEnv == SPV_ENV_VULKAN_1_1) {
  146. // The following extensions are incorporated into Vulkan 1.1, and are
  147. // therefore not required to be emitted for that target environment.
  148. // TODO: Also add the following extensions if we start to support them.
  149. // * SPV_KHR_storage_buffer_storage_class
  150. // * SPV_KHR_variable_pointers
  151. switch (ext) {
  152. case Extension::KHR_16bit_storage:
  153. case Extension::KHR_device_group:
  154. case Extension::KHR_multiview:
  155. case Extension::KHR_shader_draw_parameters:
  156. required = false;
  157. }
  158. }
  159. return required;
  160. }
  161. } // end namespace spirv
  162. } // end namespace clang