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