Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1 | /* Copyright (c) 2015-2019 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2019 Valve Corporation |
| 3 | * Copyright (c) 2015-2019 LunarG, Inc. |
| 4 | * Copyright (C) 2015-2019 Google Inc. |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | * Author: Chris Forbes <chrisf@ijw.co.nz> |
| 19 | */ |
| 20 | #ifndef VULKAN_SHADER_VALIDATION_H |
| 21 | #define VULKAN_SHADER_VALIDATION_H |
| 22 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame^] | 23 | #include <unordered_map> |
| 24 | |
Jeff Bolz | 104e743 | 2019-02-09 08:04:42 -0600 | [diff] [blame] | 25 | #include <SPIRV/spirv.hpp> |
Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 26 | #include <spirv_tools_commit_id.h> |
Mark Lobodzinski | bbaa6a2 | 2018-11-02 12:03:35 -0600 | [diff] [blame] | 27 | #include "spirv-tools/optimizer.hpp" |
Cort Stratton | a81851c | 2017-11-06 19:13:53 -0800 | [diff] [blame] | 28 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 29 | // A forward iterator over spirv instructions. Provides easy access to len, opcode, and content words |
| 30 | // without the caller needing to care too much about the physical SPIRV module layout. |
| 31 | struct spirv_inst_iter { |
| 32 | std::vector<uint32_t>::const_iterator zero; |
| 33 | std::vector<uint32_t>::const_iterator it; |
| 34 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame^] | 35 | uint32_t len() const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 36 | auto result = *it >> 16; |
| 37 | assert(result > 0); |
| 38 | return result; |
| 39 | } |
| 40 | |
| 41 | uint32_t opcode() { return *it & 0x0ffffu; } |
| 42 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame^] | 43 | uint32_t const &word(unsigned n) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 44 | assert(n < len()); |
| 45 | return it[n]; |
| 46 | } |
| 47 | |
| 48 | uint32_t offset() { return (uint32_t)(it - zero); } |
| 49 | |
| 50 | spirv_inst_iter() {} |
| 51 | |
| 52 | spirv_inst_iter(std::vector<uint32_t>::const_iterator zero, std::vector<uint32_t>::const_iterator it) : zero(zero), it(it) {} |
| 53 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame^] | 54 | bool operator==(spirv_inst_iter const &other) const { return it == other.it; } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 55 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame^] | 56 | bool operator!=(spirv_inst_iter const &other) const { return it != other.it; } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 57 | |
| 58 | spirv_inst_iter operator++(int) { // x++ |
| 59 | spirv_inst_iter ii = *this; |
| 60 | it += len(); |
| 61 | return ii; |
| 62 | } |
| 63 | |
| 64 | spirv_inst_iter operator++() { // ++x; |
| 65 | it += len(); |
| 66 | return *this; |
| 67 | } |
| 68 | |
| 69 | // The iterator and the value are the same thing. |
| 70 | spirv_inst_iter &operator*() { return *this; } |
| 71 | spirv_inst_iter const &operator*() const { return *this; } |
| 72 | }; |
| 73 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 74 | struct SHADER_MODULE_STATE { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 75 | // The spirv image itself |
| 76 | std::vector<uint32_t> words; |
| 77 | // A mapping of <id> to the first word of its def. this is useful because walking type |
| 78 | // trees, constant expressions, etc requires jumping all over the instruction stream. |
| 79 | std::unordered_map<unsigned, unsigned> def_index; |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame^] | 80 | struct EntryPoint { |
| 81 | uint32_t offset; |
| 82 | VkShaderStageFlags stage; |
| 83 | }; |
| 84 | std::unordered_multimap<std::string, EntryPoint> entry_points; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 85 | bool has_valid_spirv; |
Mark Young | 4e919b2 | 2018-05-21 15:53:59 -0600 | [diff] [blame] | 86 | VkShaderModule vk_shader_module; |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 87 | uint32_t gpu_validation_shader_id; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 88 | |
Mark Lobodzinski | bbaa6a2 | 2018-11-02 12:03:35 -0600 | [diff] [blame] | 89 | std::vector<uint32_t> PreprocessShaderBinary(uint32_t *src_binary, size_t binary_size, spv_target_env env) { |
Jeff Bolz | 104e743 | 2019-02-09 08:04:42 -0600 | [diff] [blame] | 90 | std::vector<uint32_t> src(src_binary, src_binary + binary_size / sizeof(uint32_t)); |
| 91 | |
| 92 | // Check if there are any group decoration instructions, and flatten them if found. |
| 93 | bool has_group_decoration = false; |
| 94 | bool done = false; |
| 95 | |
| 96 | // Walk through the first part of the SPIR-V module, looking for group decoration instructions. |
| 97 | // Skip the header (5 words). |
| 98 | auto itr = spirv_inst_iter(src.begin(), src.begin() + 5); |
| 99 | auto itrend = spirv_inst_iter(src.begin(), src.end()); |
| 100 | while (itr != itrend && !done) { |
| 101 | spv::Op opcode = (spv::Op)itr.opcode(); |
| 102 | switch (opcode) { |
| 103 | case spv::OpDecorationGroup: |
| 104 | case spv::OpGroupDecorate: |
| 105 | case spv::OpGroupMemberDecorate: |
| 106 | has_group_decoration = true; |
| 107 | done = true; |
| 108 | break; |
| 109 | case spv::OpFunction: |
| 110 | // An OpFunction indicates there are no more decorations |
| 111 | done = true; |
| 112 | break; |
| 113 | default: |
| 114 | break; |
| 115 | } |
| 116 | itr++; |
| 117 | } |
| 118 | |
| 119 | if (has_group_decoration) { |
| 120 | spvtools::Optimizer optimizer(env); |
| 121 | optimizer.RegisterPass(spvtools::CreateFlattenDecorationPass()); |
| 122 | std::vector<uint32_t> optimized_binary; |
Mark Lobodzinski | f2bad66 | 2019-04-16 15:06:09 -0600 | [diff] [blame] | 123 | // Run optimizer to flatten decorations only, set skip_validation so as to not re-run validator |
| 124 | auto result = |
| 125 | optimizer.Run(src_binary, binary_size / sizeof(uint32_t), &optimized_binary, spvtools::ValidatorOptions(), true); |
Jeff Bolz | 104e743 | 2019-02-09 08:04:42 -0600 | [diff] [blame] | 126 | if (result) { |
| 127 | return optimized_binary; |
| 128 | } |
| 129 | } |
| 130 | // Return the original module. |
| 131 | return src; |
Mark Lobodzinski | bbaa6a2 | 2018-11-02 12:03:35 -0600 | [diff] [blame] | 132 | } |
| 133 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 134 | SHADER_MODULE_STATE(VkShaderModuleCreateInfo const *pCreateInfo, VkShaderModule shaderModule, spv_target_env env, |
| 135 | uint32_t unique_shader_id) |
Mark Lobodzinski | bbaa6a2 | 2018-11-02 12:03:35 -0600 | [diff] [blame] | 136 | : words(PreprocessShaderBinary((uint32_t *)pCreateInfo->pCode, pCreateInfo->codeSize, env)), |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 137 | def_index(), |
Mark Young | 4e919b2 | 2018-05-21 15:53:59 -0600 | [diff] [blame] | 138 | has_valid_spirv(true), |
Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 139 | vk_shader_module(shaderModule), |
| 140 | gpu_validation_shader_id(unique_shader_id) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 141 | BuildDefIndex(); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 144 | SHADER_MODULE_STATE() : has_valid_spirv(false), vk_shader_module(VK_NULL_HANDLE) {} |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 145 | |
| 146 | // Expose begin() / end() to enable range-based for |
| 147 | spirv_inst_iter begin() const { return spirv_inst_iter(words.begin(), words.begin() + 5); } // First insn |
| 148 | spirv_inst_iter end() const { return spirv_inst_iter(words.begin(), words.end()); } // Just past last insn |
| 149 | // Given an offset into the module, produce an iterator there. |
| 150 | spirv_inst_iter at(unsigned offset) const { return spirv_inst_iter(words.begin(), words.begin() + offset); } |
| 151 | |
| 152 | // Gets an iterator to the definition of an id |
| 153 | spirv_inst_iter get_def(unsigned id) const { |
| 154 | auto it = def_index.find(id); |
| 155 | if (it == def_index.end()) { |
| 156 | return end(); |
| 157 | } |
| 158 | return at(it->second); |
| 159 | } |
| 160 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 161 | void BuildDefIndex(); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 164 | class ValidationCache { |
| 165 | // hashes of shaders that have passed validation before, and can be skipped. |
| 166 | // we don't store negative results, as we would have to also store what was |
| 167 | // wrong with them; also, we expect they will get fixed, so we're less |
| 168 | // likely to see them again. |
| 169 | std::unordered_set<uint32_t> good_shader_hashes; |
| 170 | ValidationCache() {} |
| 171 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 172 | public: |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 173 | static VkValidationCacheEXT Create(VkValidationCacheCreateInfoEXT const *pCreateInfo) { |
| 174 | auto cache = new ValidationCache(); |
| 175 | cache->Load(pCreateInfo); |
| 176 | return VkValidationCacheEXT(cache); |
| 177 | } |
| 178 | |
| 179 | void Load(VkValidationCacheCreateInfoEXT const *pCreateInfo) { |
Cort Stratton | 77955d8 | 2018-02-01 23:14:50 -0800 | [diff] [blame] | 180 | const auto headerSize = 2 * sizeof(uint32_t) + VK_UUID_SIZE; |
| 181 | auto size = headerSize; |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 182 | if (!pCreateInfo->pInitialData || pCreateInfo->initialDataSize < size) return; |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 183 | |
| 184 | uint32_t const *data = (uint32_t const *)pCreateInfo->pInitialData; |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 185 | if (data[0] != size) return; |
| 186 | if (data[1] != VK_VALIDATION_CACHE_HEADER_VERSION_ONE_EXT) return; |
Cort Stratton | 77955d8 | 2018-02-01 23:14:50 -0800 | [diff] [blame] | 187 | uint8_t expected_uuid[VK_UUID_SIZE]; |
| 188 | Sha1ToVkUuid(SPIRV_TOOLS_COMMIT_ID, expected_uuid); |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 189 | if (memcmp(&data[2], expected_uuid, VK_UUID_SIZE) != 0) return; // different version |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 190 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 191 | data = (uint32_t const *)(reinterpret_cast<uint8_t const *>(data) + headerSize); |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 192 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 193 | for (; size < pCreateInfo->initialDataSize; data++, size += sizeof(uint32_t)) { |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 194 | good_shader_hashes.insert(*data); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void Write(size_t *pDataSize, void *pData) { |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 199 | const auto headerSize = 2 * sizeof(uint32_t) + VK_UUID_SIZE; // 4 bytes for header size + 4 bytes for version number + UUID |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 200 | if (!pData) { |
| 201 | *pDataSize = headerSize + good_shader_hashes.size() * sizeof(uint32_t); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | if (*pDataSize < headerSize) { |
| 206 | *pDataSize = 0; |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 207 | return; // Too small for even the header! |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | uint32_t *out = (uint32_t *)pData; |
| 211 | size_t actualSize = headerSize; |
| 212 | |
| 213 | // Write the header |
| 214 | *out++ = headerSize; |
| 215 | *out++ = VK_VALIDATION_CACHE_HEADER_VERSION_ONE_EXT; |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 216 | Sha1ToVkUuid(SPIRV_TOOLS_COMMIT_ID, reinterpret_cast<uint8_t *>(out)); |
| 217 | out = (uint32_t *)(reinterpret_cast<uint8_t *>(out) + VK_UUID_SIZE); |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 218 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 219 | for (auto it = good_shader_hashes.begin(); it != good_shader_hashes.end() && actualSize < *pDataSize; |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 220 | it++, out++, actualSize += sizeof(uint32_t)) { |
| 221 | *out = *it; |
| 222 | } |
| 223 | |
| 224 | *pDataSize = actualSize; |
| 225 | } |
| 226 | |
| 227 | void Merge(ValidationCache const *other) { |
Chris Forbes | f73483b | 2017-11-22 16:54:46 -0800 | [diff] [blame] | 228 | good_shader_hashes.reserve(good_shader_hashes.size() + other->good_shader_hashes.size()); |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 229 | for (auto h : other->good_shader_hashes) good_shader_hashes.insert(h); |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static uint32_t MakeShaderHash(VkShaderModuleCreateInfo const *smci); |
| 233 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 234 | bool Contains(uint32_t hash) { return good_shader_hashes.count(hash) != 0; } |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 235 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 236 | void Insert(uint32_t hash) { good_shader_hashes.insert(hash); } |
| 237 | |
| 238 | private: |
| 239 | void Sha1ToVkUuid(const char *sha1_str, uint8_t uuid[VK_UUID_SIZE]) { |
| 240 | // Convert sha1_str from a hex string to binary. We only need VK_UUID_BYTES of |
| 241 | // output, so pad with zeroes if the input string is shorter than that, and truncate |
| 242 | // if it's longer. |
| 243 | char padded_sha1_str[2 * VK_UUID_SIZE + 1] = {}; |
| 244 | strncpy(padded_sha1_str, sha1_str, 2 * VK_UUID_SIZE + 1); |
| 245 | char byte_str[3] = {}; |
| 246 | for (uint32_t i = 0; i < VK_UUID_SIZE; ++i) { |
| 247 | byte_str[0] = padded_sha1_str[2 * i + 0]; |
| 248 | byte_str[1] = padded_sha1_str[2 * i + 1]; |
| 249 | uuid[i] = static_cast<uint8_t>(strtol(byte_str, NULL, 16)); |
| 250 | } |
Cort Stratton | b614d33 | 2017-11-22 16:05:49 -0800 | [diff] [blame] | 251 | } |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 252 | }; |
| 253 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 254 | #endif // VULKAN_SHADER_VALIDATION_H |