1 // Copyright (c) 2017 Pierre Moreau
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 #ifndef INCLUDE_SPIRV_TOOLS_LINKER_HPP_
16 #define INCLUDE_SPIRV_TOOLS_LINKER_HPP_
17 
18 #include <cstdint>
19 
20 #include <memory>
21 #include <vector>
22 
23 #include "libspirv.hpp"
24 
25 namespace spvtools {
26 
27 class LinkerOptions {
28  public:
LinkerOptions()29   LinkerOptions()
30       : create_library_(false),
31         verify_ids_(false),
32         allow_partial_linkage_(false) {}
33 
34   // Returns whether a library or an executable should be produced by the
35   // linking phase.
36   //
37   // All exported symbols are kept when creating a library, whereas they will
38   // be removed when creating an executable.
39   // The returned value will be true if creating a library, and false if
40   // creating an executable.
GetCreateLibrary() const41   bool GetCreateLibrary() const { return create_library_; }
42 
43   // Sets whether a library or an executable should be produced.
SetCreateLibrary(bool create_library)44   void SetCreateLibrary(bool create_library) {
45     create_library_ = create_library;
46   }
47 
48   // Returns whether to verify the uniqueness of the unique ids in the merged
49   // context.
GetVerifyIds() const50   bool GetVerifyIds() const { return verify_ids_; }
51 
52   // Sets whether to verify the uniqueness of the unique ids in the merged
53   // context.
SetVerifyIds(bool verify_ids)54   void SetVerifyIds(bool verify_ids) { verify_ids_ = verify_ids; }
55 
56   // Returns whether to allow for imported symbols to have no corresponding
57   // exported symbols
GetAllowPartialLinkage() const58   bool GetAllowPartialLinkage() const { return allow_partial_linkage_; }
59 
60   // Sets whether to allow for imported symbols to have no corresponding
61   // exported symbols
SetAllowPartialLinkage(bool allow_partial_linkage)62   void SetAllowPartialLinkage(bool allow_partial_linkage) {
63     allow_partial_linkage_ = allow_partial_linkage;
64   }
65 
66  private:
67   bool create_library_;
68   bool verify_ids_;
69   bool allow_partial_linkage_;
70 };
71 
72 // Links one or more SPIR-V modules into a new SPIR-V module. That is, combine
73 // several SPIR-V modules into one, resolving link dependencies between them.
74 //
75 // At least one binary has to be provided in |binaries|. Those binaries do not
76 // have to be valid, but they should be at least parseable.
77 // The functions can fail due to the following:
78 // * The given context was not initialised using `spvContextCreate()`;
79 // * No input modules were given;
80 // * One or more of those modules were not parseable;
81 // * The input modules used different addressing or memory models;
82 // * The ID or global variable number limit were exceeded;
83 // * Some entry points were defined multiple times;
84 // * Some imported symbols did not have an exported counterpart;
85 // * Possibly other reasons.
86 spv_result_t Link(const Context& context,
87                   const std::vector<std::vector<uint32_t>>& binaries,
88                   std::vector<uint32_t>* linked_binary,
89                   const LinkerOptions& options = LinkerOptions());
90 spv_result_t Link(const Context& context, const uint32_t* const* binaries,
91                   const size_t* binary_sizes, size_t num_binaries,
92                   std::vector<uint32_t>* linked_binary,
93                   const LinkerOptions& options = LinkerOptions());
94 
95 }  // namespace spvtools
96 
97 #endif  // INCLUDE_SPIRV_TOOLS_LINKER_HPP_
98