John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1 | // |
| 2 | //Copyright (C) 2014 LunarG, Inc. |
| 3 | // |
| 4 | //All rights reserved. |
| 5 | // |
| 6 | //Redistribution and use in source and binary forms, with or without |
| 7 | //modification, are permitted provided that the following conditions |
| 8 | //are met: |
| 9 | // |
| 10 | // Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // |
| 13 | // Redistributions in binary form must reproduce the above |
| 14 | // copyright notice, this list of conditions and the following |
| 15 | // disclaimer in the documentation and/or other materials provided |
| 16 | // with the distribution. |
| 17 | // |
| 18 | // Neither the name of 3Dlabs Inc. Ltd. nor the names of its |
| 19 | // contributors may be used to endorse or promote products derived |
| 20 | // from this software without specific prior written permission. |
| 21 | // |
| 22 | //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 25 | //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 26 | //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 27 | //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 28 | //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 29 | //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 30 | //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 31 | //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 32 | //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 33 | //POSSIBILITY OF SUCH DAMAGE. |
| 34 | |
| 35 | // |
| 36 | // Author: John Kessenich, LunarG |
| 37 | // |
| 38 | |
| 39 | // |
| 40 | // Helper for making SPIR-V IR. Generally, this is documented in the header |
| 41 | // SpvBuilder.h. |
| 42 | // |
| 43 | |
| 44 | #include <assert.h> |
| 45 | #include <stdio.h> |
| 46 | #include <stdlib.h> |
| 47 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 48 | #include <unordered_set> |
| 49 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 50 | #include "SpvBuilder.h" |
| 51 | |
| 52 | #ifndef _WIN32 |
| 53 | #include <cstdio> |
| 54 | #endif |
| 55 | |
| 56 | namespace spv { |
| 57 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 58 | Builder::Builder(unsigned int magicNumber) : |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 59 | source(SourceLanguageUnknown), |
| 60 | sourceVersion(0), |
| 61 | addressModel(AddressingModelLogical), |
| 62 | memoryModel(MemoryModelGLSL450), |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 63 | builderNumber(magicNumber), |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 64 | buildPoint(0), |
| 65 | uniqueId(0), |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 66 | mainFunction(0) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 67 | { |
| 68 | clearAccessChain(); |
| 69 | } |
| 70 | |
| 71 | Builder::~Builder() |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | Id Builder::import(const char* name) |
| 76 | { |
| 77 | Instruction* import = new Instruction(getUniqueId(), NoType, OpExtInstImport); |
| 78 | import->addStringOperand(name); |
| 79 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 80 | imports.push_back(std::unique_ptr<Instruction>(import)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 81 | return import->getResultId(); |
| 82 | } |
| 83 | |
| 84 | // For creating new groupedTypes (will return old type if the requested one was already made). |
| 85 | Id Builder::makeVoidType() |
| 86 | { |
| 87 | Instruction* type; |
| 88 | if (groupedTypes[OpTypeVoid].size() == 0) { |
| 89 | type = new Instruction(getUniqueId(), NoType, OpTypeVoid); |
| 90 | groupedTypes[OpTypeVoid].push_back(type); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 91 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 92 | module.mapInstruction(type); |
| 93 | } else |
| 94 | type = groupedTypes[OpTypeVoid].back(); |
| 95 | |
| 96 | return type->getResultId(); |
| 97 | } |
| 98 | |
| 99 | Id Builder::makeBoolType() |
| 100 | { |
| 101 | Instruction* type; |
| 102 | if (groupedTypes[OpTypeBool].size() == 0) { |
| 103 | type = new Instruction(getUniqueId(), NoType, OpTypeBool); |
| 104 | groupedTypes[OpTypeBool].push_back(type); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 105 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 106 | module.mapInstruction(type); |
| 107 | } else |
| 108 | type = groupedTypes[OpTypeBool].back(); |
| 109 | |
| 110 | return type->getResultId(); |
| 111 | } |
| 112 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 113 | Id Builder::makeSamplerType() |
| 114 | { |
| 115 | Instruction* type; |
| 116 | if (groupedTypes[OpTypeSampler].size() == 0) { |
| 117 | type = new Instruction(getUniqueId(), NoType, OpTypeSampler); |
| 118 | groupedTypes[OpTypeSampler].push_back(type); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 119 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type)); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 120 | module.mapInstruction(type); |
| 121 | } else |
| 122 | type = groupedTypes[OpTypeSampler].back(); |
| 123 | |
| 124 | return type->getResultId(); |
| 125 | } |
| 126 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 127 | Id Builder::makePointer(StorageClass storageClass, Id pointee) |
| 128 | { |
| 129 | // try to find it |
| 130 | Instruction* type; |
| 131 | for (int t = 0; t < (int)groupedTypes[OpTypePointer].size(); ++t) { |
| 132 | type = groupedTypes[OpTypePointer][t]; |
| 133 | if (type->getImmediateOperand(0) == (unsigned)storageClass && |
| 134 | type->getIdOperand(1) == pointee) |
| 135 | return type->getResultId(); |
| 136 | } |
| 137 | |
| 138 | // not found, make it |
| 139 | type = new Instruction(getUniqueId(), NoType, OpTypePointer); |
| 140 | type->addImmediateOperand(storageClass); |
| 141 | type->addIdOperand(pointee); |
| 142 | groupedTypes[OpTypePointer].push_back(type); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 143 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 144 | module.mapInstruction(type); |
| 145 | |
| 146 | return type->getResultId(); |
| 147 | } |
| 148 | |
| 149 | Id Builder::makeIntegerType(int width, bool hasSign) |
| 150 | { |
| 151 | // try to find it |
| 152 | Instruction* type; |
| 153 | for (int t = 0; t < (int)groupedTypes[OpTypeInt].size(); ++t) { |
| 154 | type = groupedTypes[OpTypeInt][t]; |
| 155 | if (type->getImmediateOperand(0) == (unsigned)width && |
| 156 | type->getImmediateOperand(1) == (hasSign ? 1u : 0u)) |
| 157 | return type->getResultId(); |
| 158 | } |
| 159 | |
| 160 | // not found, make it |
| 161 | type = new Instruction(getUniqueId(), NoType, OpTypeInt); |
| 162 | type->addImmediateOperand(width); |
| 163 | type->addImmediateOperand(hasSign ? 1 : 0); |
| 164 | groupedTypes[OpTypeInt].push_back(type); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 165 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 166 | module.mapInstruction(type); |
| 167 | |
| 168 | return type->getResultId(); |
| 169 | } |
| 170 | |
| 171 | Id Builder::makeFloatType(int width) |
| 172 | { |
| 173 | // try to find it |
| 174 | Instruction* type; |
| 175 | for (int t = 0; t < (int)groupedTypes[OpTypeFloat].size(); ++t) { |
| 176 | type = groupedTypes[OpTypeFloat][t]; |
| 177 | if (type->getImmediateOperand(0) == (unsigned)width) |
| 178 | return type->getResultId(); |
| 179 | } |
| 180 | |
| 181 | // not found, make it |
| 182 | type = new Instruction(getUniqueId(), NoType, OpTypeFloat); |
| 183 | type->addImmediateOperand(width); |
| 184 | groupedTypes[OpTypeFloat].push_back(type); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 185 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 186 | module.mapInstruction(type); |
| 187 | |
| 188 | return type->getResultId(); |
| 189 | } |
| 190 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 191 | // Make a struct without checking for duplication. |
| 192 | // See makeStructResultType() for non-decorated structs |
| 193 | // needed as the result of some instructions, which does |
| 194 | // check for duplicates. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 195 | Id Builder::makeStructType(std::vector<Id>& members, const char* name) |
| 196 | { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 197 | // Don't look for previous one, because in the general case, |
| 198 | // structs can be duplicated except for decorations. |
| 199 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 200 | // not found, make it |
| 201 | Instruction* type = new Instruction(getUniqueId(), NoType, OpTypeStruct); |
| 202 | for (int op = 0; op < (int)members.size(); ++op) |
| 203 | type->addIdOperand(members[op]); |
| 204 | groupedTypes[OpTypeStruct].push_back(type); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 205 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 206 | module.mapInstruction(type); |
| 207 | addName(type->getResultId(), name); |
| 208 | |
| 209 | return type->getResultId(); |
| 210 | } |
| 211 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 212 | // Make a struct for the simple results of several instructions, |
| 213 | // checking for duplication. |
| 214 | Id Builder::makeStructResultType(Id type0, Id type1) |
| 215 | { |
| 216 | // try to find it |
| 217 | Instruction* type; |
| 218 | for (int t = 0; t < (int)groupedTypes[OpTypeStruct].size(); ++t) { |
| 219 | type = groupedTypes[OpTypeStruct][t]; |
| 220 | if (type->getNumOperands() != 2) |
| 221 | continue; |
| 222 | if (type->getIdOperand(0) != type0 || |
| 223 | type->getIdOperand(1) != type1) |
| 224 | continue; |
| 225 | return type->getResultId(); |
| 226 | } |
| 227 | |
| 228 | // not found, make it |
| 229 | std::vector<spv::Id> members; |
| 230 | members.push_back(type0); |
| 231 | members.push_back(type1); |
| 232 | |
| 233 | return makeStructType(members, "ResType"); |
| 234 | } |
| 235 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 236 | Id Builder::makeVectorType(Id component, int size) |
| 237 | { |
| 238 | // try to find it |
| 239 | Instruction* type; |
| 240 | for (int t = 0; t < (int)groupedTypes[OpTypeVector].size(); ++t) { |
| 241 | type = groupedTypes[OpTypeVector][t]; |
| 242 | if (type->getIdOperand(0) == component && |
| 243 | type->getImmediateOperand(1) == (unsigned)size) |
| 244 | return type->getResultId(); |
| 245 | } |
| 246 | |
| 247 | // not found, make it |
| 248 | type = new Instruction(getUniqueId(), NoType, OpTypeVector); |
| 249 | type->addIdOperand(component); |
| 250 | type->addImmediateOperand(size); |
| 251 | groupedTypes[OpTypeVector].push_back(type); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 252 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 253 | module.mapInstruction(type); |
| 254 | |
| 255 | return type->getResultId(); |
| 256 | } |
| 257 | |
| 258 | Id Builder::makeMatrixType(Id component, int cols, int rows) |
| 259 | { |
| 260 | assert(cols <= maxMatrixSize && rows <= maxMatrixSize); |
| 261 | |
| 262 | Id column = makeVectorType(component, rows); |
| 263 | |
| 264 | // try to find it |
| 265 | Instruction* type; |
| 266 | for (int t = 0; t < (int)groupedTypes[OpTypeMatrix].size(); ++t) { |
| 267 | type = groupedTypes[OpTypeMatrix][t]; |
| 268 | if (type->getIdOperand(0) == column && |
| 269 | type->getImmediateOperand(1) == (unsigned)cols) |
| 270 | return type->getResultId(); |
| 271 | } |
| 272 | |
| 273 | // not found, make it |
| 274 | type = new Instruction(getUniqueId(), NoType, OpTypeMatrix); |
| 275 | type->addIdOperand(column); |
| 276 | type->addImmediateOperand(cols); |
| 277 | groupedTypes[OpTypeMatrix].push_back(type); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 278 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 279 | module.mapInstruction(type); |
| 280 | |
| 281 | return type->getResultId(); |
| 282 | } |
| 283 | |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 284 | // TODO: performance: track arrays per stride |
| 285 | // If a stride is supplied (non-zero) make an array. |
| 286 | // If no stride (0), reuse previous array types. |
| 287 | Id Builder::makeArrayType(Id element, unsigned size, int stride) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 288 | { |
| 289 | // First, we need a constant instruction for the size |
| 290 | Id sizeId = makeUintConstant(size); |
| 291 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 292 | Instruction* type; |
John Kessenich | c9e0a42 | 2015-12-29 21:27:24 -0700 | [diff] [blame] | 293 | if (stride == 0) { |
| 294 | // try to find existing type |
| 295 | for (int t = 0; t < (int)groupedTypes[OpTypeArray].size(); ++t) { |
| 296 | type = groupedTypes[OpTypeArray][t]; |
| 297 | if (type->getIdOperand(0) == element && |
| 298 | type->getIdOperand(1) == sizeId) |
| 299 | return type->getResultId(); |
| 300 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | // not found, make it |
| 304 | type = new Instruction(getUniqueId(), NoType, OpTypeArray); |
| 305 | type->addIdOperand(element); |
| 306 | type->addIdOperand(sizeId); |
| 307 | groupedTypes[OpTypeArray].push_back(type); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 308 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 309 | module.mapInstruction(type); |
| 310 | |
| 311 | return type->getResultId(); |
| 312 | } |
| 313 | |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 314 | Id Builder::makeRuntimeArray(Id element) |
| 315 | { |
| 316 | Instruction* type = new Instruction(getUniqueId(), NoType, OpTypeRuntimeArray); |
| 317 | type->addIdOperand(element); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 318 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type)); |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 319 | module.mapInstruction(type); |
| 320 | |
| 321 | return type->getResultId(); |
| 322 | } |
| 323 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 324 | Id Builder::makeFunctionType(Id returnType, std::vector<Id>& paramTypes) |
| 325 | { |
| 326 | // try to find it |
| 327 | Instruction* type; |
| 328 | for (int t = 0; t < (int)groupedTypes[OpTypeFunction].size(); ++t) { |
| 329 | type = groupedTypes[OpTypeFunction][t]; |
| 330 | if (type->getIdOperand(0) != returnType || (int)paramTypes.size() != type->getNumOperands() - 1) |
| 331 | continue; |
| 332 | bool mismatch = false; |
| 333 | for (int p = 0; p < (int)paramTypes.size(); ++p) { |
| 334 | if (paramTypes[p] != type->getIdOperand(p + 1)) { |
| 335 | mismatch = true; |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | if (! mismatch) |
John Kessenich | c9a8083 | 2015-09-12 12:17:44 -0600 | [diff] [blame] | 340 | return type->getResultId(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | // not found, make it |
| 344 | type = new Instruction(getUniqueId(), NoType, OpTypeFunction); |
| 345 | type->addIdOperand(returnType); |
| 346 | for (int p = 0; p < (int)paramTypes.size(); ++p) |
| 347 | type->addIdOperand(paramTypes[p]); |
| 348 | groupedTypes[OpTypeFunction].push_back(type); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 349 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 350 | module.mapInstruction(type); |
| 351 | |
| 352 | return type->getResultId(); |
| 353 | } |
| 354 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 355 | Id Builder::makeImageType(Id sampledType, Dim dim, bool depth, bool arrayed, bool ms, unsigned sampled, ImageFormat format) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 356 | { |
| 357 | // try to find it |
| 358 | Instruction* type; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 359 | for (int t = 0; t < (int)groupedTypes[OpTypeImage].size(); ++t) { |
| 360 | type = groupedTypes[OpTypeImage][t]; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 361 | if (type->getIdOperand(0) == sampledType && |
| 362 | type->getImmediateOperand(1) == (unsigned int)dim && |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 363 | type->getImmediateOperand(2) == ( depth ? 1u : 0u) && |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 364 | type->getImmediateOperand(3) == (arrayed ? 1u : 0u) && |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 365 | type->getImmediateOperand(4) == ( ms ? 1u : 0u) && |
| 366 | type->getImmediateOperand(5) == sampled && |
| 367 | type->getImmediateOperand(6) == (unsigned int)format) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 368 | return type->getResultId(); |
| 369 | } |
| 370 | |
| 371 | // not found, make it |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 372 | type = new Instruction(getUniqueId(), NoType, OpTypeImage); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 373 | type->addIdOperand(sampledType); |
| 374 | type->addImmediateOperand( dim); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 375 | type->addImmediateOperand( depth ? 1 : 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 376 | type->addImmediateOperand(arrayed ? 1 : 0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 377 | type->addImmediateOperand( ms ? 1 : 0); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 378 | type->addImmediateOperand(sampled); |
| 379 | type->addImmediateOperand((unsigned int)format); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 380 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 381 | groupedTypes[OpTypeImage].push_back(type); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 382 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type)); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 383 | module.mapInstruction(type); |
| 384 | |
| 385 | return type->getResultId(); |
| 386 | } |
| 387 | |
| 388 | Id Builder::makeSampledImageType(Id imageType) |
| 389 | { |
| 390 | // try to find it |
| 391 | Instruction* type; |
| 392 | for (int t = 0; t < (int)groupedTypes[OpTypeSampledImage].size(); ++t) { |
| 393 | type = groupedTypes[OpTypeSampledImage][t]; |
| 394 | if (type->getIdOperand(0) == imageType) |
| 395 | return type->getResultId(); |
| 396 | } |
| 397 | |
| 398 | // not found, make it |
| 399 | type = new Instruction(getUniqueId(), NoType, OpTypeSampledImage); |
| 400 | type->addIdOperand(imageType); |
| 401 | |
| 402 | groupedTypes[OpTypeSampledImage].push_back(type); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 403 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 404 | module.mapInstruction(type); |
| 405 | |
| 406 | return type->getResultId(); |
| 407 | } |
| 408 | |
| 409 | Id Builder::getDerefTypeId(Id resultId) const |
| 410 | { |
| 411 | Id typeId = getTypeId(resultId); |
| 412 | assert(isPointerType(typeId)); |
| 413 | |
| 414 | return module.getInstruction(typeId)->getImmediateOperand(1); |
| 415 | } |
| 416 | |
| 417 | Op Builder::getMostBasicTypeClass(Id typeId) const |
| 418 | { |
| 419 | Instruction* instr = module.getInstruction(typeId); |
| 420 | |
| 421 | Op typeClass = instr->getOpCode(); |
| 422 | switch (typeClass) |
| 423 | { |
| 424 | case OpTypeVoid: |
| 425 | case OpTypeBool: |
| 426 | case OpTypeInt: |
| 427 | case OpTypeFloat: |
| 428 | case OpTypeStruct: |
| 429 | return typeClass; |
| 430 | case OpTypeVector: |
| 431 | case OpTypeMatrix: |
| 432 | case OpTypeArray: |
| 433 | case OpTypeRuntimeArray: |
| 434 | return getMostBasicTypeClass(instr->getIdOperand(0)); |
| 435 | case OpTypePointer: |
| 436 | return getMostBasicTypeClass(instr->getIdOperand(1)); |
| 437 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 438 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 439 | return OpTypeFloat; |
| 440 | } |
| 441 | } |
| 442 | |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 443 | int Builder::getNumTypeConstituents(Id typeId) const |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 444 | { |
| 445 | Instruction* instr = module.getInstruction(typeId); |
| 446 | |
| 447 | switch (instr->getOpCode()) |
| 448 | { |
| 449 | case OpTypeBool: |
| 450 | case OpTypeInt: |
| 451 | case OpTypeFloat: |
| 452 | return 1; |
| 453 | case OpTypeVector: |
| 454 | case OpTypeMatrix: |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 455 | case OpTypeArray: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 456 | return instr->getImmediateOperand(1); |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 457 | case OpTypeStruct: |
| 458 | return instr->getNumOperands(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 459 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 460 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 461 | return 1; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | // Return the lowest-level type of scalar that an homogeneous composite is made out of. |
| 466 | // Typically, this is just to find out if something is made out of ints or floats. |
| 467 | // However, it includes returning a structure, if say, it is an array of structure. |
| 468 | Id Builder::getScalarTypeId(Id typeId) const |
| 469 | { |
| 470 | Instruction* instr = module.getInstruction(typeId); |
| 471 | |
| 472 | Op typeClass = instr->getOpCode(); |
| 473 | switch (typeClass) |
| 474 | { |
| 475 | case OpTypeVoid: |
| 476 | case OpTypeBool: |
| 477 | case OpTypeInt: |
| 478 | case OpTypeFloat: |
| 479 | case OpTypeStruct: |
| 480 | return instr->getResultId(); |
| 481 | case OpTypeVector: |
| 482 | case OpTypeMatrix: |
| 483 | case OpTypeArray: |
| 484 | case OpTypeRuntimeArray: |
| 485 | case OpTypePointer: |
| 486 | return getScalarTypeId(getContainedTypeId(typeId)); |
| 487 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 488 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 489 | return NoResult; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // Return the type of 'member' of a composite. |
| 494 | Id Builder::getContainedTypeId(Id typeId, int member) const |
| 495 | { |
| 496 | Instruction* instr = module.getInstruction(typeId); |
| 497 | |
| 498 | Op typeClass = instr->getOpCode(); |
| 499 | switch (typeClass) |
| 500 | { |
| 501 | case OpTypeVector: |
| 502 | case OpTypeMatrix: |
| 503 | case OpTypeArray: |
| 504 | case OpTypeRuntimeArray: |
| 505 | return instr->getIdOperand(0); |
| 506 | case OpTypePointer: |
| 507 | return instr->getIdOperand(1); |
| 508 | case OpTypeStruct: |
| 509 | return instr->getIdOperand(member); |
| 510 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 511 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 512 | return NoResult; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | // Return the immediately contained type of a given composite type. |
| 517 | Id Builder::getContainedTypeId(Id typeId) const |
| 518 | { |
| 519 | return getContainedTypeId(typeId, 0); |
| 520 | } |
| 521 | |
| 522 | // See if a scalar constant of this type has already been created, so it |
| 523 | // can be reused rather than duplicated. (Required by the specification). |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 524 | Id Builder::findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned value) const |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 525 | { |
| 526 | Instruction* constant; |
| 527 | for (int i = 0; i < (int)groupedConstants[typeClass].size(); ++i) { |
| 528 | constant = groupedConstants[typeClass][i]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 529 | if (constant->getOpCode() == opcode && |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 530 | constant->getTypeId() == typeId && |
| 531 | constant->getImmediateOperand(0) == value) |
| 532 | return constant->getResultId(); |
| 533 | } |
| 534 | |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | // Version of findScalarConstant (see above) for scalars that take two operands (e.g. a 'double'). |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 539 | Id Builder::findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned v1, unsigned v2) const |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 540 | { |
| 541 | Instruction* constant; |
| 542 | for (int i = 0; i < (int)groupedConstants[typeClass].size(); ++i) { |
| 543 | constant = groupedConstants[typeClass][i]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 544 | if (constant->getOpCode() == opcode && |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 545 | constant->getTypeId() == typeId && |
| 546 | constant->getImmediateOperand(0) == v1 && |
| 547 | constant->getImmediateOperand(1) == v2) |
| 548 | return constant->getResultId(); |
| 549 | } |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
John Kessenich | f685df8 | 2015-10-13 10:55:08 -0600 | [diff] [blame] | 554 | // Return true if consuming 'opcode' means consuming a constant. |
| 555 | // "constant" here means after final transform to executable code, |
| 556 | // the value consumed will be a constant, so includes specialization. |
John Kessenich | 7163127 | 2015-10-13 10:39:19 -0600 | [diff] [blame] | 557 | bool Builder::isConstantOpCode(Op opcode) const |
| 558 | { |
| 559 | switch (opcode) { |
| 560 | case OpUndef: |
| 561 | case OpConstantTrue: |
| 562 | case OpConstantFalse: |
| 563 | case OpConstant: |
| 564 | case OpConstantComposite: |
| 565 | case OpConstantSampler: |
| 566 | case OpConstantNull: |
| 567 | case OpSpecConstantTrue: |
| 568 | case OpSpecConstantFalse: |
| 569 | case OpSpecConstant: |
| 570 | case OpSpecConstantComposite: |
| 571 | case OpSpecConstantOp: |
| 572 | return true; |
| 573 | default: |
| 574 | return false; |
| 575 | } |
| 576 | } |
| 577 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 578 | Id Builder::makeBoolConstant(bool b, bool specConstant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 579 | { |
| 580 | Id typeId = makeBoolType(); |
| 581 | Instruction* constant; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 582 | Op opcode = specConstant ? (b ? OpSpecConstantTrue : OpSpecConstantFalse) : (b ? OpConstantTrue : OpConstantFalse); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 583 | |
| 584 | // See if we already made it |
| 585 | Id existing = 0; |
| 586 | for (int i = 0; i < (int)groupedConstants[OpTypeBool].size(); ++i) { |
| 587 | constant = groupedConstants[OpTypeBool][i]; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 588 | if (constant->getTypeId() == typeId && constant->getOpCode() == opcode) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 589 | existing = constant->getResultId(); |
| 590 | } |
| 591 | |
| 592 | if (existing) |
| 593 | return existing; |
| 594 | |
| 595 | // Make it |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 596 | Instruction* c = new Instruction(getUniqueId(), typeId, opcode); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 597 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 598 | groupedConstants[OpTypeBool].push_back(c); |
| 599 | module.mapInstruction(c); |
| 600 | |
| 601 | return c->getResultId(); |
| 602 | } |
| 603 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 604 | Id Builder::makeIntConstant(Id typeId, unsigned value, bool specConstant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 605 | { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 606 | Op opcode = specConstant ? OpSpecConstant : OpConstant; |
| 607 | Id existing = findScalarConstant(OpTypeInt, opcode, typeId, value); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 608 | if (existing) |
| 609 | return existing; |
| 610 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 611 | Instruction* c = new Instruction(getUniqueId(), typeId, opcode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 612 | c->addImmediateOperand(value); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 613 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 614 | groupedConstants[OpTypeInt].push_back(c); |
| 615 | module.mapInstruction(c); |
| 616 | |
| 617 | return c->getResultId(); |
| 618 | } |
| 619 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 620 | Id Builder::makeFloatConstant(float f, bool specConstant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 621 | { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 622 | Op opcode = specConstant ? OpSpecConstant : OpConstant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 623 | Id typeId = makeFloatType(32); |
| 624 | unsigned value = *(unsigned int*)&f; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 625 | Id existing = findScalarConstant(OpTypeFloat, opcode, typeId, value); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 626 | if (existing) |
| 627 | return existing; |
| 628 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 629 | Instruction* c = new Instruction(getUniqueId(), typeId, opcode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 630 | c->addImmediateOperand(value); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 631 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 632 | groupedConstants[OpTypeFloat].push_back(c); |
| 633 | module.mapInstruction(c); |
| 634 | |
| 635 | return c->getResultId(); |
| 636 | } |
| 637 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 638 | Id Builder::makeDoubleConstant(double d, bool specConstant) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 639 | { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 640 | Op opcode = specConstant ? OpSpecConstant : OpConstant; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 641 | Id typeId = makeFloatType(64); |
| 642 | unsigned long long value = *(unsigned long long*)&d; |
| 643 | unsigned op1 = value & 0xFFFFFFFF; |
| 644 | unsigned op2 = value >> 32; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 645 | Id existing = findScalarConstant(OpTypeFloat, opcode, typeId, op1, op2); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 646 | if (existing) |
| 647 | return existing; |
| 648 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 649 | Instruction* c = new Instruction(getUniqueId(), typeId, opcode); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 650 | c->addImmediateOperand(op1); |
| 651 | c->addImmediateOperand(op2); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 652 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 653 | groupedConstants[OpTypeFloat].push_back(c); |
| 654 | module.mapInstruction(c); |
| 655 | |
| 656 | return c->getResultId(); |
| 657 | } |
| 658 | |
| 659 | Id Builder::findCompositeConstant(Op typeClass, std::vector<Id>& comps) const |
| 660 | { |
| 661 | Instruction* constant = 0; |
| 662 | bool found = false; |
| 663 | for (int i = 0; i < (int)groupedConstants[typeClass].size(); ++i) { |
| 664 | constant = groupedConstants[typeClass][i]; |
| 665 | |
| 666 | // same shape? |
| 667 | if (constant->getNumOperands() != (int)comps.size()) |
| 668 | continue; |
| 669 | |
| 670 | // same contents? |
| 671 | bool mismatch = false; |
| 672 | for (int op = 0; op < constant->getNumOperands(); ++op) { |
| 673 | if (constant->getIdOperand(op) != comps[op]) { |
| 674 | mismatch = true; |
| 675 | break; |
| 676 | } |
| 677 | } |
| 678 | if (! mismatch) { |
| 679 | found = true; |
| 680 | break; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | return found ? constant->getResultId() : NoResult; |
| 685 | } |
| 686 | |
| 687 | // Comments in header |
| 688 | Id Builder::makeCompositeConstant(Id typeId, std::vector<Id>& members) |
| 689 | { |
| 690 | assert(typeId); |
| 691 | Op typeClass = getTypeClass(typeId); |
| 692 | |
| 693 | switch (typeClass) { |
| 694 | case OpTypeVector: |
| 695 | case OpTypeArray: |
| 696 | case OpTypeStruct: |
| 697 | case OpTypeMatrix: |
| 698 | break; |
| 699 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 700 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 701 | return makeFloatConstant(0.0); |
| 702 | } |
| 703 | |
| 704 | Id existing = findCompositeConstant(typeClass, members); |
| 705 | if (existing) |
| 706 | return existing; |
| 707 | |
| 708 | Instruction* c = new Instruction(getUniqueId(), typeId, OpConstantComposite); |
| 709 | for (int op = 0; op < (int)members.size(); ++op) |
| 710 | c->addIdOperand(members[op]); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 711 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 712 | groupedConstants[typeClass].push_back(c); |
| 713 | module.mapInstruction(c); |
| 714 | |
| 715 | return c->getResultId(); |
| 716 | } |
| 717 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 718 | Instruction* Builder::addEntryPoint(ExecutionModel model, Function* function, const char* name) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 719 | { |
| 720 | Instruction* entryPoint = new Instruction(OpEntryPoint); |
| 721 | entryPoint->addImmediateOperand(model); |
| 722 | entryPoint->addIdOperand(function->getId()); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 723 | entryPoint->addStringOperand(name); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 724 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 725 | entryPoints.push_back(std::unique_ptr<Instruction>(entryPoint)); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 726 | |
| 727 | return entryPoint; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 728 | } |
| 729 | |
John Kessenich | b56a26a | 2015-09-16 16:04:05 -0600 | [diff] [blame] | 730 | // Currently relying on the fact that all 'value' of interest are small non-negative values. |
| 731 | void Builder::addExecutionMode(Function* entryPoint, ExecutionMode mode, int value1, int value2, int value3) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 732 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 733 | Instruction* instr = new Instruction(OpExecutionMode); |
| 734 | instr->addIdOperand(entryPoint->getId()); |
| 735 | instr->addImmediateOperand(mode); |
John Kessenich | b56a26a | 2015-09-16 16:04:05 -0600 | [diff] [blame] | 736 | if (value1 >= 0) |
| 737 | instr->addImmediateOperand(value1); |
| 738 | if (value2 >= 0) |
| 739 | instr->addImmediateOperand(value2); |
| 740 | if (value3 >= 0) |
| 741 | instr->addImmediateOperand(value3); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 742 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 743 | executionModes.push_back(std::unique_ptr<Instruction>(instr)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | void Builder::addName(Id id, const char* string) |
| 747 | { |
| 748 | Instruction* name = new Instruction(OpName); |
| 749 | name->addIdOperand(id); |
| 750 | name->addStringOperand(string); |
| 751 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 752 | names.push_back(std::unique_ptr<Instruction>(name)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | void Builder::addMemberName(Id id, int memberNumber, const char* string) |
| 756 | { |
| 757 | Instruction* name = new Instruction(OpMemberName); |
| 758 | name->addIdOperand(id); |
| 759 | name->addImmediateOperand(memberNumber); |
| 760 | name->addStringOperand(string); |
| 761 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 762 | names.push_back(std::unique_ptr<Instruction>(name)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | void Builder::addLine(Id target, Id fileName, int lineNum, int column) |
| 766 | { |
| 767 | Instruction* line = new Instruction(OpLine); |
| 768 | line->addIdOperand(target); |
| 769 | line->addIdOperand(fileName); |
| 770 | line->addImmediateOperand(lineNum); |
| 771 | line->addImmediateOperand(column); |
| 772 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 773 | lines.push_back(std::unique_ptr<Instruction>(line)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | void Builder::addDecoration(Id id, Decoration decoration, int num) |
| 777 | { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 778 | if (decoration == (spv::Decoration)spv::BadValue) |
| 779 | return; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 780 | Instruction* dec = new Instruction(OpDecorate); |
| 781 | dec->addIdOperand(id); |
| 782 | dec->addImmediateOperand(decoration); |
| 783 | if (num >= 0) |
| 784 | dec->addImmediateOperand(num); |
| 785 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 786 | decorations.push_back(std::unique_ptr<Instruction>(dec)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | void Builder::addMemberDecoration(Id id, unsigned int member, Decoration decoration, int num) |
| 790 | { |
| 791 | Instruction* dec = new Instruction(OpMemberDecorate); |
| 792 | dec->addIdOperand(id); |
| 793 | dec->addImmediateOperand(member); |
| 794 | dec->addImmediateOperand(decoration); |
| 795 | if (num >= 0) |
| 796 | dec->addImmediateOperand(num); |
| 797 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 798 | decorations.push_back(std::unique_ptr<Instruction>(dec)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | // Comments in header |
| 802 | Function* Builder::makeMain() |
| 803 | { |
| 804 | assert(! mainFunction); |
| 805 | |
| 806 | Block* entry; |
| 807 | std::vector<Id> params; |
| 808 | |
| 809 | mainFunction = makeFunctionEntry(makeVoidType(), "main", params, &entry); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 810 | |
| 811 | return mainFunction; |
| 812 | } |
| 813 | |
| 814 | // Comments in header |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 815 | Function* Builder::makeFunctionEntry(Id returnType, const char* name, std::vector<Id>& paramTypes, Block **entry) |
| 816 | { |
| 817 | Id typeId = makeFunctionType(returnType, paramTypes); |
baldurk | d76692d | 2015-07-12 11:32:58 +0200 | [diff] [blame] | 818 | Id firstParamId = paramTypes.size() == 0 ? 0 : getUniqueIds((int)paramTypes.size()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 819 | Function* function = new Function(getUniqueId(), returnType, typeId, firstParamId, module); |
| 820 | |
| 821 | if (entry) { |
| 822 | *entry = new Block(getUniqueId(), *function); |
| 823 | function->addBlock(*entry); |
| 824 | setBuildPoint(*entry); |
| 825 | } |
| 826 | |
| 827 | if (name) |
| 828 | addName(function->getId(), name); |
| 829 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 830 | functions.push_back(std::unique_ptr<Function>(function)); |
| 831 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 832 | return function; |
| 833 | } |
| 834 | |
| 835 | // Comments in header |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 836 | void Builder::makeReturn(bool implicit, Id retVal) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 837 | { |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 838 | if (retVal) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 839 | Instruction* inst = new Instruction(NoResult, NoType, OpReturnValue); |
| 840 | inst->addIdOperand(retVal); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 841 | buildPoint->addInstruction(std::unique_ptr<Instruction>(inst)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 842 | } else |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 843 | buildPoint->addInstruction(std::unique_ptr<Instruction>(new Instruction(NoResult, NoType, OpReturn))); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 844 | |
| 845 | if (! implicit) |
| 846 | createAndSetNoPredecessorBlock("post-return"); |
| 847 | } |
| 848 | |
| 849 | // Comments in header |
John Kessenich | e770b3e | 2015-09-14 20:58:02 -0600 | [diff] [blame] | 850 | void Builder::leaveFunction() |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 851 | { |
| 852 | Block* block = buildPoint; |
| 853 | Function& function = buildPoint->getParent(); |
| 854 | assert(block); |
| 855 | |
| 856 | // If our function did not contain a return, add a return void now. |
| 857 | if (! block->isTerminated()) { |
Dejan Mircevski | ed55bcd | 2016-01-19 21:13:38 -0500 | [diff] [blame] | 858 | if (function.getReturnType() == makeVoidType()) |
| 859 | makeReturn(true); |
| 860 | else { |
| 861 | makeReturn(true, createUndefined(function.getReturnType())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 862 | } |
| 863 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | // Comments in header |
| 867 | void Builder::makeDiscard() |
| 868 | { |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 869 | buildPoint->addInstruction(std::unique_ptr<Instruction>(new Instruction(OpKill))); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 870 | createAndSetNoPredecessorBlock("post-discard"); |
| 871 | } |
| 872 | |
| 873 | // Comments in header |
| 874 | Id Builder::createVariable(StorageClass storageClass, Id type, const char* name) |
| 875 | { |
| 876 | Id pointerType = makePointer(storageClass, type); |
| 877 | Instruction* inst = new Instruction(getUniqueId(), pointerType, OpVariable); |
| 878 | inst->addImmediateOperand(storageClass); |
| 879 | |
| 880 | switch (storageClass) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 881 | case StorageClassFunction: |
| 882 | // Validation rules require the declaration in the entry block |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 883 | buildPoint->getParent().addLocalVariable(std::unique_ptr<Instruction>(inst)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 884 | break; |
| 885 | |
| 886 | default: |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 887 | constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(inst)); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 888 | module.mapInstruction(inst); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 889 | break; |
| 890 | } |
| 891 | |
| 892 | if (name) |
| 893 | addName(inst->getResultId(), name); |
| 894 | |
| 895 | return inst->getResultId(); |
| 896 | } |
| 897 | |
| 898 | // Comments in header |
Miro Knejp | 28f9b1c | 2015-08-11 02:45:24 +0200 | [diff] [blame] | 899 | Id Builder::createUndefined(Id type) |
| 900 | { |
| 901 | Instruction* inst = new Instruction(getUniqueId(), type, OpUndef); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 902 | buildPoint->addInstruction(std::unique_ptr<Instruction>(inst)); |
Miro Knejp | 28f9b1c | 2015-08-11 02:45:24 +0200 | [diff] [blame] | 903 | return inst->getResultId(); |
| 904 | } |
| 905 | |
| 906 | // Comments in header |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 907 | void Builder::createStore(Id rValue, Id lValue) |
| 908 | { |
| 909 | Instruction* store = new Instruction(OpStore); |
| 910 | store->addIdOperand(lValue); |
| 911 | store->addIdOperand(rValue); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 912 | buildPoint->addInstruction(std::unique_ptr<Instruction>(store)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | // Comments in header |
| 916 | Id Builder::createLoad(Id lValue) |
| 917 | { |
| 918 | Instruction* load = new Instruction(getUniqueId(), getDerefTypeId(lValue), OpLoad); |
| 919 | load->addIdOperand(lValue); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 920 | buildPoint->addInstruction(std::unique_ptr<Instruction>(load)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 921 | |
| 922 | return load->getResultId(); |
| 923 | } |
| 924 | |
| 925 | // Comments in header |
| 926 | Id Builder::createAccessChain(StorageClass storageClass, Id base, std::vector<Id>& offsets) |
| 927 | { |
| 928 | // Figure out the final resulting type. |
| 929 | spv::Id typeId = getTypeId(base); |
| 930 | assert(isPointerType(typeId) && offsets.size() > 0); |
| 931 | typeId = getContainedTypeId(typeId); |
| 932 | for (int i = 0; i < (int)offsets.size(); ++i) { |
| 933 | if (isStructType(typeId)) { |
| 934 | assert(isConstantScalar(offsets[i])); |
| 935 | typeId = getContainedTypeId(typeId, getConstantScalar(offsets[i])); |
| 936 | } else |
| 937 | typeId = getContainedTypeId(typeId, offsets[i]); |
| 938 | } |
| 939 | typeId = makePointer(storageClass, typeId); |
| 940 | |
| 941 | // Make the instruction |
| 942 | Instruction* chain = new Instruction(getUniqueId(), typeId, OpAccessChain); |
| 943 | chain->addIdOperand(base); |
| 944 | for (int i = 0; i < (int)offsets.size(); ++i) |
| 945 | chain->addIdOperand(offsets[i]); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 946 | buildPoint->addInstruction(std::unique_ptr<Instruction>(chain)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 947 | |
| 948 | return chain->getResultId(); |
| 949 | } |
| 950 | |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 951 | Id Builder::createArrayLength(Id base, unsigned int member) |
| 952 | { |
| 953 | Instruction* length = new Instruction(getUniqueId(), makeIntType(32), OpArrayLength); |
| 954 | length->addIdOperand(base); |
| 955 | length->addImmediateOperand(member); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 956 | buildPoint->addInstruction(std::unique_ptr<Instruction>(length)); |
John Kessenich | ee21fc9 | 2015-09-21 21:50:29 -0600 | [diff] [blame] | 957 | |
| 958 | return length->getResultId(); |
| 959 | } |
| 960 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 961 | Id Builder::createCompositeExtract(Id composite, Id typeId, unsigned index) |
| 962 | { |
| 963 | Instruction* extract = new Instruction(getUniqueId(), typeId, OpCompositeExtract); |
| 964 | extract->addIdOperand(composite); |
| 965 | extract->addImmediateOperand(index); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 966 | buildPoint->addInstruction(std::unique_ptr<Instruction>(extract)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 967 | |
| 968 | return extract->getResultId(); |
| 969 | } |
| 970 | |
| 971 | Id Builder::createCompositeExtract(Id composite, Id typeId, std::vector<unsigned>& indexes) |
| 972 | { |
| 973 | Instruction* extract = new Instruction(getUniqueId(), typeId, OpCompositeExtract); |
| 974 | extract->addIdOperand(composite); |
| 975 | for (int i = 0; i < (int)indexes.size(); ++i) |
| 976 | extract->addImmediateOperand(indexes[i]); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 977 | buildPoint->addInstruction(std::unique_ptr<Instruction>(extract)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 978 | |
| 979 | return extract->getResultId(); |
| 980 | } |
| 981 | |
| 982 | Id Builder::createCompositeInsert(Id object, Id composite, Id typeId, unsigned index) |
| 983 | { |
| 984 | Instruction* insert = new Instruction(getUniqueId(), typeId, OpCompositeInsert); |
| 985 | insert->addIdOperand(object); |
| 986 | insert->addIdOperand(composite); |
| 987 | insert->addImmediateOperand(index); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 988 | buildPoint->addInstruction(std::unique_ptr<Instruction>(insert)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 989 | |
| 990 | return insert->getResultId(); |
| 991 | } |
| 992 | |
| 993 | Id Builder::createCompositeInsert(Id object, Id composite, Id typeId, std::vector<unsigned>& indexes) |
| 994 | { |
| 995 | Instruction* insert = new Instruction(getUniqueId(), typeId, OpCompositeInsert); |
| 996 | insert->addIdOperand(object); |
| 997 | insert->addIdOperand(composite); |
| 998 | for (int i = 0; i < (int)indexes.size(); ++i) |
| 999 | insert->addImmediateOperand(indexes[i]); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1000 | buildPoint->addInstruction(std::unique_ptr<Instruction>(insert)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1001 | |
| 1002 | return insert->getResultId(); |
| 1003 | } |
| 1004 | |
| 1005 | Id Builder::createVectorExtractDynamic(Id vector, Id typeId, Id componentIndex) |
| 1006 | { |
| 1007 | Instruction* extract = new Instruction(getUniqueId(), typeId, OpVectorExtractDynamic); |
| 1008 | extract->addIdOperand(vector); |
| 1009 | extract->addIdOperand(componentIndex); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1010 | buildPoint->addInstruction(std::unique_ptr<Instruction>(extract)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1011 | |
| 1012 | return extract->getResultId(); |
| 1013 | } |
| 1014 | |
| 1015 | Id Builder::createVectorInsertDynamic(Id vector, Id typeId, Id component, Id componentIndex) |
| 1016 | { |
| 1017 | Instruction* insert = new Instruction(getUniqueId(), typeId, OpVectorInsertDynamic); |
| 1018 | insert->addIdOperand(vector); |
| 1019 | insert->addIdOperand(component); |
| 1020 | insert->addIdOperand(componentIndex); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1021 | buildPoint->addInstruction(std::unique_ptr<Instruction>(insert)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1022 | |
| 1023 | return insert->getResultId(); |
| 1024 | } |
| 1025 | |
| 1026 | // An opcode that has no operands, no result id, and no type |
| 1027 | void Builder::createNoResultOp(Op opCode) |
| 1028 | { |
| 1029 | Instruction* op = new Instruction(opCode); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1030 | buildPoint->addInstruction(std::unique_ptr<Instruction>(op)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | // An opcode that has one operand, no result id, and no type |
| 1034 | void Builder::createNoResultOp(Op opCode, Id operand) |
| 1035 | { |
| 1036 | Instruction* op = new Instruction(opCode); |
| 1037 | op->addIdOperand(operand); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1038 | buildPoint->addInstruction(std::unique_ptr<Instruction>(op)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1039 | } |
| 1040 | |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1041 | // An opcode that has one operand, no result id, and no type |
| 1042 | void Builder::createNoResultOp(Op opCode, const std::vector<Id>& operands) |
| 1043 | { |
| 1044 | Instruction* op = new Instruction(opCode); |
| 1045 | for (auto operand : operands) |
| 1046 | op->addIdOperand(operand); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1047 | buildPoint->addInstruction(std::unique_ptr<Instruction>(op)); |
Rex Xu | fc61891 | 2015-09-09 16:42:49 +0800 | [diff] [blame] | 1048 | } |
| 1049 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1050 | void Builder::createControlBarrier(Scope execution, Scope memory, MemorySemanticsMask semantics) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1051 | { |
| 1052 | Instruction* op = new Instruction(OpControlBarrier); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1053 | op->addImmediateOperand(makeUintConstant(execution)); |
| 1054 | op->addImmediateOperand(makeUintConstant(memory)); |
| 1055 | op->addImmediateOperand(makeUintConstant(semantics)); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1056 | buildPoint->addInstruction(std::unique_ptr<Instruction>(op)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | void Builder::createMemoryBarrier(unsigned executionScope, unsigned memorySemantics) |
| 1060 | { |
| 1061 | Instruction* op = new Instruction(OpMemoryBarrier); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1062 | op->addImmediateOperand(makeUintConstant(executionScope)); |
| 1063 | op->addImmediateOperand(makeUintConstant(memorySemantics)); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1064 | buildPoint->addInstruction(std::unique_ptr<Instruction>(op)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | // An opcode that has one operands, a result id, and a type |
| 1068 | Id Builder::createUnaryOp(Op opCode, Id typeId, Id operand) |
| 1069 | { |
| 1070 | Instruction* op = new Instruction(getUniqueId(), typeId, opCode); |
| 1071 | op->addIdOperand(operand); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1072 | buildPoint->addInstruction(std::unique_ptr<Instruction>(op)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1073 | |
| 1074 | return op->getResultId(); |
| 1075 | } |
| 1076 | |
| 1077 | Id Builder::createBinOp(Op opCode, Id typeId, Id left, Id right) |
| 1078 | { |
| 1079 | Instruction* op = new Instruction(getUniqueId(), typeId, opCode); |
| 1080 | op->addIdOperand(left); |
| 1081 | op->addIdOperand(right); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1082 | buildPoint->addInstruction(std::unique_ptr<Instruction>(op)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1083 | |
| 1084 | return op->getResultId(); |
| 1085 | } |
| 1086 | |
| 1087 | Id Builder::createTriOp(Op opCode, Id typeId, Id op1, Id op2, Id op3) |
| 1088 | { |
| 1089 | Instruction* op = new Instruction(getUniqueId(), typeId, opCode); |
| 1090 | op->addIdOperand(op1); |
| 1091 | op->addIdOperand(op2); |
| 1092 | op->addIdOperand(op3); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1093 | buildPoint->addInstruction(std::unique_ptr<Instruction>(op)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1094 | |
| 1095 | return op->getResultId(); |
| 1096 | } |
| 1097 | |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1098 | Id Builder::createOp(Op opCode, Id typeId, const std::vector<Id>& operands) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1099 | { |
| 1100 | Instruction* op = new Instruction(getUniqueId(), typeId, opCode); |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 1101 | for (auto operand : operands) |
| 1102 | op->addIdOperand(operand); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1103 | buildPoint->addInstruction(std::unique_ptr<Instruction>(op)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1104 | |
| 1105 | return op->getResultId(); |
| 1106 | } |
| 1107 | |
| 1108 | Id Builder::createFunctionCall(spv::Function* function, std::vector<spv::Id>& args) |
| 1109 | { |
| 1110 | Instruction* op = new Instruction(getUniqueId(), function->getReturnType(), OpFunctionCall); |
| 1111 | op->addIdOperand(function->getId()); |
| 1112 | for (int a = 0; a < (int)args.size(); ++a) |
| 1113 | op->addIdOperand(args[a]); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1114 | buildPoint->addInstruction(std::unique_ptr<Instruction>(op)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1115 | |
| 1116 | return op->getResultId(); |
| 1117 | } |
| 1118 | |
| 1119 | // Comments in header |
| 1120 | Id Builder::createRvalueSwizzle(Id typeId, Id source, std::vector<unsigned>& channels) |
| 1121 | { |
| 1122 | if (channels.size() == 1) |
| 1123 | return createCompositeExtract(source, typeId, channels.front()); |
| 1124 | |
| 1125 | Instruction* swizzle = new Instruction(getUniqueId(), typeId, OpVectorShuffle); |
| 1126 | assert(isVector(source)); |
| 1127 | swizzle->addIdOperand(source); |
| 1128 | swizzle->addIdOperand(source); |
| 1129 | for (int i = 0; i < (int)channels.size(); ++i) |
| 1130 | swizzle->addImmediateOperand(channels[i]); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1131 | buildPoint->addInstruction(std::unique_ptr<Instruction>(swizzle)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1132 | |
| 1133 | return swizzle->getResultId(); |
| 1134 | } |
| 1135 | |
| 1136 | // Comments in header |
| 1137 | Id Builder::createLvalueSwizzle(Id typeId, Id target, Id source, std::vector<unsigned>& channels) |
| 1138 | { |
John Kessenich | 3066953 | 2015-08-06 22:02:24 -0600 | [diff] [blame] | 1139 | assert(getNumComponents(source) == (int)channels.size()); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1140 | if (channels.size() == 1 && getNumComponents(source) == 1) |
| 1141 | return createCompositeInsert(source, target, typeId, channels.front()); |
| 1142 | |
| 1143 | Instruction* swizzle = new Instruction(getUniqueId(), typeId, OpVectorShuffle); |
| 1144 | assert(isVector(source)); |
| 1145 | assert(isVector(target)); |
| 1146 | swizzle->addIdOperand(target); |
| 1147 | swizzle->addIdOperand(source); |
| 1148 | |
| 1149 | // Set up an identity shuffle from the base value to the result value |
| 1150 | unsigned int components[4]; |
| 1151 | int numTargetComponents = getNumComponents(target); |
| 1152 | for (int i = 0; i < numTargetComponents; ++i) |
| 1153 | components[i] = i; |
| 1154 | |
| 1155 | // Punch in the l-value swizzle |
| 1156 | for (int i = 0; i < (int)channels.size(); ++i) |
| 1157 | components[channels[i]] = numTargetComponents + i; |
| 1158 | |
| 1159 | // finish the instruction with these components selectors |
| 1160 | for (int i = 0; i < numTargetComponents; ++i) |
| 1161 | swizzle->addImmediateOperand(components[i]); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1162 | buildPoint->addInstruction(std::unique_ptr<Instruction>(swizzle)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1163 | |
| 1164 | return swizzle->getResultId(); |
| 1165 | } |
| 1166 | |
| 1167 | // Comments in header |
| 1168 | void Builder::promoteScalar(Decoration precision, Id& left, Id& right) |
| 1169 | { |
| 1170 | int direction = getNumComponents(right) - getNumComponents(left); |
| 1171 | |
| 1172 | if (direction > 0) |
John Kessenich | 76f7139 | 2015-12-09 19:08:42 -0700 | [diff] [blame] | 1173 | left = smearScalar(precision, left, makeVectorType(getTypeId(left), getNumComponents(right))); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1174 | else if (direction < 0) |
John Kessenich | 76f7139 | 2015-12-09 19:08:42 -0700 | [diff] [blame] | 1175 | right = smearScalar(precision, right, makeVectorType(getTypeId(right), getNumComponents(left))); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1176 | |
| 1177 | return; |
| 1178 | } |
| 1179 | |
| 1180 | // Comments in header |
| 1181 | Id Builder::smearScalar(Decoration /*precision*/, Id scalar, Id vectorType) |
| 1182 | { |
| 1183 | assert(getNumComponents(scalar) == 1); |
John Kessenich | 76f7139 | 2015-12-09 19:08:42 -0700 | [diff] [blame] | 1184 | assert(getTypeId(scalar) == getScalarTypeId(vectorType)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1185 | |
| 1186 | int numComponents = getNumTypeComponents(vectorType); |
| 1187 | if (numComponents == 1) |
| 1188 | return scalar; |
| 1189 | |
| 1190 | Instruction* smear = new Instruction(getUniqueId(), vectorType, OpCompositeConstruct); |
| 1191 | for (int c = 0; c < numComponents; ++c) |
| 1192 | smear->addIdOperand(scalar); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1193 | buildPoint->addInstruction(std::unique_ptr<Instruction>(smear)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1194 | |
| 1195 | return smear->getResultId(); |
| 1196 | } |
| 1197 | |
| 1198 | // Comments in header |
| 1199 | Id Builder::createBuiltinCall(Decoration /*precision*/, Id resultType, Id builtins, int entryPoint, std::vector<Id>& args) |
| 1200 | { |
| 1201 | Instruction* inst = new Instruction(getUniqueId(), resultType, OpExtInst); |
| 1202 | inst->addIdOperand(builtins); |
| 1203 | inst->addImmediateOperand(entryPoint); |
| 1204 | for (int arg = 0; arg < (int)args.size(); ++arg) |
| 1205 | inst->addIdOperand(args[arg]); |
| 1206 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1207 | buildPoint->addInstruction(std::unique_ptr<Instruction>(inst)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1208 | return inst->getResultId(); |
| 1209 | } |
| 1210 | |
| 1211 | // Accept all parameters needed to create a texture instruction. |
| 1212 | // Create the correct instruction based on the inputs, and make the call. |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 1213 | Id Builder::createTextureCall(Decoration precision, Id resultType, bool sparse, bool fetch, bool proj, bool gather, const TextureParameters& parameters) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1214 | { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1215 | static const int maxTextureArgs = 10; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1216 | Id texArgs[maxTextureArgs] = {}; |
| 1217 | |
| 1218 | // |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1219 | // Set up the fixed arguments |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1220 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1221 | int numArgs = 0; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1222 | bool xplicit = false; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1223 | texArgs[numArgs++] = parameters.sampler; |
| 1224 | texArgs[numArgs++] = parameters.coords; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1225 | if (parameters.Dref) |
| 1226 | texArgs[numArgs++] = parameters.Dref; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1227 | if (parameters.comp) |
| 1228 | texArgs[numArgs++] = parameters.comp; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1229 | |
| 1230 | // |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1231 | // Set up the optional arguments |
| 1232 | // |
| 1233 | int optArgNum = numArgs; // track which operand, if it exists, is the mask of optional arguments |
| 1234 | ++numArgs; // speculatively make room for the mask operand |
| 1235 | ImageOperandsMask mask = ImageOperandsMaskNone; // the mask operand |
| 1236 | if (parameters.bias) { |
| 1237 | mask = (ImageOperandsMask)(mask | ImageOperandsBiasMask); |
| 1238 | texArgs[numArgs++] = parameters.bias; |
| 1239 | } |
| 1240 | if (parameters.lod) { |
| 1241 | mask = (ImageOperandsMask)(mask | ImageOperandsLodMask); |
| 1242 | texArgs[numArgs++] = parameters.lod; |
| 1243 | xplicit = true; |
| 1244 | } |
| 1245 | if (parameters.gradX) { |
| 1246 | mask = (ImageOperandsMask)(mask | ImageOperandsGradMask); |
| 1247 | texArgs[numArgs++] = parameters.gradX; |
| 1248 | texArgs[numArgs++] = parameters.gradY; |
| 1249 | xplicit = true; |
| 1250 | } |
| 1251 | if (parameters.offset) { |
Rex Xu | 86e6081 | 2015-10-11 19:37:48 +0800 | [diff] [blame] | 1252 | if (isConstant(parameters.offset)) |
| 1253 | mask = (ImageOperandsMask)(mask | ImageOperandsConstOffsetMask); |
| 1254 | else |
| 1255 | mask = (ImageOperandsMask)(mask | ImageOperandsOffsetMask); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1256 | texArgs[numArgs++] = parameters.offset; |
| 1257 | } |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1258 | if (parameters.offsets) { |
| 1259 | mask = (ImageOperandsMask)(mask | ImageOperandsConstOffsetsMask); |
| 1260 | texArgs[numArgs++] = parameters.offsets; |
| 1261 | } |
| 1262 | if (parameters.sample) { |
| 1263 | mask = (ImageOperandsMask)(mask | ImageOperandsSampleMask); |
| 1264 | texArgs[numArgs++] = parameters.sample; |
| 1265 | } |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 1266 | if (parameters.lodClamp) { |
| 1267 | mask = (ImageOperandsMask)(mask | ImageOperandsMinLodMask); |
| 1268 | texArgs[numArgs++] = parameters.lodClamp; |
| 1269 | } |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1270 | if (mask == ImageOperandsMaskNone) |
| 1271 | --numArgs; // undo speculative reservation for the mask argument |
| 1272 | else |
| 1273 | texArgs[optArgNum] = mask; |
| 1274 | |
| 1275 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1276 | // Set up the instruction |
| 1277 | // |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1278 | Op opCode; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1279 | opCode = OpImageSampleImplicitLod; |
Jason Ekstrand | 18b9fbd | 2015-09-05 14:14:48 -0700 | [diff] [blame] | 1280 | if (fetch) { |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 1281 | if (sparse) |
| 1282 | opCode = OpImageSparseFetch; |
| 1283 | else |
| 1284 | opCode = OpImageFetch; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1285 | } else if (gather) { |
| 1286 | if (parameters.Dref) |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 1287 | if (sparse) |
| 1288 | opCode = OpImageSparseDrefGather; |
| 1289 | else |
| 1290 | opCode = OpImageDrefGather; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1291 | else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 1292 | if (sparse) |
| 1293 | opCode = OpImageSparseGather; |
| 1294 | else |
| 1295 | opCode = OpImageGather; |
Jason Ekstrand | 18b9fbd | 2015-09-05 14:14:48 -0700 | [diff] [blame] | 1296 | } else if (xplicit) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1297 | if (parameters.Dref) { |
| 1298 | if (proj) |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 1299 | if (sparse) |
| 1300 | opCode = OpImageSparseSampleProjDrefExplicitLod; |
| 1301 | else |
| 1302 | opCode = OpImageSampleProjDrefExplicitLod; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1303 | else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 1304 | if (sparse) |
| 1305 | opCode = OpImageSparseSampleDrefExplicitLod; |
| 1306 | else |
| 1307 | opCode = OpImageSampleDrefExplicitLod; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1308 | } else { |
| 1309 | if (proj) |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 1310 | if (sparse) |
| 1311 | opCode = OpImageSparseSampleProjExplicitLod; |
| 1312 | else |
| 1313 | opCode = OpImageSampleProjExplicitLod; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1314 | else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 1315 | if (sparse) |
| 1316 | opCode = OpImageSparseSampleExplicitLod; |
| 1317 | else |
| 1318 | opCode = OpImageSampleExplicitLod; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1319 | } |
| 1320 | } else { |
| 1321 | if (parameters.Dref) { |
| 1322 | if (proj) |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 1323 | if (sparse) |
| 1324 | opCode = OpImageSparseSampleProjDrefImplicitLod; |
| 1325 | else |
| 1326 | opCode = OpImageSampleProjDrefImplicitLod; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1327 | else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 1328 | if (sparse) |
| 1329 | opCode = OpImageSparseSampleDrefImplicitLod; |
| 1330 | else |
| 1331 | opCode = OpImageSampleDrefImplicitLod; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1332 | } else { |
| 1333 | if (proj) |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 1334 | if (sparse) |
| 1335 | opCode = OpImageSparseSampleProjImplicitLod; |
| 1336 | else |
| 1337 | opCode = OpImageSampleProjImplicitLod; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1338 | else |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 1339 | if (sparse) |
| 1340 | opCode = OpImageSparseSampleImplicitLod; |
| 1341 | else |
| 1342 | opCode = OpImageSampleImplicitLod; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1343 | } |
| 1344 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1345 | |
John Kessenich | 7355eeb | 2015-09-14 22:08:12 -0600 | [diff] [blame] | 1346 | // See if the result type is expecting a smeared result. |
| 1347 | // This happens when a legacy shadow*() call is made, which |
| 1348 | // gets a vec4 back instead of a float. |
| 1349 | Id smearedType = resultType; |
| 1350 | if (! isScalarType(resultType)) { |
| 1351 | switch (opCode) { |
| 1352 | case OpImageSampleDrefImplicitLod: |
| 1353 | case OpImageSampleDrefExplicitLod: |
| 1354 | case OpImageSampleProjDrefImplicitLod: |
| 1355 | case OpImageSampleProjDrefExplicitLod: |
| 1356 | resultType = getScalarTypeId(resultType); |
| 1357 | break; |
| 1358 | default: |
| 1359 | break; |
| 1360 | } |
| 1361 | } |
| 1362 | |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 1363 | Id typeId0 = 0; |
| 1364 | Id typeId1 = 0; |
| 1365 | |
| 1366 | if (sparse) { |
| 1367 | typeId0 = resultType; |
| 1368 | typeId1 = getDerefTypeId(parameters.texelOut); |
| 1369 | resultType = makeStructResultType(typeId0, typeId1); |
| 1370 | } |
| 1371 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1372 | // Build the SPIR-V instruction |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1373 | Instruction* textureInst = new Instruction(getUniqueId(), resultType, opCode); |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1374 | for (int op = 0; op < optArgNum; ++op) |
| 1375 | textureInst->addIdOperand(texArgs[op]); |
| 1376 | if (optArgNum < numArgs) |
| 1377 | textureInst->addImmediateOperand(texArgs[optArgNum]); |
| 1378 | for (int op = optArgNum + 1; op < numArgs; ++op) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1379 | textureInst->addIdOperand(texArgs[op]); |
| 1380 | setPrecision(textureInst->getResultId(), precision); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1381 | buildPoint->addInstruction(std::unique_ptr<Instruction>(textureInst)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1382 | |
John Kessenich | 7355eeb | 2015-09-14 22:08:12 -0600 | [diff] [blame] | 1383 | Id resultId = textureInst->getResultId(); |
| 1384 | |
Rex Xu | 48edadf | 2015-12-31 16:11:41 +0800 | [diff] [blame] | 1385 | if (sparse) { |
| 1386 | // Decode the return type that was a special structure |
| 1387 | createStore(createCompositeExtract(resultId, typeId1, 1), parameters.texelOut); |
| 1388 | resultId = createCompositeExtract(resultId, typeId0, 0); |
| 1389 | } else { |
| 1390 | // When a smear is needed, do it, as per what was computed |
| 1391 | // above when resultType was changed to a scalar type. |
| 1392 | if (resultType != smearedType) |
| 1393 | resultId = smearScalar(precision, resultId, smearedType); |
| 1394 | } |
John Kessenich | 7355eeb | 2015-09-14 22:08:12 -0600 | [diff] [blame] | 1395 | |
| 1396 | return resultId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | // Comments in header |
| 1400 | Id Builder::createTextureQueryCall(Op opCode, const TextureParameters& parameters) |
| 1401 | { |
| 1402 | // Figure out the result type |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1403 | Id resultType = 0; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1404 | switch (opCode) { |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1405 | case OpImageQuerySize: |
| 1406 | case OpImageQuerySizeLod: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1407 | { |
Mark Adams | 364c21c | 2016-01-06 13:41:02 -0500 | [diff] [blame] | 1408 | int numComponents = 0; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1409 | switch (getTypeDimensionality(getImageType(parameters.sampler))) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1410 | case Dim1D: |
| 1411 | case DimBuffer: |
| 1412 | numComponents = 1; |
| 1413 | break; |
| 1414 | case Dim2D: |
| 1415 | case DimCube: |
| 1416 | case DimRect: |
John Kessenich | 50e5756 | 2015-12-21 21:21:11 -0700 | [diff] [blame] | 1417 | case DimSubpassData: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1418 | numComponents = 2; |
| 1419 | break; |
| 1420 | case Dim3D: |
| 1421 | numComponents = 3; |
| 1422 | break; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1423 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1424 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1425 | assert(0); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1426 | break; |
| 1427 | } |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1428 | if (isArrayedImageType(getImageType(parameters.sampler))) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1429 | ++numComponents; |
| 1430 | if (numComponents == 1) |
| 1431 | resultType = makeIntType(32); |
| 1432 | else |
| 1433 | resultType = makeVectorType(makeIntType(32), numComponents); |
| 1434 | |
| 1435 | break; |
| 1436 | } |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1437 | case OpImageQueryLod: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1438 | resultType = makeVectorType(makeFloatType(32), 2); |
| 1439 | break; |
John Kessenich | 5e4b124 | 2015-08-06 22:53:06 -0600 | [diff] [blame] | 1440 | case OpImageQueryLevels: |
| 1441 | case OpImageQuerySamples: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1442 | resultType = makeIntType(32); |
| 1443 | break; |
| 1444 | default: |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1445 | assert(0); |
| 1446 | break; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | Instruction* query = new Instruction(getUniqueId(), resultType, opCode); |
| 1450 | query->addIdOperand(parameters.sampler); |
| 1451 | if (parameters.coords) |
| 1452 | query->addIdOperand(parameters.coords); |
| 1453 | if (parameters.lod) |
| 1454 | query->addIdOperand(parameters.lod); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1455 | buildPoint->addInstruction(std::unique_ptr<Instruction>(query)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1456 | |
| 1457 | return query->getResultId(); |
| 1458 | } |
| 1459 | |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 1460 | // External comments in header. |
| 1461 | // Operates recursively to visit the composite's hierarchy. |
| 1462 | Id Builder::createCompositeCompare(Decoration precision, Id value1, Id value2, bool equal) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1463 | { |
| 1464 | Id boolType = makeBoolType(); |
| 1465 | Id valueType = getTypeId(value1); |
| 1466 | |
Mark Adams | d5ac538 | 2016-02-01 19:13:06 -0800 | [diff] [blame^] | 1467 | Id resultId = NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1468 | |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 1469 | int numConstituents = getNumTypeConstituents(valueType); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1470 | |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 1471 | // Scalars and Vectors |
| 1472 | |
| 1473 | if (isScalarType(valueType) || isVectorType(valueType)) { |
John Kessenich | e23c984 | 2016-01-04 23:49:03 -0700 | [diff] [blame] | 1474 | assert(valueType == getTypeId(value2)); |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 1475 | // These just need a single comparison, just have |
| 1476 | // to figure out what it is. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1477 | Op op; |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 1478 | switch (getMostBasicTypeClass(valueType)) { |
| 1479 | case OpTypeFloat: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1480 | op = equal ? OpFOrdEqual : OpFOrdNotEqual; |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 1481 | break; |
| 1482 | case OpTypeInt: |
Mark Adams | d5ac538 | 2016-02-01 19:13:06 -0800 | [diff] [blame^] | 1483 | default: |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1484 | op = equal ? OpIEqual : OpINotEqual; |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 1485 | break; |
| 1486 | case OpTypeBool: |
| 1487 | op = equal ? OpLogicalEqual : OpLogicalNotEqual; |
| 1488 | precision = NoPrecision; |
| 1489 | break; |
| 1490 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1491 | |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 1492 | if (isScalarType(valueType)) { |
| 1493 | // scalar |
| 1494 | resultId = createBinOp(op, boolType, value1, value2); |
| 1495 | setPrecision(resultId, precision); |
| 1496 | } else { |
| 1497 | // vector |
| 1498 | resultId = createBinOp(op, makeVectorType(boolType, numConstituents), value1, value2); |
| 1499 | setPrecision(resultId, precision); |
| 1500 | // reduce vector compares... |
| 1501 | resultId = createUnaryOp(equal ? OpAll : OpAny, boolType, resultId); |
| 1502 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1503 | |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 1504 | return resultId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1505 | } |
| 1506 | |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 1507 | // Only structs, arrays, and matrices should be left. |
| 1508 | // They share in common the reduction operation across their constituents. |
| 1509 | assert(isAggregateType(valueType) || isMatrixType(valueType)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1510 | |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 1511 | // Compare each pair of constituents |
| 1512 | for (int constituent = 0; constituent < numConstituents; ++constituent) { |
| 1513 | std::vector<unsigned> indexes(1, constituent); |
John Kessenich | e23c984 | 2016-01-04 23:49:03 -0700 | [diff] [blame] | 1514 | Id constituentType1 = getContainedTypeId(getTypeId(value1), constituent); |
| 1515 | Id constituentType2 = getContainedTypeId(getTypeId(value2), constituent); |
| 1516 | Id constituent1 = createCompositeExtract(value1, constituentType1, indexes); |
| 1517 | Id constituent2 = createCompositeExtract(value2, constituentType2, indexes); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1518 | |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 1519 | Id subResultId = createCompositeCompare(precision, constituent1, constituent2, equal); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1520 | |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 1521 | if (constituent == 0) |
| 1522 | resultId = subResultId; |
| 1523 | else |
| 1524 | resultId = createBinOp(equal ? OpLogicalAnd : OpLogicalOr, boolType, resultId, subResultId); |
| 1525 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1526 | |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 1527 | return resultId; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1528 | } |
| 1529 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1530 | // OpCompositeConstruct |
| 1531 | Id Builder::createCompositeConstruct(Id typeId, std::vector<Id>& constituents) |
| 1532 | { |
John Kessenich | 2211835 | 2015-12-21 20:54:09 -0700 | [diff] [blame] | 1533 | assert(isAggregateType(typeId) || (getNumTypeConstituents(typeId) > 1 && getNumTypeConstituents(typeId) == (int)constituents.size())); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1534 | |
| 1535 | Instruction* op = new Instruction(getUniqueId(), typeId, OpCompositeConstruct); |
| 1536 | for (int c = 0; c < (int)constituents.size(); ++c) |
| 1537 | op->addIdOperand(constituents[c]); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1538 | buildPoint->addInstruction(std::unique_ptr<Instruction>(op)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1539 | |
| 1540 | return op->getResultId(); |
| 1541 | } |
| 1542 | |
| 1543 | // Vector or scalar constructor |
| 1544 | Id Builder::createConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId) |
| 1545 | { |
| 1546 | Id result = 0; |
| 1547 | unsigned int numTargetComponents = getNumTypeComponents(resultTypeId); |
| 1548 | unsigned int targetComponent = 0; |
| 1549 | |
| 1550 | // Special case: when calling a vector constructor with a single scalar |
| 1551 | // argument, smear the scalar |
| 1552 | if (sources.size() == 1 && isScalar(sources[0]) && numTargetComponents > 1) |
| 1553 | return smearScalar(precision, sources[0], resultTypeId); |
| 1554 | |
| 1555 | Id scalarTypeId = getScalarTypeId(resultTypeId); |
| 1556 | std::vector<Id> constituents; // accumulate the arguments for OpCompositeConstruct |
| 1557 | for (unsigned int i = 0; i < sources.size(); ++i) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1558 | assert(! isAggregate(sources[i])); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1559 | unsigned int sourceSize = getNumComponents(sources[i]); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1560 | unsigned int sourcesToUse = sourceSize; |
| 1561 | if (sourcesToUse + targetComponent > numTargetComponents) |
| 1562 | sourcesToUse = numTargetComponents - targetComponent; |
| 1563 | |
| 1564 | for (unsigned int s = 0; s < sourcesToUse; ++s) { |
| 1565 | Id arg = sources[i]; |
| 1566 | if (sourceSize > 1) { |
| 1567 | std::vector<unsigned> swiz; |
| 1568 | swiz.push_back(s); |
| 1569 | arg = createRvalueSwizzle(scalarTypeId, arg, swiz); |
| 1570 | } |
| 1571 | |
| 1572 | if (numTargetComponents > 1) |
| 1573 | constituents.push_back(arg); |
| 1574 | else |
| 1575 | result = arg; |
| 1576 | ++targetComponent; |
| 1577 | } |
| 1578 | |
| 1579 | if (targetComponent >= numTargetComponents) |
| 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | if (constituents.size() > 0) |
| 1584 | result = createCompositeConstruct(resultTypeId, constituents); |
| 1585 | |
| 1586 | setPrecision(result, precision); |
| 1587 | |
| 1588 | return result; |
| 1589 | } |
| 1590 | |
| 1591 | // Comments in header |
| 1592 | Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId) |
| 1593 | { |
| 1594 | Id componentTypeId = getScalarTypeId(resultTypeId); |
| 1595 | int numCols = getTypeNumColumns(resultTypeId); |
| 1596 | int numRows = getTypeNumRows(resultTypeId); |
| 1597 | |
| 1598 | // Will use a two step process |
| 1599 | // 1. make a compile-time 2D array of values |
| 1600 | // 2. construct a matrix from that array |
| 1601 | |
| 1602 | // Step 1. |
| 1603 | |
| 1604 | // initialize the array to the identity matrix |
| 1605 | Id ids[maxMatrixSize][maxMatrixSize]; |
| 1606 | Id one = makeFloatConstant(1.0); |
| 1607 | Id zero = makeFloatConstant(0.0); |
| 1608 | for (int col = 0; col < 4; ++col) { |
| 1609 | for (int row = 0; row < 4; ++row) { |
| 1610 | if (col == row) |
| 1611 | ids[col][row] = one; |
| 1612 | else |
| 1613 | ids[col][row] = zero; |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | // modify components as dictated by the arguments |
| 1618 | if (sources.size() == 1 && isScalar(sources[0])) { |
| 1619 | // a single scalar; resets the diagonals |
| 1620 | for (int col = 0; col < 4; ++col) |
| 1621 | ids[col][col] = sources[0]; |
| 1622 | } else if (isMatrix(sources[0])) { |
| 1623 | // constructing from another matrix; copy over the parts that exist in both the argument and constructee |
| 1624 | Id matrix = sources[0]; |
| 1625 | int minCols = std::min(numCols, getNumColumns(matrix)); |
| 1626 | int minRows = std::min(numRows, getNumRows(matrix)); |
| 1627 | for (int col = 0; col < minCols; ++col) { |
| 1628 | std::vector<unsigned> indexes; |
| 1629 | indexes.push_back(col); |
| 1630 | for (int row = 0; row < minRows; ++row) { |
| 1631 | indexes.push_back(row); |
| 1632 | ids[col][row] = createCompositeExtract(matrix, componentTypeId, indexes); |
| 1633 | indexes.pop_back(); |
| 1634 | setPrecision(ids[col][row], precision); |
| 1635 | } |
| 1636 | } |
| 1637 | } else { |
| 1638 | // fill in the matrix in column-major order with whatever argument components are available |
| 1639 | int row = 0; |
| 1640 | int col = 0; |
| 1641 | |
| 1642 | for (int arg = 0; arg < (int)sources.size(); ++arg) { |
| 1643 | Id argComp = sources[arg]; |
| 1644 | for (int comp = 0; comp < getNumComponents(sources[arg]); ++comp) { |
| 1645 | if (getNumComponents(sources[arg]) > 1) { |
| 1646 | argComp = createCompositeExtract(sources[arg], componentTypeId, comp); |
| 1647 | setPrecision(argComp, precision); |
| 1648 | } |
| 1649 | ids[col][row++] = argComp; |
| 1650 | if (row == numRows) { |
| 1651 | row = 0; |
| 1652 | col++; |
| 1653 | } |
| 1654 | } |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | |
| 1659 | // Step 2: Construct a matrix from that array. |
| 1660 | // First make the column vectors, then make the matrix. |
| 1661 | |
| 1662 | // make the column vectors |
| 1663 | Id columnTypeId = getContainedTypeId(resultTypeId); |
| 1664 | std::vector<Id> matrixColumns; |
| 1665 | for (int col = 0; col < numCols; ++col) { |
| 1666 | std::vector<Id> vectorComponents; |
| 1667 | for (int row = 0; row < numRows; ++row) |
| 1668 | vectorComponents.push_back(ids[col][row]); |
| 1669 | matrixColumns.push_back(createCompositeConstruct(columnTypeId, vectorComponents)); |
| 1670 | } |
| 1671 | |
| 1672 | // make the matrix |
| 1673 | return createCompositeConstruct(resultTypeId, matrixColumns); |
| 1674 | } |
| 1675 | |
| 1676 | // Comments in header |
| 1677 | Builder::If::If(Id cond, Builder& gb) : |
| 1678 | builder(gb), |
| 1679 | condition(cond), |
| 1680 | elseBlock(0) |
| 1681 | { |
| 1682 | function = &builder.getBuildPoint()->getParent(); |
| 1683 | |
| 1684 | // make the blocks, but only put the then-block into the function, |
| 1685 | // the else-block and merge-block will be added later, in order, after |
| 1686 | // earlier code is emitted |
| 1687 | thenBlock = new Block(builder.getUniqueId(), *function); |
| 1688 | mergeBlock = new Block(builder.getUniqueId(), *function); |
| 1689 | |
| 1690 | // Save the current block, so that we can add in the flow control split when |
| 1691 | // makeEndIf is called. |
| 1692 | headerBlock = builder.getBuildPoint(); |
| 1693 | |
| 1694 | function->addBlock(thenBlock); |
| 1695 | builder.setBuildPoint(thenBlock); |
| 1696 | } |
| 1697 | |
| 1698 | // Comments in header |
| 1699 | void Builder::If::makeBeginElse() |
| 1700 | { |
| 1701 | // Close out the "then" by having it jump to the mergeBlock |
| 1702 | builder.createBranch(mergeBlock); |
| 1703 | |
| 1704 | // Make the first else block and add it to the function |
| 1705 | elseBlock = new Block(builder.getUniqueId(), *function); |
| 1706 | function->addBlock(elseBlock); |
| 1707 | |
| 1708 | // Start building the else block |
| 1709 | builder.setBuildPoint(elseBlock); |
| 1710 | } |
| 1711 | |
| 1712 | // Comments in header |
| 1713 | void Builder::If::makeEndIf() |
| 1714 | { |
| 1715 | // jump to the merge block |
| 1716 | builder.createBranch(mergeBlock); |
| 1717 | |
| 1718 | // Go back to the headerBlock and make the flow control split |
| 1719 | builder.setBuildPoint(headerBlock); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1720 | builder.createSelectionMerge(mergeBlock, SelectionControlMaskNone); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1721 | if (elseBlock) |
| 1722 | builder.createConditionalBranch(condition, thenBlock, elseBlock); |
| 1723 | else |
| 1724 | builder.createConditionalBranch(condition, thenBlock, mergeBlock); |
| 1725 | |
| 1726 | // add the merge block to the function |
| 1727 | function->addBlock(mergeBlock); |
| 1728 | builder.setBuildPoint(mergeBlock); |
| 1729 | } |
| 1730 | |
| 1731 | // Comments in header |
| 1732 | void Builder::makeSwitch(Id selector, int numSegments, std::vector<int>& caseValues, std::vector<int>& valueIndexToSegment, int defaultSegment, |
| 1733 | std::vector<Block*>& segmentBlocks) |
| 1734 | { |
| 1735 | Function& function = buildPoint->getParent(); |
| 1736 | |
| 1737 | // make all the blocks |
| 1738 | for (int s = 0; s < numSegments; ++s) |
| 1739 | segmentBlocks.push_back(new Block(getUniqueId(), function)); |
| 1740 | |
| 1741 | Block* mergeBlock = new Block(getUniqueId(), function); |
| 1742 | |
| 1743 | // make and insert the switch's selection-merge instruction |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1744 | createSelectionMerge(mergeBlock, SelectionControlMaskNone); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1745 | |
| 1746 | // make the switch instruction |
| 1747 | Instruction* switchInst = new Instruction(NoResult, NoType, OpSwitch); |
| 1748 | switchInst->addIdOperand(selector); |
Dejan Mircevski | 454796e | 2016-01-18 16:18:01 -0500 | [diff] [blame] | 1749 | auto defaultOrMerge = (defaultSegment >= 0) ? segmentBlocks[defaultSegment] : mergeBlock; |
| 1750 | switchInst->addIdOperand(defaultOrMerge->getId()); |
| 1751 | defaultOrMerge->addPredecessor(buildPoint); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1752 | for (int i = 0; i < (int)caseValues.size(); ++i) { |
| 1753 | switchInst->addImmediateOperand(caseValues[i]); |
| 1754 | switchInst->addIdOperand(segmentBlocks[valueIndexToSegment[i]]->getId()); |
Dejan Mircevski | 454796e | 2016-01-18 16:18:01 -0500 | [diff] [blame] | 1755 | segmentBlocks[valueIndexToSegment[i]]->addPredecessor(buildPoint); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1756 | } |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 1757 | buildPoint->addInstruction(std::unique_ptr<Instruction>(switchInst)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1758 | |
| 1759 | // push the merge block |
| 1760 | switchMerges.push(mergeBlock); |
| 1761 | } |
| 1762 | |
| 1763 | // Comments in header |
| 1764 | void Builder::addSwitchBreak() |
| 1765 | { |
| 1766 | // branch to the top of the merge block stack |
| 1767 | createBranch(switchMerges.top()); |
| 1768 | createAndSetNoPredecessorBlock("post-switch-break"); |
| 1769 | } |
| 1770 | |
| 1771 | // Comments in header |
| 1772 | void Builder::nextSwitchSegment(std::vector<Block*>& segmentBlock, int nextSegment) |
| 1773 | { |
| 1774 | int lastSegment = nextSegment - 1; |
| 1775 | if (lastSegment >= 0) { |
| 1776 | // Close out previous segment by jumping, if necessary, to next segment |
| 1777 | if (! buildPoint->isTerminated()) |
| 1778 | createBranch(segmentBlock[nextSegment]); |
| 1779 | } |
| 1780 | Block* block = segmentBlock[nextSegment]; |
| 1781 | block->getParent().addBlock(block); |
| 1782 | setBuildPoint(block); |
| 1783 | } |
| 1784 | |
| 1785 | // Comments in header |
| 1786 | void Builder::endSwitch(std::vector<Block*>& /*segmentBlock*/) |
| 1787 | { |
| 1788 | // Close out previous segment by jumping, if necessary, to next segment |
| 1789 | if (! buildPoint->isTerminated()) |
| 1790 | addSwitchBreak(); |
| 1791 | |
| 1792 | switchMerges.top()->getParent().addBlock(switchMerges.top()); |
| 1793 | setBuildPoint(switchMerges.top()); |
| 1794 | |
| 1795 | switchMerges.pop(); |
| 1796 | } |
| 1797 | |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1798 | Block& Builder::makeNewBlock() |
| 1799 | { |
| 1800 | Function& function = buildPoint->getParent(); |
| 1801 | auto block = new Block(getUniqueId(), function); |
| 1802 | function.addBlock(block); |
| 1803 | return *block; |
| 1804 | } |
| 1805 | |
Dejan Mircevski | 7819bee | 2016-01-11 09:35:22 -0500 | [diff] [blame] | 1806 | Builder::LoopBlocks& Builder::makeNewLoop() |
Dejan Mircevski | 9c6734c | 2016-01-10 12:15:13 -0500 | [diff] [blame] | 1807 | { |
Dejan Mircevski | 97605c8 | 2016-01-20 10:19:25 -0500 | [diff] [blame] | 1808 | // Older MSVC versions don't allow inlining of blocks below. |
| 1809 | LoopBlocks blocks = {makeNewBlock(), makeNewBlock(), makeNewBlock(), makeNewBlock()}; |
| 1810 | loops.push(blocks); |
Dejan Mircevski | 7819bee | 2016-01-11 09:35:22 -0500 | [diff] [blame] | 1811 | return loops.top(); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | void Builder::createLoopContinue() |
| 1815 | { |
Dejan Mircevski | 7819bee | 2016-01-11 09:35:22 -0500 | [diff] [blame] | 1816 | createBranch(&loops.top().continue_target); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1817 | // Set up a block for dead code. |
| 1818 | createAndSetNoPredecessorBlock("post-loop-continue"); |
| 1819 | } |
| 1820 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1821 | void Builder::createLoopExit() |
| 1822 | { |
Dejan Mircevski | 7819bee | 2016-01-11 09:35:22 -0500 | [diff] [blame] | 1823 | createBranch(&loops.top().merge); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1824 | // Set up a block for dead code. |
| 1825 | createAndSetNoPredecessorBlock("post-loop-break"); |
| 1826 | } |
| 1827 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1828 | void Builder::closeLoop() |
| 1829 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1830 | loops.pop(); |
| 1831 | } |
| 1832 | |
| 1833 | void Builder::clearAccessChain() |
| 1834 | { |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1835 | accessChain.base = NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1836 | accessChain.indexChain.clear(); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1837 | accessChain.instr = NoResult; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1838 | accessChain.swizzle.clear(); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1839 | accessChain.component = NoResult; |
| 1840 | accessChain.preSwizzleBaseType = NoType; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1841 | accessChain.isRValue = false; |
| 1842 | } |
| 1843 | |
| 1844 | // Comments in header |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1845 | void Builder::accessChainPushSwizzle(std::vector<unsigned>& swizzle, Id preSwizzleBaseType) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1846 | { |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1847 | // swizzles can be stacked in GLSL, but simplified to a single |
| 1848 | // one here; the base type doesn't change |
| 1849 | if (accessChain.preSwizzleBaseType == NoType) |
| 1850 | accessChain.preSwizzleBaseType = preSwizzleBaseType; |
| 1851 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1852 | // if needed, propagate the swizzle for the current access chain |
| 1853 | if (accessChain.swizzle.size()) { |
| 1854 | std::vector<unsigned> oldSwizzle = accessChain.swizzle; |
| 1855 | accessChain.swizzle.resize(0); |
| 1856 | for (unsigned int i = 0; i < swizzle.size(); ++i) { |
| 1857 | accessChain.swizzle.push_back(oldSwizzle[swizzle[i]]); |
| 1858 | } |
| 1859 | } else |
| 1860 | accessChain.swizzle = swizzle; |
| 1861 | |
| 1862 | // determine if we need to track this swizzle anymore |
| 1863 | simplifyAccessChainSwizzle(); |
| 1864 | } |
| 1865 | |
| 1866 | // Comments in header |
| 1867 | void Builder::accessChainStore(Id rvalue) |
| 1868 | { |
| 1869 | assert(accessChain.isRValue == false); |
| 1870 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1871 | transferAccessChainSwizzle(true); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1872 | Id base = collapseAccessChain(); |
| 1873 | |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1874 | if (accessChain.swizzle.size() && accessChain.component != NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1875 | MissingFunctionality("simultaneous l-value swizzle and dynamic component selection"); |
| 1876 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1877 | // If swizzle still exists, it is out-of-order or not full, we must load the target vector, |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1878 | // extract and insert elements to perform writeMask and/or swizzle. |
| 1879 | Id source = NoResult; |
| 1880 | if (accessChain.swizzle.size()) { |
| 1881 | Id tempBaseId = createLoad(base); |
| 1882 | source = createLvalueSwizzle(getTypeId(tempBaseId), tempBaseId, rvalue, accessChain.swizzle); |
| 1883 | } |
| 1884 | |
| 1885 | // dynamic component selection |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1886 | if (accessChain.component != NoResult) { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1887 | Id tempBaseId = (source == NoResult) ? createLoad(base) : source; |
| 1888 | source = createVectorInsertDynamic(tempBaseId, getTypeId(tempBaseId), rvalue, accessChain.component); |
| 1889 | } |
| 1890 | |
| 1891 | if (source == NoResult) |
| 1892 | source = rvalue; |
| 1893 | |
| 1894 | createStore(source, base); |
| 1895 | } |
| 1896 | |
| 1897 | // Comments in header |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1898 | Id Builder::accessChainLoad(Id resultType) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1899 | { |
| 1900 | Id id; |
| 1901 | |
| 1902 | if (accessChain.isRValue) { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1903 | // transfer access chain, but keep it static, so we can stay in registers |
| 1904 | transferAccessChainSwizzle(false); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1905 | if (accessChain.indexChain.size() > 0) { |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1906 | Id swizzleBase = accessChain.preSwizzleBaseType != NoType ? accessChain.preSwizzleBaseType : resultType; |
| 1907 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1908 | // if all the accesses are constants, we can use OpCompositeExtract |
| 1909 | std::vector<unsigned> indexes; |
| 1910 | bool constant = true; |
| 1911 | for (int i = 0; i < (int)accessChain.indexChain.size(); ++i) { |
| 1912 | if (isConstantScalar(accessChain.indexChain[i])) |
| 1913 | indexes.push_back(getConstantScalar(accessChain.indexChain[i])); |
| 1914 | else { |
| 1915 | constant = false; |
| 1916 | break; |
| 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | if (constant) |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1921 | id = createCompositeExtract(accessChain.base, swizzleBase, indexes); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1922 | else { |
| 1923 | // make a new function variable for this r-value |
| 1924 | Id lValue = createVariable(StorageClassFunction, getTypeId(accessChain.base), "indexable"); |
| 1925 | |
| 1926 | // store into it |
| 1927 | createStore(accessChain.base, lValue); |
| 1928 | |
| 1929 | // move base to the new variable |
| 1930 | accessChain.base = lValue; |
| 1931 | accessChain.isRValue = false; |
| 1932 | |
| 1933 | // load through the access chain |
| 1934 | id = createLoad(collapseAccessChain()); |
| 1935 | } |
| 1936 | } else |
| 1937 | id = accessChain.base; |
| 1938 | } else { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1939 | transferAccessChainSwizzle(true); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1940 | // load through the access chain |
| 1941 | id = createLoad(collapseAccessChain()); |
| 1942 | } |
| 1943 | |
| 1944 | // Done, unless there are swizzles to do |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1945 | if (accessChain.swizzle.size() == 0 && accessChain.component == NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1946 | return id; |
| 1947 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1948 | // Do remaining swizzling |
| 1949 | // First, static swizzling |
| 1950 | if (accessChain.swizzle.size()) { |
| 1951 | // static swizzle |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1952 | Id swizzledType = getScalarTypeId(getTypeId(id)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1953 | if (accessChain.swizzle.size() > 1) |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1954 | swizzledType = makeVectorType(swizzledType, (int)accessChain.swizzle.size()); |
| 1955 | id = createRvalueSwizzle(swizzledType, id, accessChain.swizzle); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | // dynamic single-component selection |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1959 | if (accessChain.component != NoResult) |
| 1960 | id = createVectorExtractDynamic(id, resultType, accessChain.component); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1961 | |
| 1962 | return id; |
| 1963 | } |
| 1964 | |
| 1965 | Id Builder::accessChainGetLValue() |
| 1966 | { |
| 1967 | assert(accessChain.isRValue == false); |
| 1968 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1969 | transferAccessChainSwizzle(true); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1970 | Id lvalue = collapseAccessChain(); |
| 1971 | |
| 1972 | // If swizzle exists, it is out-of-order or not full, we must load the target vector, |
| 1973 | // extract and insert elements to perform writeMask and/or swizzle. This does not |
| 1974 | // go with getting a direct l-value pointer. |
| 1975 | assert(accessChain.swizzle.size() == 0); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 1976 | assert(accessChain.component == NoResult); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 1977 | |
| 1978 | return lvalue; |
| 1979 | } |
| 1980 | |
| 1981 | void Builder::dump(std::vector<unsigned int>& out) const |
| 1982 | { |
| 1983 | // Header, before first instructions: |
| 1984 | out.push_back(MagicNumber); |
| 1985 | out.push_back(Version); |
| 1986 | out.push_back(builderNumber); |
| 1987 | out.push_back(uniqueId + 1); |
| 1988 | out.push_back(0); |
| 1989 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 1990 | // Capabilities |
| 1991 | for (auto cap : capabilities) { |
| 1992 | Instruction capInst(0, 0, OpCapability); |
| 1993 | capInst.addImmediateOperand(cap); |
| 1994 | capInst.dump(out); |
| 1995 | } |
| 1996 | |
| 1997 | // TBD: OpExtension ... |
| 1998 | |
| 1999 | dumpInstructions(out, imports); |
| 2000 | Instruction memInst(0, 0, OpMemoryModel); |
| 2001 | memInst.addImmediateOperand(addressModel); |
| 2002 | memInst.addImmediateOperand(memoryModel); |
| 2003 | memInst.dump(out); |
| 2004 | |
| 2005 | // Instructions saved up while building: |
| 2006 | dumpInstructions(out, entryPoints); |
| 2007 | dumpInstructions(out, executionModes); |
| 2008 | |
| 2009 | // Debug instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2010 | if (source != SourceLanguageUnknown) { |
| 2011 | Instruction sourceInst(0, 0, OpSource); |
| 2012 | sourceInst.addImmediateOperand(source); |
| 2013 | sourceInst.addImmediateOperand(sourceVersion); |
| 2014 | sourceInst.dump(out); |
| 2015 | } |
| 2016 | for (int e = 0; e < (int)extensions.size(); ++e) { |
| 2017 | Instruction extInst(0, 0, OpSourceExtension); |
| 2018 | extInst.addStringOperand(extensions[e]); |
| 2019 | extInst.dump(out); |
| 2020 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2021 | dumpInstructions(out, names); |
| 2022 | dumpInstructions(out, lines); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2023 | |
| 2024 | // Annotation instructions |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2025 | dumpInstructions(out, decorations); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2026 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2027 | dumpInstructions(out, constantsTypesGlobals); |
| 2028 | dumpInstructions(out, externals); |
| 2029 | |
| 2030 | // The functions |
| 2031 | module.dump(out); |
| 2032 | } |
| 2033 | |
| 2034 | // |
| 2035 | // Protected methods. |
| 2036 | // |
| 2037 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2038 | // Turn the described access chain in 'accessChain' into an instruction |
| 2039 | // computing its address. This *cannot* include complex swizzles, which must |
| 2040 | // be handled after this is called, but it does include swizzles that select |
| 2041 | // an individual element, as a single address of a scalar type can be |
| 2042 | // computed by an OpAccessChain instruction. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2043 | Id Builder::collapseAccessChain() |
| 2044 | { |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2045 | assert(accessChain.isRValue == false); |
| 2046 | |
| 2047 | if (accessChain.indexChain.size() > 0) { |
| 2048 | if (accessChain.instr == 0) { |
| 2049 | StorageClass storageClass = (StorageClass)module.getStorageClass(getTypeId(accessChain.base)); |
| 2050 | accessChain.instr = createAccessChain(storageClass, accessChain.base, accessChain.indexChain); |
| 2051 | } |
| 2052 | |
| 2053 | return accessChain.instr; |
| 2054 | } else |
| 2055 | return accessChain.base; |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2056 | |
| 2057 | // note that non-trivial swizzling is left pending... |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2058 | } |
| 2059 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2060 | // clear out swizzle if it is redundant, that is reselecting the same components |
| 2061 | // that would be present without the swizzle. |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2062 | void Builder::simplifyAccessChainSwizzle() |
| 2063 | { |
| 2064 | // If the swizzle has fewer components than the vector, it is subsetting, and must stay |
| 2065 | // to preserve that fact. |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 2066 | if (getNumTypeComponents(accessChain.preSwizzleBaseType) > (int)accessChain.swizzle.size()) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2067 | return; |
| 2068 | |
| 2069 | // if components are out of order, it is a swizzle |
| 2070 | for (unsigned int i = 0; i < accessChain.swizzle.size(); ++i) { |
| 2071 | if (i != accessChain.swizzle[i]) |
| 2072 | return; |
| 2073 | } |
| 2074 | |
| 2075 | // otherwise, there is no need to track this swizzle |
| 2076 | accessChain.swizzle.clear(); |
John Kessenich | fa668da | 2015-09-13 14:46:30 -0600 | [diff] [blame] | 2077 | if (accessChain.component == NoResult) |
| 2078 | accessChain.preSwizzleBaseType = NoType; |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2079 | } |
| 2080 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2081 | // To the extent any swizzling can become part of the chain |
| 2082 | // of accesses instead of a post operation, make it so. |
| 2083 | // If 'dynamic' is true, include transfering a non-static component index, |
| 2084 | // otherwise, only transfer static indexes. |
| 2085 | // |
| 2086 | // Also, Boolean vectors are likely to be special. While |
| 2087 | // for external storage, they should only be integer types, |
| 2088 | // function-local bool vectors could use sub-word indexing, |
| 2089 | // so keep that as a separate Insert/Extract on a loaded vector. |
| 2090 | void Builder::transferAccessChainSwizzle(bool dynamic) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2091 | { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2092 | // too complex? |
| 2093 | if (accessChain.swizzle.size() > 1) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2094 | return; |
| 2095 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2096 | // non existent? |
| 2097 | if (accessChain.swizzle.size() == 0 && accessChain.component == NoResult) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2098 | return; |
| 2099 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2100 | // single component... |
| 2101 | |
| 2102 | // skip doing it for Boolean vectors |
| 2103 | if (isBoolType(getContainedTypeId(accessChain.preSwizzleBaseType))) |
| 2104 | return; |
| 2105 | |
| 2106 | if (accessChain.swizzle.size() == 1) { |
| 2107 | // handle static component |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2108 | accessChain.indexChain.push_back(makeUintConstant(accessChain.swizzle.front())); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2109 | accessChain.swizzle.clear(); |
| 2110 | // note, the only valid remaining dynamic access would be to this one |
| 2111 | // component, so don't bother even looking at accessChain.component |
| 2112 | accessChain.preSwizzleBaseType = NoType; |
| 2113 | accessChain.component = NoResult; |
| 2114 | } else if (dynamic && accessChain.component != NoResult) { |
| 2115 | // handle dynamic component |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2116 | accessChain.indexChain.push_back(accessChain.component); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2117 | accessChain.preSwizzleBaseType = NoType; |
| 2118 | accessChain.component = NoResult; |
| 2119 | } |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | // Utility method for creating a new block and setting the insert point to |
| 2123 | // be in it. This is useful for flow-control operations that need a "dummy" |
| 2124 | // block proceeding them (e.g. instructions after a discard, etc). |
| 2125 | void Builder::createAndSetNoPredecessorBlock(const char* /*name*/) |
| 2126 | { |
| 2127 | Block* block = new Block(getUniqueId(), buildPoint->getParent()); |
| 2128 | block->setUnreachable(); |
| 2129 | buildPoint->getParent().addBlock(block); |
| 2130 | setBuildPoint(block); |
| 2131 | |
| 2132 | //if (name) |
| 2133 | // addName(block->getId(), name); |
| 2134 | } |
| 2135 | |
| 2136 | // Comments in header |
| 2137 | void Builder::createBranch(Block* block) |
| 2138 | { |
| 2139 | Instruction* branch = new Instruction(OpBranch); |
| 2140 | branch->addIdOperand(block->getId()); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 2141 | buildPoint->addInstruction(std::unique_ptr<Instruction>(branch)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2142 | block->addPredecessor(buildPoint); |
| 2143 | } |
| 2144 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2145 | void Builder::createSelectionMerge(Block* mergeBlock, unsigned int control) |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2146 | { |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2147 | Instruction* merge = new Instruction(OpSelectionMerge); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2148 | merge->addIdOperand(mergeBlock->getId()); |
| 2149 | merge->addImmediateOperand(control); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 2150 | buildPoint->addInstruction(std::unique_ptr<Instruction>(merge)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2151 | } |
| 2152 | |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2153 | void Builder::createLoopMerge(Block* mergeBlock, Block* continueBlock, unsigned int control) |
| 2154 | { |
| 2155 | Instruction* merge = new Instruction(OpLoopMerge); |
| 2156 | merge->addIdOperand(mergeBlock->getId()); |
| 2157 | merge->addIdOperand(continueBlock->getId()); |
| 2158 | merge->addImmediateOperand(control); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 2159 | buildPoint->addInstruction(std::unique_ptr<Instruction>(merge)); |
John Kessenich | 55e7d11 | 2015-11-15 21:33:39 -0700 | [diff] [blame] | 2160 | } |
| 2161 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2162 | void Builder::createConditionalBranch(Id condition, Block* thenBlock, Block* elseBlock) |
| 2163 | { |
| 2164 | Instruction* branch = new Instruction(OpBranchConditional); |
| 2165 | branch->addIdOperand(condition); |
| 2166 | branch->addIdOperand(thenBlock->getId()); |
| 2167 | branch->addIdOperand(elseBlock->getId()); |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 2168 | buildPoint->addInstruction(std::unique_ptr<Instruction>(branch)); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2169 | thenBlock->addPredecessor(buildPoint); |
| 2170 | elseBlock->addPredecessor(buildPoint); |
| 2171 | } |
| 2172 | |
Andrew Woloszyn | b7946d1 | 2016-01-18 09:23:56 -0500 | [diff] [blame] | 2173 | void Builder::dumpInstructions(std::vector<unsigned int>& out, const std::vector<std::unique_ptr<Instruction> >& instructions) const |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2174 | { |
| 2175 | for (int i = 0; i < (int)instructions.size(); ++i) { |
| 2176 | instructions[i]->dump(out); |
| 2177 | } |
| 2178 | } |
| 2179 | |
John Kessenich | 426394d | 2015-07-23 10:22:48 -0600 | [diff] [blame] | 2180 | void TbdFunctionality(const char* tbd) |
| 2181 | { |
| 2182 | static std::unordered_set<const char*> issued; |
| 2183 | |
| 2184 | if (issued.find(tbd) == issued.end()) { |
| 2185 | printf("TBD functionality: %s\n", tbd); |
| 2186 | issued.insert(tbd); |
| 2187 | } |
| 2188 | } |
| 2189 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2190 | void MissingFunctionality(const char* fun) |
| 2191 | { |
| 2192 | printf("Missing functionality: %s\n", fun); |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2193 | } |
| 2194 | |
John Kessenich | 140f3df | 2015-06-26 16:58:36 -0600 | [diff] [blame] | 2195 | }; // end spv namespace |