• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-------------------------------------------------------------------------
2  * Vulkan CTS Framework
3  * --------------------
4  *
5  * Copyright (c) 2015 Google Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief SPIR-V assembly to binary.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "vkSpirVAsm.hpp"
25 #include "vkSpirVProgram.hpp"
26 #include "deClock.h"
27 
28 #include <algorithm>
29 
30 #include "spirv-tools/libspirv.h"
31 
32 namespace vk
33 {
34 
35 using std::string;
36 using std::vector;
37 
38 // Returns the SPIRV-Tools target environment enum for the given dEQP Spirv validator options object.
39 // Do this here instead of as a method on SpirvValidatorOptions because only this file has access to
40 // the SPIRV-Tools headers.
getSpirvToolsEnvForValidatorOptions(SpirvValidatorOptions opts)41 static spv_target_env getSpirvToolsEnvForValidatorOptions(SpirvValidatorOptions opts)
42 {
43 	const bool allow_1_4 = opts.supports_VK_KHR_spirv_1_4;
44 	switch (opts.vulkanVersion)
45 	{
46 		case VK_MAKE_VERSION(1, 0, 0): return SPV_ENV_VULKAN_1_0;
47 		case VK_MAKE_VERSION(1, 1, 0): return allow_1_4 ? SPV_ENV_VULKAN_1_1_SPIRV_1_4 : SPV_ENV_VULKAN_1_1;
48 		case VK_MAKE_VERSION(1, 2, 0): return SPV_ENV_VULKAN_1_2;
49 		case VK_MAKE_VERSION(1, 3, 0): return SPV_ENV_VULKAN_1_3;
50 		default:
51 			break;
52 	}
53 	TCU_THROW(InternalError, "Unexpected Vulkan Version version requested");
54 	return SPV_ENV_VULKAN_1_0;
55 }
56 
mapTargetSpvEnvironment(SpirvVersion spirvVersion)57 static spv_target_env mapTargetSpvEnvironment(SpirvVersion spirvVersion)
58 {
59 	spv_target_env result = SPV_ENV_UNIVERSAL_1_0;
60 
61 	switch (spirvVersion)
62 	{
63 		case SPIRV_VERSION_1_0: result = SPV_ENV_UNIVERSAL_1_0; break;	//!< SPIR-V 1.0
64 		case SPIRV_VERSION_1_1: result = SPV_ENV_UNIVERSAL_1_1; break;	//!< SPIR-V 1.1
65 		case SPIRV_VERSION_1_2: result = SPV_ENV_UNIVERSAL_1_2; break;	//!< SPIR-V 1.2
66 		case SPIRV_VERSION_1_3: result = SPV_ENV_UNIVERSAL_1_3; break;	//!< SPIR-V 1.3
67 		case SPIRV_VERSION_1_4: result = SPV_ENV_UNIVERSAL_1_4; break;	//!< SPIR-V 1.4
68 		case SPIRV_VERSION_1_5: result = SPV_ENV_UNIVERSAL_1_5; break;	//!< SPIR-V 1.5
69 		case SPIRV_VERSION_1_6: result = SPV_ENV_UNIVERSAL_1_6; break;	//!< SPIR-V 1.6
70 		default:				TCU_THROW(InternalError, "Unknown SPIR-V version");
71 	}
72 
73 	return result;
74 }
75 
assembleSpirV(const SpirVAsmSource * program,std::vector<deUint32> * dst,SpirVProgramInfo * buildInfo,SpirvVersion spirvVersion)76 bool assembleSpirV (const SpirVAsmSource* program, std::vector<deUint32>* dst, SpirVProgramInfo* buildInfo, SpirvVersion spirvVersion)
77 {
78 	const spv_context	context		= spvContextCreate(mapTargetSpvEnvironment(spirvVersion));
79 	spv_binary			binary		= DE_NULL;
80 	spv_diagnostic		diagnostic	= DE_NULL;
81 
82 	if (!context)
83 		throw std::bad_alloc();
84 
85 	try
86 	{
87 		const std::string&	spvSource			= program->source;
88 		const deUint64		compileStartTime	= deGetMicroseconds();
89 		const deUint32		options				= SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS;
90 		const spv_result_t	compileOk			= spvTextToBinaryWithOptions(context, spvSource.c_str(), spvSource.size(), options, &binary, &diagnostic);
91 
92 		buildInfo->source			= spvSource;
93 		buildInfo->infoLog			= diagnostic? diagnostic->error : ""; // \todo [2015-07-13 pyry] Include debug log?
94 		buildInfo->compileTimeUs	= deGetMicroseconds() - compileStartTime;
95 		buildInfo->compileOk		= (compileOk == SPV_SUCCESS);
96 
97 		if (buildInfo->compileOk)
98 		{
99 			DE_ASSERT(binary->wordCount > 0);
100 			dst->resize(binary->wordCount);
101 			std::copy(&binary->code[0], &binary->code[0] + binary->wordCount, dst->begin());
102 		}
103 
104 		spvBinaryDestroy(binary);
105 		spvDiagnosticDestroy(diagnostic);
106 		spvContextDestroy(context);
107 
108 		return compileOk == SPV_SUCCESS;
109 	}
110 	catch (...)
111 	{
112 		spvBinaryDestroy(binary);
113 		spvDiagnosticDestroy(diagnostic);
114 		spvContextDestroy(context);
115 
116 		throw;
117 	}
118 }
119 
disassembleSpirV(size_t binarySizeInWords,const deUint32 * binary,std::ostream * dst,SpirvVersion spirvVersion)120 void disassembleSpirV (size_t binarySizeInWords, const deUint32* binary, std::ostream* dst, SpirvVersion spirvVersion)
121 {
122 	const spv_context	context		= spvContextCreate(mapTargetSpvEnvironment(spirvVersion));
123 	spv_text			text		= DE_NULL;
124 	spv_diagnostic		diagnostic	= DE_NULL;
125 
126 	if (!context)
127 		throw std::bad_alloc();
128 
129 	try
130 	{
131 		const spv_result_t	result	= spvBinaryToText(context, binary, binarySizeInWords, 0, &text, &diagnostic);
132 
133 		if (result != SPV_SUCCESS)
134 			TCU_THROW(InternalError, "Disassembling SPIR-V failed");
135 
136 		*dst << text->str;
137 
138 		spvTextDestroy(text);
139 		spvDiagnosticDestroy(diagnostic);
140 		spvContextDestroy(context);
141 	}
142 	catch (...)
143 	{
144 		spvTextDestroy(text);
145 		spvDiagnosticDestroy(diagnostic);
146 		spvContextDestroy(context);
147 
148 		throw;
149 	}
150 }
151 
validateSpirV(size_t binarySizeInWords,const deUint32 * binary,std::ostream * infoLog,const SpirvValidatorOptions & val_options)152 bool validateSpirV (size_t binarySizeInWords, const deUint32* binary, std::ostream* infoLog, const SpirvValidatorOptions &val_options)
153 {
154 	const spv_context		context		= spvContextCreate(getSpirvToolsEnvForValidatorOptions(val_options));
155 	spv_diagnostic			diagnostic	= DE_NULL;
156 	spv_validator_options	options		= DE_NULL;
157 	spv_text				disasmText	= DE_NULL;
158 
159 	if (!context)
160 		throw std::bad_alloc();
161 
162 	try
163 	{
164 		spv_const_binary_t		cbinary	= { binary, binarySizeInWords };
165 
166 		options = spvValidatorOptionsCreate();
167 
168 		if (options == DE_NULL)
169 			throw std::bad_alloc();
170 
171 		switch (val_options.blockLayout)
172 		{
173 			case SpirvValidatorOptions::kDefaultBlockLayout:
174 				break;
175 			case SpirvValidatorOptions::kNoneBlockLayout:
176 				spvValidatorOptionsSetSkipBlockLayout(options, true);
177 				break;
178 			case SpirvValidatorOptions::kRelaxedBlockLayout:
179 				spvValidatorOptionsSetRelaxBlockLayout(options, true);
180 				break;
181 			case SpirvValidatorOptions::kUniformStandardLayout:
182 				spvValidatorOptionsSetUniformBufferStandardLayout(options, true);
183 				break;
184 			case SpirvValidatorOptions::kScalarBlockLayout:
185 				spvValidatorOptionsSetScalarBlockLayout(options, true);
186 				break;
187 		}
188 
189 		if (val_options.flags & SpirvValidatorOptions::FLAG_SPIRV_VALIDATOR_WORKGROUP_SCALAR_BLOCK_LAYOUT)
190 		{
191 			spvValidatorOptionsSetWorkgroupScalarBlockLayout(options, true);
192 		}
193 
194 		if (val_options.flags & SpirvValidatorOptions::FLAG_SPIRV_VALIDATOR_ALLOW_LOCALSIZEID)
195 			spvValidatorOptionsSetAllowLocalSizeId(options, true);
196 
197 		const spv_result_t		valid	= spvValidateWithOptions(context, options, &cbinary, &diagnostic);
198 		const bool				passed	= (valid == SPV_SUCCESS);
199 
200 		*infoLog << "Validation " << (passed ? "PASSED: " : "FAILED: ");
201 
202 		if (diagnostic && diagnostic->error)
203 		{
204 			// Print the diagnostic whether validation passes or fails.
205 			// In theory we could get a warning even in the pass case, but there are no cases
206 			// like that now.
207 			*infoLog << diagnostic->error << "\n";
208 
209 			const deUint32		disasmOptions	= SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES
210 												| SPV_BINARY_TO_TEXT_OPTION_INDENT;
211 			const spv_result_t	disasmResult	= spvBinaryToText(context, binary, binarySizeInWords, disasmOptions, &disasmText, DE_NULL);
212 
213 			if (disasmResult != SPV_SUCCESS)
214 				*infoLog << "Disassembly failed with code: " << de::toString(disasmResult) << "\n";
215 
216 			if (disasmText != DE_NULL)
217 				*infoLog << disasmText->str << "\n";
218 		}
219 
220 		spvTextDestroy(disasmText);
221 		spvValidatorOptionsDestroy(options);
222 		spvDiagnosticDestroy(diagnostic);
223 		spvContextDestroy(context);
224 
225 		return passed;
226 	}
227 	catch (...)
228 	{
229 		spvTextDestroy(disasmText);
230 		spvValidatorOptionsDestroy(options);
231 		spvDiagnosticDestroy(diagnostic);
232 		spvContextDestroy(context);
233 
234 		throw;
235 	}
236 }
237 
238 } // vk
239