libspirv.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. text->assign(spvtext->str, spvtext->str + spvtext->length);
  83. }
  84. spvTextDestroy(spvtext);
  85. return status == SPV_SUCCESS;
  86. }
  87. bool SpirvTools::Validate(const std::vector<uint32_t>& binary) const {
  88. return Validate(binary.data(), binary.size());
  89. }
  90. bool SpirvTools::Validate(const uint32_t* binary,
  91. const size_t binary_size) const {
  92. return spvValidateBinary(impl_->context, binary, binary_size, nullptr) ==
  93. SPV_SUCCESS;
  94. }
  95. bool SpirvTools::Validate(const uint32_t* binary, const size_t binary_size,
  96. spv_validator_options options) const {
  97. spv_const_binary_t the_binary{binary, binary_size};
  98. spv_diagnostic diagnostic = nullptr;
  99. bool valid = spvValidateWithOptions(impl_->context, options, &the_binary,
  100. &diagnostic) == SPV_SUCCESS;
  101. if (!valid && impl_->context->consumer) {
  102. impl_->context->consumer.operator()(
  103. SPV_MSG_ERROR, nullptr, diagnostic->position, diagnostic->error);
  104. }
  105. spvDiagnosticDestroy(diagnostic);
  106. return valid;
  107. }
  108. bool SpirvTools::IsValid() const { return impl_->context != nullptr; }
  109. } // namespace spvtools