1 //
2 // Copyright (C) 2014-2016 LunarG, Inc.
3 // Copyright (C) 2018-2020 Google, Inc.
4 //
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 // Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //
14 // Redistributions in binary form must reproduce the above
15 // copyright notice, this list of conditions and the following
16 // disclaimer in the documentation and/or other materials provided
17 // with the distribution.
18 //
19 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20 // contributors may be used to endorse or promote products derived
21 // from this software without specific prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 // POSSIBILITY OF SUCH DAMAGE.
35
36 //
37 // Call into SPIRV-Tools to disassemble, validate, and optimize.
38 //
39
40 #if ENABLE_OPT
41
42 #include <cstdio>
43 #include <iostream>
44
45 #include "SpvTools.h"
46 #include "spirv-tools/optimizer.hpp"
47
48 namespace glslang {
49
50 // Translate glslang's view of target versioning to what SPIRV-Tools uses.
MapToSpirvToolsEnv(const SpvVersion & spvVersion,spv::SpvBuildLogger * logger)51 spv_target_env MapToSpirvToolsEnv(const SpvVersion& spvVersion, spv::SpvBuildLogger* logger)
52 {
53 switch (spvVersion.vulkan) {
54 case glslang::EShTargetVulkan_1_0:
55 return spv_target_env::SPV_ENV_VULKAN_1_0;
56 case glslang::EShTargetVulkan_1_1:
57 switch (spvVersion.spv) {
58 case EShTargetSpv_1_0:
59 case EShTargetSpv_1_1:
60 case EShTargetSpv_1_2:
61 case EShTargetSpv_1_3:
62 return spv_target_env::SPV_ENV_VULKAN_1_1;
63 case EShTargetSpv_1_4:
64 return spv_target_env::SPV_ENV_VULKAN_1_1_SPIRV_1_4;
65 default:
66 logger->missingFunctionality("Target version for SPIRV-Tools validator");
67 return spv_target_env::SPV_ENV_VULKAN_1_1;
68 }
69 case glslang::EShTargetVulkan_1_2:
70 return spv_target_env::SPV_ENV_VULKAN_1_2;
71 case glslang::EShTargetVulkan_1_3:
72 return spv_target_env::SPV_ENV_VULKAN_1_3;
73 default:
74 break;
75 }
76
77 if (spvVersion.openGl > 0)
78 return spv_target_env::SPV_ENV_OPENGL_4_5;
79
80 logger->missingFunctionality("Target version for SPIRV-Tools validator");
81 return spv_target_env::SPV_ENV_UNIVERSAL_1_0;
82 }
83
84 // Callback passed to spvtools::Optimizer::SetMessageConsumer
OptimizerMesssageConsumer(spv_message_level_t level,const char * source,const spv_position_t & position,const char * message)85 void OptimizerMesssageConsumer(spv_message_level_t level, const char *source,
86 const spv_position_t &position, const char *message)
87 {
88 auto &out = std::cerr;
89 switch (level)
90 {
91 case SPV_MSG_FATAL:
92 case SPV_MSG_INTERNAL_ERROR:
93 case SPV_MSG_ERROR:
94 out << "error: ";
95 break;
96 case SPV_MSG_WARNING:
97 out << "warning: ";
98 break;
99 case SPV_MSG_INFO:
100 case SPV_MSG_DEBUG:
101 out << "info: ";
102 break;
103 default:
104 break;
105 }
106 if (source)
107 {
108 out << source << ":";
109 }
110 out << position.line << ":" << position.column << ":" << position.index << ":";
111 if (message)
112 {
113 out << " " << message;
114 }
115 out << std::endl;
116 }
117
118 // Use the SPIRV-Tools disassembler to print SPIR-V using a SPV_ENV_UNIVERSAL_1_3 environment.
SpirvToolsDisassemble(std::ostream & out,const std::vector<unsigned int> & spirv)119 void SpirvToolsDisassemble(std::ostream& out, const std::vector<unsigned int>& spirv)
120 {
121 SpirvToolsDisassemble(out, spirv, spv_target_env::SPV_ENV_UNIVERSAL_1_3);
122 }
123
124 // Use the SPIRV-Tools disassembler to print SPIR-V with a provided SPIR-V environment.
SpirvToolsDisassemble(std::ostream & out,const std::vector<unsigned int> & spirv,spv_target_env requested_context)125 void SpirvToolsDisassemble(std::ostream& out, const std::vector<unsigned int>& spirv,
126 spv_target_env requested_context)
127 {
128 // disassemble
129 spv_context context = spvContextCreate(requested_context);
130 spv_text text;
131 spv_diagnostic diagnostic = nullptr;
132 spvBinaryToText(context, spirv.data(), spirv.size(),
133 SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES | SPV_BINARY_TO_TEXT_OPTION_INDENT,
134 &text, &diagnostic);
135
136 // dump
137 if (diagnostic == nullptr)
138 out << text->str;
139 else
140 spvDiagnosticPrint(diagnostic);
141
142 // teardown
143 spvDiagnosticDestroy(diagnostic);
144 spvContextDestroy(context);
145 }
146
147 // Apply the SPIRV-Tools validator to generated SPIR-V.
SpirvToolsValidate(const glslang::TIntermediate & intermediate,std::vector<unsigned int> & spirv,spv::SpvBuildLogger * logger,bool prelegalization)148 void SpirvToolsValidate(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv,
149 spv::SpvBuildLogger* logger, bool prelegalization)
150 {
151 // validate
152 spv_context context = spvContextCreate(MapToSpirvToolsEnv(intermediate.getSpv(), logger));
153 spv_const_binary_t binary = { spirv.data(), spirv.size() };
154 spv_diagnostic diagnostic = nullptr;
155 spv_validator_options options = spvValidatorOptionsCreate();
156 spvValidatorOptionsSetRelaxBlockLayout(options, intermediate.usingHlslOffsets());
157 spvValidatorOptionsSetBeforeHlslLegalization(options, prelegalization);
158 spvValidatorOptionsSetScalarBlockLayout(options, intermediate.usingScalarBlockLayout());
159 spvValidatorOptionsSetWorkgroupScalarBlockLayout(options, intermediate.usingScalarBlockLayout());
160 spvValidateWithOptions(context, options, &binary, &diagnostic);
161
162 // report
163 if (diagnostic != nullptr) {
164 logger->error("SPIRV-Tools Validation Errors");
165 logger->error(diagnostic->error);
166 }
167
168 // tear down
169 spvValidatorOptionsDestroy(options);
170 spvDiagnosticDestroy(diagnostic);
171 spvContextDestroy(context);
172 }
173
174 // Apply the SPIRV-Tools optimizer to generated SPIR-V. HLSL SPIR-V is legalized in the process.
SpirvToolsTransform(const glslang::TIntermediate & intermediate,std::vector<unsigned int> & spirv,spv::SpvBuildLogger * logger,const SpvOptions * options)175 void SpirvToolsTransform(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv,
176 spv::SpvBuildLogger* logger, const SpvOptions* options)
177 {
178 spv_target_env target_env = MapToSpirvToolsEnv(intermediate.getSpv(), logger);
179
180 spvtools::Optimizer optimizer(target_env);
181 optimizer.SetMessageConsumer(OptimizerMesssageConsumer);
182
183 // If debug (specifically source line info) is being generated, propagate
184 // line information into all SPIR-V instructions. This avoids loss of
185 // information when instructions are deleted or moved. Later, remove
186 // redundant information to minimize final SPRIR-V size.
187 if (options->stripDebugInfo) {
188 optimizer.RegisterPass(spvtools::CreateStripDebugInfoPass());
189 }
190 optimizer.RegisterPass(spvtools::CreateWrapOpKillPass());
191 optimizer.RegisterPass(spvtools::CreateDeadBranchElimPass());
192 optimizer.RegisterPass(spvtools::CreateMergeReturnPass());
193 optimizer.RegisterPass(spvtools::CreateInlineExhaustivePass());
194 optimizer.RegisterPass(spvtools::CreateEliminateDeadFunctionsPass());
195 optimizer.RegisterPass(spvtools::CreateScalarReplacementPass());
196 optimizer.RegisterPass(spvtools::CreateLocalAccessChainConvertPass());
197 optimizer.RegisterPass(spvtools::CreateLocalSingleBlockLoadStoreElimPass());
198 optimizer.RegisterPass(spvtools::CreateLocalSingleStoreElimPass());
199 optimizer.RegisterPass(spvtools::CreateSimplificationPass());
200 optimizer.RegisterPass(spvtools::CreateAggressiveDCEPass());
201 optimizer.RegisterPass(spvtools::CreateVectorDCEPass());
202 optimizer.RegisterPass(spvtools::CreateDeadInsertElimPass());
203 optimizer.RegisterPass(spvtools::CreateAggressiveDCEPass());
204 optimizer.RegisterPass(spvtools::CreateDeadBranchElimPass());
205 optimizer.RegisterPass(spvtools::CreateBlockMergePass());
206 optimizer.RegisterPass(spvtools::CreateLocalMultiStoreElimPass());
207 optimizer.RegisterPass(spvtools::CreateIfConversionPass());
208 optimizer.RegisterPass(spvtools::CreateSimplificationPass());
209 optimizer.RegisterPass(spvtools::CreateAggressiveDCEPass());
210 optimizer.RegisterPass(spvtools::CreateVectorDCEPass());
211 optimizer.RegisterPass(spvtools::CreateDeadInsertElimPass());
212 optimizer.RegisterPass(spvtools::CreateInterpolateFixupPass());
213 if (options->optimizeSize) {
214 optimizer.RegisterPass(spvtools::CreateRedundancyEliminationPass());
215 optimizer.RegisterPass(spvtools::CreateEliminateDeadInputComponentsSafePass());
216 }
217 optimizer.RegisterPass(spvtools::CreateAggressiveDCEPass());
218 optimizer.RegisterPass(spvtools::CreateCFGCleanupPass());
219
220 spvtools::OptimizerOptions spvOptOptions;
221 optimizer.SetTargetEnv(MapToSpirvToolsEnv(intermediate.getSpv(), logger));
222 spvOptOptions.set_run_validator(false); // The validator may run as a separate step later on
223 optimizer.Run(spirv.data(), spirv.size(), &spirv, spvOptOptions);
224 }
225
SpirvToolsAnalyzeDeadOutputStores(spv_target_env target_env,std::vector<unsigned int> & spirv,std::unordered_set<uint32_t> * live_locs,std::unordered_set<uint32_t> * live_builtins,spv::SpvBuildLogger * logger)226 bool SpirvToolsAnalyzeDeadOutputStores(spv_target_env target_env, std::vector<unsigned int>& spirv,
227 std::unordered_set<uint32_t>* live_locs,
228 std::unordered_set<uint32_t>* live_builtins,
229 spv::SpvBuildLogger* logger)
230 {
231 spvtools::Optimizer optimizer(target_env);
232 optimizer.SetMessageConsumer(OptimizerMesssageConsumer);
233
234 optimizer.RegisterPass(spvtools::CreateAnalyzeLiveInputPass(live_locs, live_builtins));
235
236 spvtools::OptimizerOptions spvOptOptions;
237 optimizer.SetTargetEnv(target_env);
238 spvOptOptions.set_run_validator(false);
239 return optimizer.Run(spirv.data(), spirv.size(), &spirv, spvOptOptions);
240 }
241
SpirvToolsEliminateDeadOutputStores(spv_target_env target_env,std::vector<unsigned int> & spirv,std::unordered_set<uint32_t> * live_locs,std::unordered_set<uint32_t> * live_builtins,spv::SpvBuildLogger * logger)242 void SpirvToolsEliminateDeadOutputStores(spv_target_env target_env, std::vector<unsigned int>& spirv,
243 std::unordered_set<uint32_t>* live_locs,
244 std::unordered_set<uint32_t>* live_builtins,
245 spv::SpvBuildLogger* logger)
246 {
247 spvtools::Optimizer optimizer(target_env);
248 optimizer.SetMessageConsumer(OptimizerMesssageConsumer);
249
250 optimizer.RegisterPass(spvtools::CreateEliminateDeadOutputStoresPass(live_locs, live_builtins));
251 optimizer.RegisterPass(spvtools::CreateAggressiveDCEPass(false, true));
252 optimizer.RegisterPass(spvtools::CreateEliminateDeadOutputComponentsPass());
253 optimizer.RegisterPass(spvtools::CreateAggressiveDCEPass(false, true));
254
255 spvtools::OptimizerOptions spvOptOptions;
256 optimizer.SetTargetEnv(target_env);
257 spvOptOptions.set_run_validator(false);
258 optimizer.Run(spirv.data(), spirv.size(), &spirv, spvOptOptions);
259 }
260
SpirvToolsEliminateDeadInputComponents(spv_target_env target_env,std::vector<unsigned int> & spirv,spv::SpvBuildLogger * logger)261 void SpirvToolsEliminateDeadInputComponents(spv_target_env target_env, std::vector<unsigned int>& spirv,
262 spv::SpvBuildLogger* logger)
263 {
264 spvtools::Optimizer optimizer(target_env);
265 optimizer.SetMessageConsumer(OptimizerMesssageConsumer);
266
267 optimizer.RegisterPass(spvtools::CreateEliminateDeadInputComponentsPass());
268 optimizer.RegisterPass(spvtools::CreateAggressiveDCEPass());
269
270 spvtools::OptimizerOptions spvOptOptions;
271 optimizer.SetTargetEnv(target_env);
272 spvOptOptions.set_run_validator(false);
273 optimizer.Run(spirv.data(), spirv.size(), &spirv, spvOptOptions);
274 }
275
276 // Apply the SPIRV-Tools optimizer to strip debug info from SPIR-V. This is implicitly done by
277 // SpirvToolsTransform if spvOptions->stripDebugInfo is set, but can be called separately if
278 // optimization is disabled.
SpirvToolsStripDebugInfo(const glslang::TIntermediate & intermediate,std::vector<unsigned int> & spirv,spv::SpvBuildLogger * logger)279 void SpirvToolsStripDebugInfo(const glslang::TIntermediate& intermediate,
280 std::vector<unsigned int>& spirv, spv::SpvBuildLogger* logger)
281 {
282 spv_target_env target_env = MapToSpirvToolsEnv(intermediate.getSpv(), logger);
283
284 spvtools::Optimizer optimizer(target_env);
285 optimizer.SetMessageConsumer(OptimizerMesssageConsumer);
286
287 optimizer.RegisterPass(spvtools::CreateStripDebugInfoPass());
288
289 spvtools::OptimizerOptions spvOptOptions;
290 optimizer.SetTargetEnv(MapToSpirvToolsEnv(intermediate.getSpv(), logger));
291 spvOptOptions.set_run_validator(false); // The validator may run as a separate step later on
292 optimizer.Run(spirv.data(), spirv.size(), &spirv, spvOptOptions);
293 }
294
295 }; // end namespace glslang
296
297 #endif
298