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 #include "spirv-tools/linker.hpp"
16
17 #include <cassert>
18 #include <cstring>
19 #include <iostream>
20 #include <vector>
21
22 #include "source/spirv_target_env.h"
23 #include "source/table.h"
24 #include "spirv-tools/libspirv.hpp"
25 #include "tools/io.h"
26 #include "tools/util/flags.h"
27
28 namespace {
29
30 constexpr auto kDefaultEnvironment = "spv1.6";
31
print_usage(const char * program)32 void print_usage(const char* program) {
33 std::string target_env_list = spvTargetEnvList(16, 80);
34 // NOTE: Please maintain flags in lexicographical order.
35 printf(
36 R"(%s - Link SPIR-V binary files together.
37
38 USAGE: %s [options] [-o <output>] <input>...
39
40 The SPIR-V binaries are read from the different <input>(s).
41 The SPIR-V resulting linked binary module is written to the file "out.spv"
42 unless the -o option is used; if <output> is "-", it is written to the standard
43 output.
44
45 NOTE: The linker is a work in progress.
46
47 Options (in lexicographical order):
48 --allow-partial-linkage
49 Allow partial linkage by accepting imported symbols to be
50 unresolved.
51 --allow-pointer-mismatch
52 Allow pointer function parameters to mismatch the target link
53 target. This is useful to workaround lost correct parameter type
54 information due to LLVM's opaque pointers.
55 --create-library
56 Link the binaries into a library, keeping all exported symbols.
57 -h, --help
58 Print this help.
59 --target-env <env>
60 Set the environment used for interpreting the inputs. Without
61 this option the environment defaults to spv1.6. <env> must be
62 one of {%s}.
63 NOTE: The SPIR-V version used by the linked binary module
64 depends only on the version of the inputs, and is not affected
65 by this option.
66 --use-highest-version
67 Upgrade the output SPIR-V version to the highest of the input
68 files, instead of requiring all of them to have the same
69 version.
70 NOTE: If one of the older input files uses an instruction that
71 is deprecated in the highest SPIR-V version, the output will
72 be invalid.
73 --verify-ids
74 Verify that IDs in the resulting modules are truly unique.
75 --version
76 Display linker version information.
77 )",
78 program, program, target_env_list.c_str());
79 }
80
81 } // namespace
82
83 // clang-format off
84 FLAG_SHORT_bool( h, /* default_value= */ false, /* required= */ false);
85 FLAG_LONG_bool( help, /* default_value= */ false, /* required= */ false);
86 FLAG_LONG_bool( version, /* default_value= */ false, /* required= */ false);
87 FLAG_LONG_bool( verify_ids, /* default_value= */ false, /* required= */ false);
88 FLAG_LONG_bool( create_library, /* default_value= */ false, /* required= */ false);
89 FLAG_LONG_bool( allow_partial_linkage, /* default_value= */ false, /* required= */ false);
90 FLAG_LONG_bool( allow_pointer_mismatch, /* default_value= */ false, /* required= */ false);
91 FLAG_SHORT_string(o, /* default_value= */ "", /* required= */ false);
92 FLAG_LONG_string( target_env, /* default_value= */ kDefaultEnvironment, /* required= */ false);
93 FLAG_LONG_bool( use_highest_version, /* default_value= */ false, /* required= */ false);
94 // clang-format on
95
main(int,const char * argv[])96 int main(int, const char* argv[]) {
97 if (!flags::Parse(argv)) {
98 return 1;
99 }
100
101 if (flags::h.value() || flags::help.value()) {
102 print_usage(argv[0]);
103 return 0;
104 }
105
106 if (flags::version.value()) {
107 spv_target_env target_env;
108 bool success = spvParseTargetEnv(kDefaultEnvironment, &target_env);
109 assert(success && "Default environment should always parse.");
110 if (!success) {
111 fprintf(stderr,
112 "error: invalid default target environment. Please report this "
113 "issue.");
114 return 1;
115 }
116 printf("%s\n", spvSoftwareVersionDetailsString());
117 printf("Target: %s\n", spvTargetEnvDescription(target_env));
118 return 0;
119 }
120
121 spv_target_env target_env;
122 if (!spvParseTargetEnv(flags::target_env.value().c_str(), &target_env)) {
123 fprintf(stderr, "error: Unrecognized target env: %s\n",
124 flags::target_env.value().c_str());
125 return 1;
126 }
127
128 const std::string outFile =
129 flags::o.value().empty() ? "out.spv" : flags::o.value();
130 const std::vector<std::string>& inFiles = flags::positional_arguments;
131
132 spvtools::LinkerOptions options;
133 options.SetAllowPartialLinkage(flags::allow_partial_linkage.value());
134 options.SetAllowPtrTypeMismatch(flags::allow_pointer_mismatch.value());
135 options.SetCreateLibrary(flags::create_library.value());
136 options.SetVerifyIds(flags::verify_ids.value());
137 options.SetUseHighestVersion(flags::use_highest_version.value());
138
139 if (inFiles.empty()) {
140 fprintf(stderr, "error: No input file specified\n");
141 return 1;
142 }
143
144 std::vector<std::vector<uint32_t>> contents(inFiles.size());
145 for (size_t i = 0u; i < inFiles.size(); ++i) {
146 if (!ReadBinaryFile(inFiles[i].c_str(), &contents[i])) return 1;
147 }
148
149 const spvtools::MessageConsumer consumer = [](spv_message_level_t level,
150 const char*,
151 const spv_position_t& position,
152 const char* message) {
153 switch (level) {
154 case SPV_MSG_FATAL:
155 case SPV_MSG_INTERNAL_ERROR:
156 case SPV_MSG_ERROR:
157 std::cerr << "error: " << position.index << ": " << message
158 << std::endl;
159 break;
160 case SPV_MSG_WARNING:
161 std::cout << "warning: " << position.index << ": " << message
162 << std::endl;
163 break;
164 case SPV_MSG_INFO:
165 std::cout << "info: " << position.index << ": " << message << std::endl;
166 break;
167 default:
168 break;
169 }
170 };
171 spvtools::Context context(target_env);
172 context.SetMessageConsumer(consumer);
173
174 std::vector<uint32_t> linkingResult;
175 spv_result_t status = Link(context, contents, &linkingResult, options);
176 if (status != SPV_SUCCESS && status != SPV_WARNING) return 1;
177
178 if (!WriteFile<uint32_t>(outFile.c_str(), "wb", linkingResult.data(),
179 linkingResult.size()))
180 return 1;
181
182 return 0;
183 }
184