Debian Codegraph @

tools/link/linker.cpp

raw
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
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 --fnvar-targets
76 CSV file specifying OpSpecConditionalTarget parameters for each
77 linked module.
78 --fnvar-architectures
79 CSV file specifying OpSpecConditionalArchitecture parameters for
80 each linked module.
81 --fnvar-capabilities
82 For each function variant, include its required capabilities
83 under OpSpecConditionalCapabilitiesINTEL.
84 --version
85 Display linker version information.
86 )",
87 program, program, target_env_list.c_str());
88 }
89
90 } // namespace
91
92 // clang-format off
93 FLAG_SHORT_bool( h, /* default_value= */ false, /* required= */ false);
94 FLAG_LONG_bool( help, /* default_value= */ false, /* required= */ false);
95 FLAG_LONG_bool( version, /* default_value= */ false, /* required= */ false);
96 FLAG_LONG_bool( verify_ids, /* default_value= */ false, /* required= */ false);
97 FLAG_LONG_bool( create_library, /* default_value= */ false, /* required= */ false);
98 FLAG_LONG_bool( allow_partial_linkage, /* default_value= */ false, /* required= */ false);
99 FLAG_LONG_bool( allow_pointer_mismatch, /* default_value= */ false, /* required= */ false);
100 FLAG_SHORT_string(o, /* default_value= */ "", /* required= */ false);
101 FLAG_LONG_string( target_env, /* default_value= */ kDefaultEnvironment, /* required= */ false);
102 FLAG_LONG_string( fnvar_targets, /* default_value= */ "", /* required= */ false);
103 FLAG_LONG_string( fnvar_architectures, /* default_value= */ "", /* required= */ false);
104 FLAG_LONG_bool( fnvar_capabilities, /* default_value= */ false, /* required= */ false);
105 FLAG_LONG_bool( use_highest_version, /* default_value= */ false, /* required= */ false);
106 // clang-format on
107
108 int main(int, const char* argv[]) {
109 if (!flags::Parse(argv)) {
110 return 1;
111 }
112
113 if (flags::h.value() || flags::help.value()) {
114 print_usage(argv[0]);
115 return 0;
116 }
117
118 if (flags::version.value()) {
119 spv_target_env target_env;
120 bool success = spvParseTargetEnv(kDefaultEnvironment, &target_env);
121 assert(success && "Default environment should always parse.");
122 if (!success) {
123 fprintf(stderr,
124 "error: invalid default target environment. Please report this "
125 "issue.");
126 return 1;
127 }
128 printf("%s\n", spvSoftwareVersionDetailsString());
129 printf("Target: %s\n", spvTargetEnvDescription(target_env));
130 return 0;
131 }
132
133 spv_target_env target_env;
134 if (!spvParseTargetEnv(flags::target_env.value().c_str(), &target_env)) {
135 fprintf(stderr, "error: Unrecognized target env: %s\n",
136 flags::target_env.value().c_str());
137 return 1;
138 }
139
140 const std::string outFile =
141 flags::o.value().empty() ? "out.spv" : flags::o.value();
142 const std::vector<std::string>& inFiles = flags::positional_arguments;
143
144 spvtools::LinkerOptions options;
145 options.SetAllowPartialLinkage(flags::allow_partial_linkage.value());
146 options.SetAllowPtrTypeMismatch(flags::allow_pointer_mismatch.value());
147 options.SetCreateLibrary(flags::create_library.value());
148 options.SetVerifyIds(flags::verify_ids.value());
149 options.SetUseHighestVersion(flags::use_highest_version.value());
150
151 if (inFiles.empty()) {
152 fprintf(stderr, "error: No input file specified\n");
153 return 1;
154 }
155 options.SetInFiles(inFiles);
156
157 if (!flags::fnvar_targets.value().empty()) {
158 std::vector<char> fn_variants_csv_chars;
159 const bool ret = ReadTextFile(flags::fnvar_targets.value().c_str(),
160 &fn_variants_csv_chars);
161 if (!ret) {
162 return 1;
163 }
164 const std::string csv_text(fn_variants_csv_chars.begin(),
165 fn_variants_csv_chars.end());
166 options.SetFnVarTargetsCsv(csv_text);
167 }
168
169 if (!flags::fnvar_architectures.value().empty()) {
170 std::vector<char> fn_variants_csv_chars;
171 const bool ret = ReadTextFile(flags::fnvar_architectures.value().c_str(),
172 &fn_variants_csv_chars);
173 if (!ret) {
174 return 1;
175 }
176 const std::string csv_text(fn_variants_csv_chars.begin(),
177 fn_variants_csv_chars.end());
178 options.SetFnVarArchitecturesCsv(csv_text);
179 }
180
181 options.SetHasFnVarCapabilities(flags::fnvar_capabilities.value());
182
183 std::vector<std::vector<uint32_t>> contents(inFiles.size());
184 for (size_t i = 0u; i < inFiles.size(); ++i) {
185 if (!ReadBinaryFile(inFiles[i].c_str(), &contents[i])) return 1;
186 }
187
188 const spvtools::MessageConsumer consumer = [](spv_message_level_t level,
189 const char*,
190 const spv_position_t& position,
191 const char* message) {
192 switch (level) {
193 case SPV_MSG_FATAL:
194 case SPV_MSG_INTERNAL_ERROR:
195 case SPV_MSG_ERROR:
196 std::cerr << "error: " << position.index << ": " << message
197 << std::endl;
198 break;
199 case SPV_MSG_WARNING:
200 std::cout << "warning: " << position.index << ": " << message
201 << std::endl;
202 break;
203 case SPV_MSG_INFO:
204 std::cout << "info: " << position.index << ": " << message << std::endl;
205 break;
206 default:
207 break;
208 }
209 };
210 spvtools::Context context(target_env);
211 context.SetMessageConsumer(consumer);
212
213 std::vector<uint32_t> linkingResult;
214 spv_result_t status = Link(context, contents, &linkingResult, options);
215 if (status != SPV_SUCCESS && status != SPV_WARNING) return 1;
216
217 if (!WriteFile<uint32_t>(outFile.c_str(), "wb", linkingResult.data(),
218 linkingResult.size()))
219 return 1;
220
221 return 0;
222 }