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 default:
50 break;
51 }
52 TCU_THROW(InternalError, "Unexpected Vulkan Version version requested");
53 return SPV_ENV_VULKAN_1_0;
54 }
55
mapTargetSpvEnvironment(SpirvVersion spirvVersion)56 static spv_target_env mapTargetSpvEnvironment(SpirvVersion spirvVersion)
57 {
58 spv_target_env result = SPV_ENV_UNIVERSAL_1_0;
59
60 switch (spirvVersion)
61 {
62 case SPIRV_VERSION_1_0: result = SPV_ENV_UNIVERSAL_1_0; break; //!< SPIR-V 1.0
63 case SPIRV_VERSION_1_1: result = SPV_ENV_UNIVERSAL_1_1; break; //!< SPIR-V 1.1
64 case SPIRV_VERSION_1_2: result = SPV_ENV_UNIVERSAL_1_2; break; //!< SPIR-V 1.2
65 case SPIRV_VERSION_1_3: result = SPV_ENV_UNIVERSAL_1_3; break; //!< SPIR-V 1.3
66 case SPIRV_VERSION_1_4: result = SPV_ENV_UNIVERSAL_1_4; break; //!< SPIR-V 1.4
67 case SPIRV_VERSION_1_5: result = SPV_ENV_UNIVERSAL_1_5; break; //!< SPIR-V 1.5
68 default: TCU_THROW(InternalError, "Unknown SPIR-V version");
69 }
70
71 return result;
72 }
73
assembleSpirV(const SpirVAsmSource * program,std::vector<deUint32> * dst,SpirVProgramInfo * buildInfo,SpirvVersion spirvVersion)74 bool assembleSpirV (const SpirVAsmSource* program, std::vector<deUint32>* dst, SpirVProgramInfo* buildInfo, SpirvVersion spirvVersion)
75 {
76 const spv_context context = spvContextCreate(mapTargetSpvEnvironment(spirvVersion));
77 spv_binary binary = DE_NULL;
78 spv_diagnostic diagnostic = DE_NULL;
79
80 if (!context)
81 throw std::bad_alloc();
82
83 try
84 {
85 const std::string& spvSource = program->source;
86 const deUint64 compileStartTime = deGetMicroseconds();
87 const deUint32 options = SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS;
88 const spv_result_t compileOk = spvTextToBinaryWithOptions(context, spvSource.c_str(), spvSource.size(), options, &binary, &diagnostic);
89
90 buildInfo->source = spvSource;
91 buildInfo->infoLog = diagnostic? diagnostic->error : ""; // \todo [2015-07-13 pyry] Include debug log?
92 buildInfo->compileTimeUs = deGetMicroseconds() - compileStartTime;
93 buildInfo->compileOk = (compileOk == SPV_SUCCESS);
94
95 if (buildInfo->compileOk)
96 {
97 DE_ASSERT(binary->wordCount > 0);
98 dst->resize(binary->wordCount);
99 std::copy(&binary->code[0], &binary->code[0] + binary->wordCount, dst->begin());
100 }
101
102 spvBinaryDestroy(binary);
103 spvDiagnosticDestroy(diagnostic);
104 spvContextDestroy(context);
105
106 return compileOk == SPV_SUCCESS;
107 }
108 catch (...)
109 {
110 spvBinaryDestroy(binary);
111 spvDiagnosticDestroy(diagnostic);
112 spvContextDestroy(context);
113
114 throw;
115 }
116 }
117
disassembleSpirV(size_t binarySizeInWords,const deUint32 * binary,std::ostream * dst,SpirvVersion spirvVersion)118 void disassembleSpirV (size_t binarySizeInWords, const deUint32* binary, std::ostream* dst, SpirvVersion spirvVersion)
119 {
120 const spv_context context = spvContextCreate(mapTargetSpvEnvironment(spirvVersion));
121 spv_text text = DE_NULL;
122 spv_diagnostic diagnostic = DE_NULL;
123
124 if (!context)
125 throw std::bad_alloc();
126
127 try
128 {
129 const spv_result_t result = spvBinaryToText(context, binary, binarySizeInWords, 0, &text, &diagnostic);
130
131 if (result != SPV_SUCCESS)
132 TCU_THROW(InternalError, "Disassembling SPIR-V failed");
133
134 *dst << text->str;
135
136 spvTextDestroy(text);
137 spvDiagnosticDestroy(diagnostic);
138 spvContextDestroy(context);
139 }
140 catch (...)
141 {
142 spvTextDestroy(text);
143 spvDiagnosticDestroy(diagnostic);
144 spvContextDestroy(context);
145
146 throw;
147 }
148 }
149
validateSpirV(size_t binarySizeInWords,const deUint32 * binary,std::ostream * infoLog,const SpirvValidatorOptions & val_options)150 bool validateSpirV (size_t binarySizeInWords, const deUint32* binary, std::ostream* infoLog, const SpirvValidatorOptions &val_options)
151 {
152 const spv_context context = spvContextCreate(getSpirvToolsEnvForValidatorOptions(val_options));
153 spv_diagnostic diagnostic = DE_NULL;
154 spv_validator_options options = DE_NULL;
155 spv_text disasmText = DE_NULL;
156
157 if (!context)
158 throw std::bad_alloc();
159
160 try
161 {
162 spv_const_binary_t cbinary = { binary, binarySizeInWords };
163
164 options = spvValidatorOptionsCreate();
165
166 if (options == DE_NULL)
167 throw std::bad_alloc();
168
169 switch (val_options.blockLayout)
170 {
171 case SpirvValidatorOptions::kDefaultBlockLayout:
172 break;
173 case SpirvValidatorOptions::kNoneBlockLayout:
174 spvValidatorOptionsSetSkipBlockLayout(options, true);
175 break;
176 case SpirvValidatorOptions::kRelaxedBlockLayout:
177 spvValidatorOptionsSetRelaxBlockLayout(options, true);
178 break;
179 case SpirvValidatorOptions::kUniformStandardLayout:
180 spvValidatorOptionsSetUniformBufferStandardLayout(options, true);
181 break;
182 case SpirvValidatorOptions::kScalarBlockLayout:
183 spvValidatorOptionsSetScalarBlockLayout(options, true);
184 break;
185 }
186
187 if (val_options.flags & SpirvValidatorOptions::FLAG_SPIRV_VALIDATOR_WORKGROUP_SCALAR_BLOCK_LAYOUT)
188 {
189 spvValidatorOptionsSetWorkgroupScalarBlockLayout(options, true);
190 }
191
192 const spv_result_t valid = spvValidateWithOptions(context, options, &cbinary, &diagnostic);
193 const bool passed = (valid == SPV_SUCCESS);
194
195 *infoLog << "Validation " << (passed ? "PASSED: " : "FAILED: ");
196
197 if (diagnostic && diagnostic->error)
198 {
199 // Print the diagnostic whether validation passes or fails.
200 // In theory we could get a warning even in the pass case, but there are no cases
201 // like that now.
202 *infoLog << diagnostic->error << "\n";
203
204 const deUint32 disasmOptions = SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES
205 | SPV_BINARY_TO_TEXT_OPTION_INDENT;
206 const spv_result_t disasmResult = spvBinaryToText(context, binary, binarySizeInWords, disasmOptions, &disasmText, DE_NULL);
207
208 if (disasmResult != SPV_SUCCESS)
209 *infoLog << "Disassembly failed with code: " << de::toString(disasmResult) << "\n";
210
211 if (disasmText != DE_NULL)
212 *infoLog << disasmText->str << "\n";
213 }
214
215 spvTextDestroy(disasmText);
216 spvValidatorOptionsDestroy(options);
217 spvDiagnosticDestroy(diagnostic);
218 spvContextDestroy(context);
219
220 return passed;
221 }
222 catch (...)
223 {
224 spvTextDestroy(disasmText);
225 spvValidatorOptionsDestroy(options);
226 spvDiagnosticDestroy(diagnostic);
227 spvContextDestroy(context);
228
229 throw;
230 }
231 }
232
233 } // vk
234