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