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