libspirv.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright (c) 2016 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 "spirv-tools/libspirv.hpp"
  15. #include <cassert>
  16. #include <iostream>
  17. #include <string>
  18. #include <utility>
  19. #include <vector>
  20. #include "source/table.h"
  21. namespace spvtools {
  22. Context::Context(spv_target_env env) : context_(spvContextCreate(env)) {}
  23. Context::Context(Context&& other) : context_(other.context_) {
  24. other.context_ = nullptr;
  25. }
  26. Context& Context::operator=(Context&& other) {
  27. spvContextDestroy(context_);
  28. context_ = other.context_;
  29. other.context_ = nullptr;
  30. return *this;
  31. }
  32. Context::~Context() { spvContextDestroy(context_); }
  33. void Context::SetMessageConsumer(MessageConsumer consumer) {
  34. SetContextMessageConsumer(context_, std::move(consumer));
  35. }
  36. spv_context& Context::CContext() { return context_; }
  37. const spv_context& Context::CContext() const { return context_; }
  38. // Structs for holding the data members for SpvTools.
  39. struct SpirvTools::Impl {
  40. explicit Impl(spv_target_env env) : context(spvContextCreate(env)) {
  41. // The default consumer in spv_context_t is a null consumer, which provides
  42. // equivalent functionality (from the user's perspective) as a real consumer
  43. // does nothing.
  44. }
  45. ~Impl() { spvContextDestroy(context); }
  46. spv_context context; // C interface context object.
  47. };
  48. SpirvTools::SpirvTools(spv_target_env env) : impl_(new Impl(env)) {
  49. assert(env != SPV_ENV_WEBGPU_0);
  50. }
  51. SpirvTools::~SpirvTools() {}
  52. void SpirvTools::SetMessageConsumer(MessageConsumer consumer) {
  53. SetContextMessageConsumer(impl_->context, std::move(consumer));
  54. }
  55. bool SpirvTools::Assemble(const std::string& text,
  56. std::vector<uint32_t>* binary,
  57. uint32_t options) const {
  58. return Assemble(text.data(), text.size(), binary, options);
  59. }
  60. bool SpirvTools::Assemble(const char* text, const size_t text_size,
  61. std::vector<uint32_t>* binary,
  62. uint32_t options) const {
  63. spv_binary spvbinary = nullptr;
  64. spv_result_t status = spvTextToBinaryWithOptions(
  65. impl_->context, text, text_size, options, &spvbinary, nullptr);
  66. if (status == SPV_SUCCESS) {
  67. binary->assign(spvbinary->code, spvbinary->code + spvbinary->wordCount);
  68. }
  69. spvBinaryDestroy(spvbinary);
  70. return status == SPV_SUCCESS;
  71. }
  72. bool SpirvTools::Disassemble(const std::vector<uint32_t>& binary,
  73. std::string* text, uint32_t options) const {
  74. return Disassemble(binary.data(), binary.size(), text, options);
  75. }
  76. bool SpirvTools::Disassemble(const uint32_t* binary, const size_t binary_size,
  77. std::string* text, uint32_t options) const {
  78. spv_text spvtext = nullptr;
  79. spv_result_t status = spvBinaryToText(impl_->context, binary, binary_size,
  80. options, &spvtext, nullptr);
  81. if (status == SPV_SUCCESS &&
  82. (options & SPV_BINARY_TO_TEXT_OPTION_PRINT) == 0) {
  83. assert(spvtext);
  84. text->assign(spvtext->str, spvtext->str + spvtext->length);
  85. }
  86. spvTextDestroy(spvtext);
  87. return status == SPV_SUCCESS;
  88. }
  89. struct CxxParserContext {
  90. const HeaderParser& header_parser;
  91. const InstructionParser& instruction_parser;
  92. };
  93. bool SpirvTools::Parse(const std::vector<uint32_t>& binary,
  94. const HeaderParser& header_parser,
  95. const InstructionParser& instruction_parser,
  96. spv_diagnostic* diagnostic) {
  97. CxxParserContext parser_context = {header_parser, instruction_parser};
  98. spv_parsed_header_fn_t header_fn_wrapper =
  99. [](void* user_data, spv_endianness_t endianness, uint32_t magic,
  100. uint32_t version, uint32_t generator, uint32_t id_bound,
  101. uint32_t reserved) {
  102. CxxParserContext* ctx = reinterpret_cast<CxxParserContext*>(user_data);
  103. spv_parsed_header_t header = {magic, version, generator, id_bound,
  104. reserved};
  105. return ctx->header_parser(endianness, header);
  106. };
  107. spv_parsed_instruction_fn_t instruction_fn_wrapper =
  108. [](void* user_data, const spv_parsed_instruction_t* instruction) {
  109. CxxParserContext* ctx = reinterpret_cast<CxxParserContext*>(user_data);
  110. return ctx->instruction_parser(*instruction);
  111. };
  112. spv_result_t status = spvBinaryParse(
  113. impl_->context, &parser_context, binary.data(), binary.size(),
  114. header_fn_wrapper, instruction_fn_wrapper, diagnostic);
  115. return status == SPV_SUCCESS;
  116. }
  117. bool SpirvTools::Validate(const std::vector<uint32_t>& binary) const {
  118. return Validate(binary.data(), binary.size());
  119. }
  120. bool SpirvTools::Validate(const uint32_t* binary,
  121. const size_t binary_size) const {
  122. return spvValidateBinary(impl_->context, binary, binary_size, nullptr) ==
  123. SPV_SUCCESS;
  124. }
  125. bool SpirvTools::Validate(const uint32_t* binary, const size_t binary_size,
  126. spv_validator_options options) const {
  127. spv_const_binary_t the_binary{binary, binary_size};
  128. spv_diagnostic diagnostic = nullptr;
  129. bool valid = spvValidateWithOptions(impl_->context, options, &the_binary,
  130. &diagnostic) == SPV_SUCCESS;
  131. if (!valid && impl_->context->consumer) {
  132. impl_->context->consumer.operator()(
  133. SPV_MSG_ERROR, nullptr, diagnostic->position, diagnostic->error);
  134. }
  135. spvDiagnosticDestroy(diagnostic);
  136. return valid;
  137. }
  138. bool SpirvTools::IsValid() const { return impl_->context != nullptr; }
  139. } // namespace spvtools