build_module.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "source/opt/build_module.h"
  15. #include <utility>
  16. #include <vector>
  17. #include "source/opt/ir_context.h"
  18. #include "source/opt/ir_loader.h"
  19. #include "source/table.h"
  20. #include "source/util/make_unique.h"
  21. namespace spvtools {
  22. namespace {
  23. // Sets the module header for IrLoader. Meets the interface requirement of
  24. // spvBinaryParse().
  25. spv_result_t SetSpvHeader(void* builder, spv_endianness_t, uint32_t magic,
  26. uint32_t version, uint32_t generator,
  27. uint32_t id_bound, uint32_t reserved) {
  28. reinterpret_cast<opt::IrLoader*>(builder)->SetModuleHeader(
  29. magic, version, generator, id_bound, reserved);
  30. return SPV_SUCCESS;
  31. }
  32. // Processes a parsed instruction for IrLoader. Meets the interface requirement
  33. // of spvBinaryParse().
  34. spv_result_t SetSpvInst(void* builder, const spv_parsed_instruction_t* inst) {
  35. if (reinterpret_cast<opt::IrLoader*>(builder)->AddInstruction(inst)) {
  36. return SPV_SUCCESS;
  37. }
  38. return SPV_ERROR_INVALID_BINARY;
  39. }
  40. } // namespace
  41. std::unique_ptr<opt::IRContext> BuildModule(spv_target_env env,
  42. MessageConsumer consumer,
  43. const uint32_t* binary,
  44. const size_t size) {
  45. return BuildModule(env, consumer, binary, size, true);
  46. }
  47. std::unique_ptr<opt::IRContext> BuildModule(spv_target_env env,
  48. MessageConsumer consumer,
  49. const uint32_t* binary,
  50. const size_t size,
  51. bool extra_line_tracking) {
  52. auto context = spvContextCreate(env);
  53. SetContextMessageConsumer(context, consumer);
  54. auto irContext = MakeUnique<opt::IRContext>(env, consumer);
  55. opt::IrLoader loader(consumer, irContext->module());
  56. loader.SetExtraLineTracking(extra_line_tracking);
  57. spv_result_t status = spvBinaryParse(context, &loader, binary, size,
  58. SetSpvHeader, SetSpvInst, nullptr);
  59. loader.EndModule();
  60. spvContextDestroy(context);
  61. return status == SPV_SUCCESS ? std::move(irContext) : nullptr;
  62. }
  63. std::unique_ptr<opt::IRContext> BuildModule(spv_target_env env,
  64. MessageConsumer consumer,
  65. const std::string& text,
  66. uint32_t assemble_options) {
  67. SpirvTools t(env);
  68. t.SetMessageConsumer(consumer);
  69. std::vector<uint32_t> binary;
  70. if (!t.Assemble(text, &binary, assemble_options)) return nullptr;
  71. return BuildModule(env, consumer, binary.data(), binary.size());
  72. }
  73. } // namespace spvtools