libspirv.cpp 4.0 KB

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