libspirv.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <iostream>
  16. #include <string>
  17. #include <utility>
  18. #include <vector>
  19. #include "source/table.h"
  20. namespace spvtools {
  21. Context::Context(spv_target_env env) : context_(spvContextCreate(env)) {}
  22. Context::Context(Context&& other) : context_(other.context_) {
  23. other.context_ = nullptr;
  24. }
  25. Context& Context::operator=(Context&& other) {
  26. spvContextDestroy(context_);
  27. context_ = other.context_;
  28. other.context_ = nullptr;
  29. return *this;
  30. }
  31. Context::~Context() { spvContextDestroy(context_); }
  32. void Context::SetMessageConsumer(MessageConsumer consumer) {
  33. SetContextMessageConsumer(context_, std::move(consumer));
  34. }
  35. spv_context& Context::CContext() { return context_; }
  36. const spv_context& Context::CContext() const { return context_; }
  37. // Structs for holding the data members for SpvTools.
  38. struct SpirvTools::Impl {
  39. explicit Impl(spv_target_env env) : context(spvContextCreate(env)) {
  40. // The default consumer in spv_context_t is a null consumer, which provides
  41. // equivalent functionality (from the user's perspective) as a real consumer
  42. // does nothing.
  43. }
  44. ~Impl() { spvContextDestroy(context); }
  45. spv_context context; // C interface context object.
  46. };
  47. SpirvTools::SpirvTools(spv_target_env env) : impl_(new Impl(env)) {}
  48. SpirvTools::~SpirvTools() {}
  49. void SpirvTools::SetMessageConsumer(MessageConsumer consumer) {
  50. SetContextMessageConsumer(impl_->context, std::move(consumer));
  51. }
  52. bool SpirvTools::Assemble(const std::string& text,
  53. std::vector<uint32_t>* binary,
  54. uint32_t options) const {
  55. return Assemble(text.data(), text.size(), binary, options);
  56. }
  57. bool SpirvTools::Assemble(const char* text, const size_t text_size,
  58. std::vector<uint32_t>* binary,
  59. uint32_t options) const {
  60. spv_binary spvbinary = nullptr;
  61. spv_result_t status = spvTextToBinaryWithOptions(
  62. impl_->context, text, text_size, options, &spvbinary, nullptr);
  63. if (status == SPV_SUCCESS) {
  64. binary->assign(spvbinary->code, spvbinary->code + spvbinary->wordCount);
  65. }
  66. spvBinaryDestroy(spvbinary);
  67. return status == SPV_SUCCESS;
  68. }
  69. bool SpirvTools::Disassemble(const std::vector<uint32_t>& binary,
  70. std::string* text, uint32_t options) const {
  71. return Disassemble(binary.data(), binary.size(), text, options);
  72. }
  73. bool SpirvTools::Disassemble(const uint32_t* binary, const size_t binary_size,
  74. std::string* text, uint32_t options) const {
  75. spv_text spvtext = nullptr;
  76. spv_result_t status = spvBinaryToText(impl_->context, binary, binary_size,
  77. options, &spvtext, nullptr);
  78. if (status == SPV_SUCCESS) {
  79. text->assign(spvtext->str, spvtext->str + spvtext->length);
  80. }
  81. spvTextDestroy(spvtext);
  82. return status == SPV_SUCCESS;
  83. }
  84. bool SpirvTools::Validate(const std::vector<uint32_t>& binary) const {
  85. return Validate(binary.data(), binary.size());
  86. }
  87. bool SpirvTools::Validate(const uint32_t* binary,
  88. const size_t binary_size) const {
  89. return spvValidateBinary(impl_->context, binary, binary_size, nullptr) ==
  90. SPV_SUCCESS;
  91. }
  92. bool SpirvTools::Validate(const uint32_t* binary, const size_t binary_size,
  93. spv_validator_options options) const {
  94. spv_const_binary_t the_binary{binary, binary_size};
  95. spv_diagnostic diagnostic = nullptr;
  96. bool valid = spvValidateWithOptions(impl_->context, options, &the_binary,
  97. &diagnostic) == SPV_SUCCESS;
  98. if (!valid && impl_->context->consumer) {
  99. impl_->context->consumer.operator()(
  100. SPV_MSG_ERROR, nullptr, diagnostic->position, diagnostic->error);
  101. }
  102. spvDiagnosticDestroy(diagnostic);
  103. return valid;
  104. }
  105. bool SpirvTools::IsValid() const { return impl_->context != nullptr; }
  106. } // namespace spvtools