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 #include "spirv-tools/libspirv.h"
48
49 namespace glslang {
50
51 // Translate glslang's view of target versioning to what SPIRV-Tools uses.
MapToSpirvToolsEnv(const SpvVersion & spvVersion,spv::SpvBuildLogger * logger)52 spv_target_env MapToSpirvToolsEnv(const SpvVersion& spvVersion, spv::SpvBuildLogger* logger)
53 {
54 switch (spvVersion.vulkan) {
55 case glslang::EShTargetVulkan_1_0:
56 return spv_target_env::SPV_ENV_VULKAN_1_0;
57 case glslang::EShTargetVulkan_1_1:
58 switch (spvVersion.spv) {
59 case EShTargetSpv_1_0:
60 case EShTargetSpv_1_1:
61 case EShTargetSpv_1_2:
62 case EShTargetSpv_1_3:
63 return spv_target_env::SPV_ENV_VULKAN_1_1;
64 case EShTargetSpv_1_4:
65 return spv_target_env::SPV_ENV_VULKAN_1_1_SPIRV_1_4;
66 default:
67 logger->missingFunctionality("Target version for SPIRV-Tools validator");
68 return spv_target_env::SPV_ENV_VULKAN_1_1;
69 }
70 case glslang::EShTargetVulkan_1_2:
71 return spv_target_env::SPV_ENV_VULKAN_1_2;
72 default:
73 break;
74 }
75
76 if (spvVersion.openGl > 0)
77 return spv_target_env::SPV_ENV_OPENGL_4_5;
78
79 logger->missingFunctionality("Target version for SPIRV-Tools validator");
80 return spv_target_env::SPV_ENV_UNIVERSAL_1_0;
81 }
82
83 // Callback passed to spvtools::Optimizer::SetMessageConsumer
OptimizerMesssageConsumer(spv_message_level_t level,const char * source,const spv_position_t & position,const char * message)84 void OptimizerMesssageConsumer(spv_message_level_t level, const char *source,
85 const spv_position_t &position, const char *message)
86 {
87 auto &out = std::cerr;
88 switch (level)
89 {
90 case SPV_MSG_FATAL:
91 case SPV_MSG_INTERNAL_ERROR:
92 case SPV_MSG_ERROR:
93 out << "error: ";
94 break;
95 case SPV_MSG_WARNING:
96 out << "warning: ";
97 break;
98 case SPV_MSG_INFO:
99 case SPV_MSG_DEBUG:
100 out << "info: ";
101 break;
102 default:
103 break;
104 }
105 if (source)
106 {
107 out << source << ":";
108 }
109 out << position.line << ":" << position.column << ":" << position.index << ":";
110 if (message)
111 {
112 out << " " << message;
113 }
114 out << std::endl;
115 }
116
117 // Use the SPIRV-Tools disassembler to print SPIR-V.
SpirvToolsDisassemble(std::ostream & out,const std::vector<unsigned int> & spirv)118 void SpirvToolsDisassemble(std::ostream& out, const std::vector<unsigned int>& spirv)
119 {
120 // disassemble
121 spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_3);
122 spv_text text;
123 spv_diagnostic diagnostic = nullptr;
124 spvBinaryToText(context, spirv.data(), spirv.size(),
125 SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES | SPV_BINARY_TO_TEXT_OPTION_INDENT,
126 &text, &diagnostic);
127
128 // dump
129 if (diagnostic == nullptr)
130 out << text->str;
131 else
132 spvDiagnosticPrint(diagnostic);
133
134 // teardown
135 spvDiagnosticDestroy(diagnostic);
136 spvContextDestroy(context);
137 }
138
139 // Apply the SPIRV-Tools validator to generated SPIR-V.
SpirvToolsValidate(const glslang::TIntermediate & intermediate,std::vector<unsigned int> & spirv,spv::SpvBuildLogger * logger,bool prelegalization)140 void SpirvToolsValidate(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv,
141 spv::SpvBuildLogger* logger, bool prelegalization)
142 {
143 // validate
144 spv_context context = spvContextCreate(MapToSpirvToolsEnv(intermediate.getSpv(), logger));
145 spv_const_binary_t binary = { spirv.data(), spirv.size() };
146 spv_diagnostic diagnostic = nullptr;
147 spv_validator_options options = spvValidatorOptionsCreate();
148 spvValidatorOptionsSetRelaxBlockLayout(options, intermediate.usingHlslOffsets());
149 spvValidatorOptionsSetBeforeHlslLegalization(options, prelegalization);
150 spvValidateWithOptions(context, options, &binary, &diagnostic);
151
152 // report
153 if (diagnostic != nullptr) {
154 logger->error("SPIRV-Tools Validation Errors");
155 logger->error(diagnostic->error);
156 }
157
158 // tear down
159 spvValidatorOptionsDestroy(options);
160 spvDiagnosticDestroy(diagnostic);
161 spvContextDestroy(context);
162 }
163
164 // 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)165 void SpirvToolsTransform(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv,
166 spv::SpvBuildLogger* logger, const SpvOptions* options)
167 {
168 spv_target_env target_env = MapToSpirvToolsEnv(intermediate.getSpv(), logger);
169
170 spvtools::Optimizer optimizer(target_env);
171 optimizer.SetMessageConsumer(OptimizerMesssageConsumer);
172
173 // If debug (specifically source line info) is being generated, propagate
174 // line information into all SPIR-V instructions. This avoids loss of
175 // information when instructions are deleted or moved. Later, remove
176 // redundant information to minimize final SPRIR-V size.
177 if (options->generateDebugInfo) {
178 optimizer.RegisterPass(spvtools::CreatePropagateLineInfoPass());
179 }
180 else if (options->stripDebugInfo) {
181 optimizer.RegisterPass(spvtools::CreateStripDebugInfoPass());
182 }
183 optimizer.RegisterPass(spvtools::CreateWrapOpKillPass());
184 optimizer.RegisterPass(spvtools::CreateDeadBranchElimPass());
185 optimizer.RegisterPass(spvtools::CreateMergeReturnPass());
186 optimizer.RegisterPass(spvtools::CreateInlineExhaustivePass());
187 optimizer.RegisterPass(spvtools::CreateEliminateDeadFunctionsPass());
188 optimizer.RegisterPass(spvtools::CreateScalarReplacementPass());
189 optimizer.RegisterPass(spvtools::CreateLocalAccessChainConvertPass());
190 optimizer.RegisterPass(spvtools::CreateLocalSingleBlockLoadStoreElimPass());
191 optimizer.RegisterPass(spvtools::CreateLocalSingleStoreElimPass());
192 optimizer.RegisterPass(spvtools::CreateSimplificationPass());
193 optimizer.RegisterPass(spvtools::CreateAggressiveDCEPass());
194 optimizer.RegisterPass(spvtools::CreateVectorDCEPass());
195 optimizer.RegisterPass(spvtools::CreateDeadInsertElimPass());
196 optimizer.RegisterPass(spvtools::CreateAggressiveDCEPass());
197 optimizer.RegisterPass(spvtools::CreateDeadBranchElimPass());
198 optimizer.RegisterPass(spvtools::CreateBlockMergePass());
199 optimizer.RegisterPass(spvtools::CreateLocalMultiStoreElimPass());
200 optimizer.RegisterPass(spvtools::CreateIfConversionPass());
201 optimizer.RegisterPass(spvtools::CreateSimplificationPass());
202 optimizer.RegisterPass(spvtools::CreateAggressiveDCEPass());
203 optimizer.RegisterPass(spvtools::CreateVectorDCEPass());
204 optimizer.RegisterPass(spvtools::CreateDeadInsertElimPass());
205 if (options->optimizeSize) {
206 optimizer.RegisterPass(spvtools::CreateRedundancyEliminationPass());
207 }
208 optimizer.RegisterPass(spvtools::CreateAggressiveDCEPass());
209 optimizer.RegisterPass(spvtools::CreateCFGCleanupPass());
210 if (options->generateDebugInfo) {
211 optimizer.RegisterPass(spvtools::CreateRedundantLineInfoElimPass());
212 }
213
214 spvtools::OptimizerOptions spvOptOptions;
215 optimizer.SetTargetEnv(MapToSpirvToolsEnv(intermediate.getSpv(), logger));
216 spvOptOptions.set_run_validator(false); // The validator may run as a separate step later on
217 optimizer.Run(spirv.data(), spirv.size(), &spirv, spvOptOptions);
218 }
219
220 // Apply the SPIRV-Tools optimizer to strip debug info from SPIR-V. This is implicitly done by
221 // SpirvToolsTransform if spvOptions->stripDebugInfo is set, but can be called separately if
222 // optimization is disabled.
SpirvToolsStripDebugInfo(const glslang::TIntermediate & intermediate,std::vector<unsigned int> & spirv,spv::SpvBuildLogger * logger)223 void SpirvToolsStripDebugInfo(const glslang::TIntermediate& intermediate,
224 std::vector<unsigned int>& spirv, spv::SpvBuildLogger* logger)
225 {
226 spv_target_env target_env = MapToSpirvToolsEnv(intermediate.getSpv(), logger);
227
228 spvtools::Optimizer optimizer(target_env);
229 optimizer.SetMessageConsumer(OptimizerMesssageConsumer);
230
231 optimizer.RegisterPass(spvtools::CreateStripDebugInfoPass());
232
233 spvtools::OptimizerOptions spvOptOptions;
234 optimizer.SetTargetEnv(MapToSpirvToolsEnv(intermediate.getSpv(), logger));
235 spvOptOptions.set_run_validator(false); // The validator may run as a separate step later on
236 optimizer.Run(spirv.data(), spirv.size(), &spirv, spvOptOptions);
237 }
238
239 }; // end namespace glslang
240
241 #endif
242