| 1 |
/* |
| 2 |
* Copyright 2015-2020 Arm Limited |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "spirv_cross.hpp" |
| 18 |
#include "GLSL.std.450.h" |
| 19 |
#include "spirv_cfg.hpp" |
| 20 |
#include "spirv_common.hpp" |
| 21 |
#include "spirv_parser.hpp" |
| 22 |
#include <algorithm> |
| 23 |
#include <cstring> |
| 24 |
#include <utility> |
| 25 |
|
| 26 |
using namespace std; |
| 27 |
using namespace spv; |
| 28 |
using namespace SPIRV_CROSS_NAMESPACE; |
| 29 |
|
| 30 |
Compiler::Compiler(vector<uint32_t> ir_) |
| 31 |
{ |
| 32 |
Parser parser(move(ir_)); |
| 33 |
parser.parse(); |
| 34 |
set_ir(move(parser.get_parsed_ir())); |
| 35 |
} |
| 36 |
|
| 37 |
Compiler::Compiler(const uint32_t *ir_, size_t word_count) |
| 38 |
{ |
| 39 |
Parser parser(ir_, word_count); |
| 40 |
parser.parse(); |
| 41 |
set_ir(move(parser.get_parsed_ir())); |
| 42 |
} |
| 43 |
|
| 44 |
Compiler::Compiler(const ParsedIR &ir_) |
| 45 |
{ |
| 46 |
set_ir(ir_); |
| 47 |
} |
| 48 |
|
| 49 |
Compiler::Compiler(ParsedIR &&ir_) |
| 50 |
{ |
| 51 |
set_ir(move(ir_)); |
| 52 |
} |
| 53 |
|
| 54 |
void Compiler::set_ir(ParsedIR &&ir_) |
| 55 |
{ |
| 56 |
ir = move(ir_); |
| 57 |
parse_fixup(); |
| 58 |
} |
| 59 |
|
| 60 |
void Compiler::set_ir(const ParsedIR &ir_) |
| 61 |
{ |
| 62 |
ir = ir_; |
| 63 |
parse_fixup(); |
| 64 |
} |
| 65 |
|
| 66 |
string Compiler::compile() |
| 67 |
{ |
| 68 |
return ""; |
| 69 |
} |
| 70 |
|
| 71 |
bool Compiler::variable_storage_is_aliased(const SPIRVariable &v) |
| 72 |
{ |
| 73 |
auto &type = get<SPIRType>(v.basetype); |
| 74 |
bool ssbo = v.storage == StorageClassStorageBuffer || |
| 75 |
ir.meta[type.self].decoration.decoration_flags.get(DecorationBufferBlock); |
| 76 |
bool image = type.basetype == SPIRType::Image; |
| 77 |
bool counter = type.basetype == SPIRType::AtomicCounter; |
| 78 |
bool buffer_reference = type.storage == StorageClassPhysicalStorageBufferEXT; |
| 79 |
|
| 80 |
bool is_restrict; |
| 81 |
if (ssbo) |
| 82 |
is_restrict = ir.get_buffer_block_flags(v).get(DecorationRestrict); |
| 83 |
else |
| 84 |
is_restrict = has_decoration(v.self, DecorationRestrict); |
| 85 |
|
| 86 |
return !is_restrict && (ssbo || image || counter || buffer_reference); |
| 87 |
} |
| 88 |
|
| 89 |
bool Compiler::block_is_pure(const SPIRBlock &block) |
| 90 |
{ |
| 91 |
// This is a global side effect of the function. |
| 92 |
if (block.terminator == SPIRBlock::Kill) |
| 93 |
return false; |
| 94 |
|
| 95 |
for (auto &i : block.ops) |
| 96 |
{ |
| 97 |
auto ops = stream(i); |
| 98 |
auto op = static_cast<Op>(i.op); |
| 99 |
|
| 100 |
switch (op) |
| 101 |
{ |
| 102 |
case OpFunctionCall: |
| 103 |
{ |
| 104 |
uint32_t func = ops[2]; |
| 105 |
if (!function_is_pure(get<SPIRFunction>(func))) |
| 106 |
return false; |
| 107 |
break; |
| 108 |
} |
| 109 |
|
| 110 |
case OpCopyMemory: |
| 111 |
case OpStore: |
| 112 |
{ |
| 113 |
auto &type = expression_type(ops[0]); |
| 114 |
if (type.storage != StorageClassFunction) |
| 115 |
return false; |
| 116 |
break; |
| 117 |
} |
| 118 |
|
| 119 |
case OpImageWrite: |
| 120 |
return false; |
| 121 |
|
| 122 |
// Atomics are impure. |
| 123 |
case OpAtomicLoad: |
| 124 |
case OpAtomicStore: |
| 125 |
case OpAtomicExchange: |
| 126 |
case OpAtomicCompareExchange: |
| 127 |
case OpAtomicCompareExchangeWeak: |
| 128 |
case OpAtomicIIncrement: |
| 129 |
case OpAtomicIDecrement: |
| 130 |
case OpAtomicIAdd: |
| 131 |
case OpAtomicISub: |
| 132 |
case OpAtomicSMin: |
| 133 |
case OpAtomicUMin: |
| 134 |
case OpAtomicSMax: |
| 135 |
case OpAtomicUMax: |
| 136 |
case OpAtomicAnd: |
| 137 |
case OpAtomicOr: |
| 138 |
case OpAtomicXor: |
| 139 |
return false; |
| 140 |
|
| 141 |
// Geometry shader builtins modify global state. |
| 142 |
case OpEndPrimitive: |
| 143 |
case OpEmitStreamVertex: |
| 144 |
case OpEndStreamPrimitive: |
| 145 |
case OpEmitVertex: |
| 146 |
return false; |
| 147 |
|
| 148 |
// Barriers disallow any reordering, so we should treat blocks with barrier as writing. |
| 149 |
case OpControlBarrier: |
| 150 |
case OpMemoryBarrier: |
| 151 |
return false; |
| 152 |
|
| 153 |
// Ray tracing builtins are impure. |
| 154 |
case OpReportIntersectionNV: |
| 155 |
case OpIgnoreIntersectionNV: |
| 156 |
case OpTerminateRayNV: |
| 157 |
case OpTraceNV: |
| 158 |
case OpExecuteCallableNV: |
| 159 |
return false; |
| 160 |
|
| 161 |
// OpExtInst is potentially impure depending on extension, but GLSL builtins are at least pure. |
| 162 |
|
| 163 |
case OpDemoteToHelperInvocationEXT: |
| 164 |
// This is a global side effect of the function. |
| 165 |
return false; |
| 166 |
|
| 167 |
default: |
| 168 |
break; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return true; |
| 173 |
} |
| 174 |
|
| 175 |
string Compiler::to_name(uint32_t id, bool allow_alias) const |
| 176 |
{ |
| 177 |
if (allow_alias && ir.ids[id].get_type() == TypeType) |
| 178 |
{ |
| 179 |
// If this type is a simple alias, emit the |
| 180 |
// name of the original type instead. |
| 181 |
// We don't want to override the meta alias |
| 182 |
// as that can be overridden by the reflection APIs after parse. |
| 183 |
auto &type = get<SPIRType>(id); |
| 184 |
if (type.type_alias) |
| 185 |
{ |
| 186 |
// If the alias master has been specially packed, we will have emitted a clean variant as well, |
| 187 |
// so skip the name aliasing here. |
| 188 |
if (!has_extended_decoration(type.type_alias, SPIRVCrossDecorationBufferBlockRepacked)) |
| 189 |
return to_name(type.type_alias); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
auto &alias = ir.get_name(id); |
| 194 |
if (alias.empty()) |
| 195 |
return join("_", id); |
| 196 |
else |
| 197 |
return alias; |
| 198 |
} |
| 199 |
|
| 200 |
bool Compiler::function_is_pure(const SPIRFunction &func) |
| 201 |
{ |
| 202 |
for (auto block : func.blocks) |
| 203 |
{ |
| 204 |
if (!block_is_pure(get<SPIRBlock>(block))) |
| 205 |
{ |
| 206 |
//fprintf(stderr, "Function %s is impure!\n", to_name(func.self).c_str()); |
| 207 |
return false; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
//fprintf(stderr, "Function %s is pure!\n", to_name(func.self).c_str()); |
| 212 |
return true; |
| 213 |
} |
| 214 |
|
| 215 |
void Compiler::register_global_read_dependencies(const SPIRBlock &block, uint32_t id) |
| 216 |
{ |
| 217 |
for (auto &i : block.ops) |
| 218 |
{ |
| 219 |
auto ops = stream(i); |
| 220 |
auto op = static_cast<Op>(i.op); |
| 221 |
|
| 222 |
switch (op) |
| 223 |
{ |
| 224 |
case OpFunctionCall: |
| 225 |
{ |
| 226 |
uint32_t func = ops[2]; |
| 227 |
register_global_read_dependencies(get<SPIRFunction>(func), id); |
| 228 |
break; |
| 229 |
} |
| 230 |
|
| 231 |
case OpLoad: |
| 232 |
case OpImageRead: |
| 233 |
{ |
| 234 |
// If we're in a storage class which does not get invalidated, adding dependencies here is no big deal. |
| 235 |
auto *var = maybe_get_backing_variable(ops[2]); |
| 236 |
if (var && var->storage != StorageClassFunction) |
| 237 |
{ |
| 238 |
auto &type = get<SPIRType>(var->basetype); |
| 239 |
|
| 240 |
// InputTargets are immutable. |
| 241 |
if (type.basetype != SPIRType::Image && type.image.dim != DimSubpassData) |
| 242 |
var->dependees.push_back(id); |
| 243 |
} |
| 244 |
break; |
| 245 |
} |
| 246 |
|
| 247 |
default: |
| 248 |
break; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
void Compiler::register_global_read_dependencies(const SPIRFunction &func, uint32_t id) |
| 254 |
{ |
| 255 |
for (auto block : func.blocks) |
| 256 |
register_global_read_dependencies(get<SPIRBlock>(block), id); |
| 257 |
} |
| 258 |
|
| 259 |
SPIRVariable *Compiler::maybe_get_backing_variable(uint32_t chain) |
| 260 |
{ |
| 261 |
auto *var = maybe_get<SPIRVariable>(chain); |
| 262 |
if (!var) |
| 263 |
{ |
| 264 |
auto *cexpr = maybe_get<SPIRExpression>(chain); |
| 265 |
if (cexpr) |
| 266 |
var = maybe_get<SPIRVariable>(cexpr->loaded_from); |
| 267 |
|
| 268 |
auto *access_chain = maybe_get<SPIRAccessChain>(chain); |
| 269 |
if (access_chain) |
| 270 |
var = maybe_get<SPIRVariable>(access_chain->loaded_from); |
| 271 |
} |
| 272 |
|
| 273 |
return var; |
| 274 |
} |
| 275 |
|
| 276 |
StorageClass Compiler::get_expression_effective_storage_class(uint32_t ptr) |
| 277 |
{ |
| 278 |
auto *var = maybe_get_backing_variable(ptr); |
| 279 |
|
| 280 |
// If the expression has been lowered to a temporary, we need to use the Generic storage class. |
| 281 |
// We're looking for the effective storage class of a given expression. |
| 282 |
// An access chain or forwarded OpLoads from such access chains |
| 283 |
// will generally have the storage class of the underlying variable, but if the load was not forwarded |
| 284 |
// we have lost any address space qualifiers. |
| 285 |
bool forced_temporary = ir.ids[ptr].get_type() == TypeExpression && !get<SPIRExpression>(ptr).access_chain && |
| 286 |
(forced_temporaries.count(ptr) != 0 || forwarded_temporaries.count(ptr) == 0); |
| 287 |
|
| 288 |
if (var && !forced_temporary) |
| 289 |
{ |
| 290 |
// Normalize SSBOs to StorageBuffer here. |
| 291 |
if (var->storage == StorageClassUniform && |
| 292 |
has_decoration(get<SPIRType>(var->basetype).self, DecorationBufferBlock)) |
| 293 |
return StorageClassStorageBuffer; |
| 294 |
else |
| 295 |
return var->storage; |
| 296 |
} |
| 297 |
else |
| 298 |
return expression_type(ptr).storage; |
| 299 |
} |
| 300 |
|
| 301 |
void Compiler::register_read(uint32_t expr, uint32_t chain, bool forwarded) |
| 302 |
{ |
| 303 |
auto &e = get<SPIRExpression>(expr); |
| 304 |
auto *var = maybe_get_backing_variable(chain); |
| 305 |
|
| 306 |
if (var) |
| 307 |
{ |
| 308 |
e.loaded_from = var->self; |
| 309 |
|
| 310 |
// If the backing variable is immutable, we do not need to depend on the variable. |
| 311 |
if (forwarded && !is_immutable(var->self)) |
| 312 |
var->dependees.push_back(e.self); |
| 313 |
|
| 314 |
// If we load from a parameter, make sure we create "inout" if we also write to the parameter. |
| 315 |
// The default is "in" however, so we never invalidate our compilation by reading. |
| 316 |
if (var && var->parameter) |
| 317 |
var->parameter->read_count++; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
void Compiler::register_write(uint32_t chain) |
| 322 |
{ |
| 323 |
auto *var = maybe_get<SPIRVariable>(chain); |
| 324 |
if (!var) |
| 325 |
{ |
| 326 |
// If we're storing through an access chain, invalidate the backing variable instead. |
| 327 |
auto *expr = maybe_get<SPIRExpression>(chain); |
| 328 |
if (expr && expr->loaded_from) |
| 329 |
var = maybe_get<SPIRVariable>(expr->loaded_from); |
| 330 |
|
| 331 |
auto *access_chain = maybe_get<SPIRAccessChain>(chain); |
| 332 |
if (access_chain && access_chain->loaded_from) |
| 333 |
var = maybe_get<SPIRVariable>(access_chain->loaded_from); |
| 334 |
} |
| 335 |
|
| 336 |
auto &chain_type = expression_type(chain); |
| 337 |
|
| 338 |
if (var) |
| 339 |
{ |
| 340 |
bool check_argument_storage_qualifier = true; |
| 341 |
auto &type = expression_type(chain); |
| 342 |
|
| 343 |
// If our variable is in a storage class which can alias with other buffers, |
| 344 |
// invalidate all variables which depend on aliased variables. And if this is a |
| 345 |
// variable pointer, then invalidate all variables regardless. |
| 346 |
if (get_variable_data_type(*var).pointer) |
| 347 |
{ |
| 348 |
flush_all_active_variables(); |
| 349 |
|
| 350 |
if (type.pointer_depth == 1) |
| 351 |
{ |
| 352 |
// We have a backing variable which is a pointer-to-pointer type. |
| 353 |
// We are storing some data through a pointer acquired through that variable, |
| 354 |
// but we are not writing to the value of the variable itself, |
| 355 |
// i.e., we are not modifying the pointer directly. |
| 356 |
// If we are storing a non-pointer type (pointer_depth == 1), |
| 357 |
// we know that we are storing some unrelated data. |
| 358 |
// A case here would be |
| 359 |
// void foo(Foo * const *arg) { |
| 360 |
// Foo *bar = *arg; |
| 361 |
// bar->unrelated = 42; |
| 362 |
// } |
| 363 |
// arg, the argument is constant. |
| 364 |
check_argument_storage_qualifier = false; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
if (type.storage == StorageClassPhysicalStorageBufferEXT || variable_storage_is_aliased(*var)) |
| 369 |
flush_all_aliased_variables(); |
| 370 |
else if (var) |
| 371 |
flush_dependees(*var); |
| 372 |
|
| 373 |
// We tried to write to a parameter which is not marked with out qualifier, force a recompile. |
| 374 |
if (check_argument_storage_qualifier && var->parameter && var->parameter->write_count == 0) |
| 375 |
{ |
| 376 |
var->parameter->write_count++; |
| 377 |
force_recompile(); |
| 378 |
} |
| 379 |
} |
| 380 |
else if (chain_type.pointer) |
| 381 |
{ |
| 382 |
// If we stored through a variable pointer, then we don't know which |
| 383 |
// variable we stored to. So *all* expressions after this point need to |
| 384 |
// be invalidated. |
| 385 |
// FIXME: If we can prove that the variable pointer will point to |
| 386 |
// only certain variables, we can invalidate only those. |
| 387 |
flush_all_active_variables(); |
| 388 |
} |
| 389 |
|
| 390 |
// If chain_type.pointer is false, we're not writing to memory backed variables, but temporaries instead. |
| 391 |
// This can happen in copy_logical_type where we unroll complex reads and writes to temporaries. |
| 392 |
} |
| 393 |
|
| 394 |
void Compiler::flush_dependees(SPIRVariable &var) |
| 395 |
{ |
| 396 |
for (auto expr : var.dependees) |
| 397 |
invalid_expressions.insert(expr); |
| 398 |
var.dependees.clear(); |
| 399 |
} |
| 400 |
|
| 401 |
void Compiler::flush_all_aliased_variables() |
| 402 |
{ |
| 403 |
for (auto aliased : aliased_variables) |
| 404 |
flush_dependees(get<SPIRVariable>(aliased)); |
| 405 |
} |
| 406 |
|
| 407 |
void Compiler::flush_all_atomic_capable_variables() |
| 408 |
{ |
| 409 |
for (auto global : global_variables) |
| 410 |
flush_dependees(get<SPIRVariable>(global)); |
| 411 |
flush_all_aliased_variables(); |
| 412 |
} |
| 413 |
|
| 414 |
void Compiler::flush_control_dependent_expressions(uint32_t block_id) |
| 415 |
{ |
| 416 |
auto &block = get<SPIRBlock>(block_id); |
| 417 |
for (auto &expr : block.invalidate_expressions) |
| 418 |
invalid_expressions.insert(expr); |
| 419 |
block.invalidate_expressions.clear(); |
| 420 |
} |
| 421 |
|
| 422 |
void Compiler::flush_all_active_variables() |
| 423 |
{ |
| 424 |
// Invalidate all temporaries we read from variables in this block since they were forwarded. |
| 425 |
// Invalidate all temporaries we read from globals. |
| 426 |
for (auto &v : current_function->local_variables) |
| 427 |
flush_dependees(get<SPIRVariable>(v)); |
| 428 |
for (auto &arg : current_function->arguments) |
| 429 |
flush_dependees(get<SPIRVariable>(arg.id)); |
| 430 |
for (auto global : global_variables) |
| 431 |
flush_dependees(get<SPIRVariable>(global)); |
| 432 |
|
| 433 |
flush_all_aliased_variables(); |
| 434 |
} |
| 435 |
|
| 436 |
uint32_t Compiler::expression_type_id(uint32_t id) const |
| 437 |
{ |
| 438 |
switch (ir.ids[id].get_type()) |
| 439 |
{ |
| 440 |
case TypeVariable: |
| 441 |
return get<SPIRVariable>(id).basetype; |
| 442 |
|
| 443 |
case TypeExpression: |
| 444 |
return get<SPIRExpression>(id).expression_type; |
| 445 |
|
| 446 |
case TypeConstant: |
| 447 |
return get<SPIRConstant>(id).constant_type; |
| 448 |
|
| 449 |
case TypeConstantOp: |
| 450 |
return get<SPIRConstantOp>(id).basetype; |
| 451 |
|
| 452 |
case TypeUndef: |
| 453 |
return get<SPIRUndef>(id).basetype; |
| 454 |
|
| 455 |
case TypeCombinedImageSampler: |
| 456 |
return get<SPIRCombinedImageSampler>(id).combined_type; |
| 457 |
|
| 458 |
case TypeAccessChain: |
| 459 |
return get<SPIRAccessChain>(id).basetype; |
| 460 |
|
| 461 |
default: |
| 462 |
SPIRV_CROSS_THROW("Cannot resolve expression type."); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
const SPIRType &Compiler::expression_type(uint32_t id) const |
| 467 |
{ |
| 468 |
return get<SPIRType>(expression_type_id(id)); |
| 469 |
} |
| 470 |
|
| 471 |
bool Compiler::expression_is_lvalue(uint32_t id) const |
| 472 |
{ |
| 473 |
auto &type = expression_type(id); |
| 474 |
switch (type.basetype) |
| 475 |
{ |
| 476 |
case SPIRType::SampledImage: |
| 477 |
case SPIRType::Image: |
| 478 |
case SPIRType::Sampler: |
| 479 |
return false; |
| 480 |
|
| 481 |
default: |
| 482 |
return true; |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
bool Compiler::is_immutable(uint32_t id) const |
| 487 |
{ |
| 488 |
if (ir.ids[id].get_type() == TypeVariable) |
| 489 |
{ |
| 490 |
auto &var = get<SPIRVariable>(id); |
| 491 |
|
| 492 |
// Anything we load from the UniformConstant address space is guaranteed to be immutable. |
| 493 |
bool pointer_to_const = var.storage == StorageClassUniformConstant; |
| 494 |
return pointer_to_const || var.phi_variable || !expression_is_lvalue(id); |
| 495 |
} |
| 496 |
else if (ir.ids[id].get_type() == TypeAccessChain) |
| 497 |
return get<SPIRAccessChain>(id).immutable; |
| 498 |
else if (ir.ids[id].get_type() == TypeExpression) |
| 499 |
return get<SPIRExpression>(id).immutable; |
| 500 |
else if (ir.ids[id].get_type() == TypeConstant || ir.ids[id].get_type() == TypeConstantOp || |
| 501 |
ir.ids[id].get_type() == TypeUndef) |
| 502 |
return true; |
| 503 |
else |
| 504 |
return false; |
| 505 |
} |
| 506 |
|
| 507 |
static inline bool storage_class_is_interface(spv::StorageClass storage) |
| 508 |
{ |
| 509 |
switch (storage) |
| 510 |
{ |
| 511 |
case StorageClassInput: |
| 512 |
case StorageClassOutput: |
| 513 |
case StorageClassUniform: |
| 514 |
case StorageClassUniformConstant: |
| 515 |
case StorageClassAtomicCounter: |
| 516 |
case StorageClassPushConstant: |
| 517 |
case StorageClassStorageBuffer: |
| 518 |
return true; |
| 519 |
|
| 520 |
default: |
| 521 |
return false; |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
bool Compiler::is_hidden_variable(const SPIRVariable &var, bool include_builtins) const |
| 526 |
{ |
| 527 |
if ((is_builtin_variable(var) && !include_builtins) || var.remapped_variable) |
| 528 |
return true; |
| 529 |
|
| 530 |
// Combined image samplers are always considered active as they are "magic" variables. |
| 531 |
if (find_if(begin(combined_image_samplers), end(combined_image_samplers), [&var](const CombinedImageSampler &samp) { |
| 532 |
return samp.combined_id == var.self; |
| 533 |
}) != end(combined_image_samplers)) |
| 534 |
{ |
| 535 |
return false; |
| 536 |
} |
| 537 |
|
| 538 |
bool hidden = false; |
| 539 |
if (check_active_interface_variables && storage_class_is_interface(var.storage)) |
| 540 |
hidden = active_interface_variables.find(var.self) == end(active_interface_variables); |
| 541 |
return hidden; |
| 542 |
} |
| 543 |
|
| 544 |
bool Compiler::is_builtin_type(const SPIRType &type) const |
| 545 |
{ |
| 546 |
auto *type_meta = ir.find_meta(type.self); |
| 547 |
|
| 548 |
// We can have builtin structs as well. If one member of a struct is builtin, the struct must also be builtin. |
| 549 |
if (type_meta) |
| 550 |
for (auto &m : type_meta->members) |
| 551 |
if (m.builtin) |
| 552 |
return true; |
| 553 |
|
| 554 |
return false; |
| 555 |
} |
| 556 |
|
| 557 |
bool Compiler::is_builtin_variable(const SPIRVariable &var) const |
| 558 |
{ |
| 559 |
auto *m = ir.find_meta(var.self); |
| 560 |
|
| 561 |
if (var.compat_builtin || (m && m->decoration.builtin)) |
| 562 |
return true; |
| 563 |
else |
| 564 |
return is_builtin_type(get<SPIRType>(var.basetype)); |
| 565 |
} |
| 566 |
|
| 567 |
bool Compiler::is_member_builtin(const SPIRType &type, uint32_t index, BuiltIn *builtin) const |
| 568 |
{ |
| 569 |
auto *type_meta = ir.find_meta(type.self); |
| 570 |
|
| 571 |
if (type_meta) |
| 572 |
{ |
| 573 |
auto &memb = type_meta->members; |
| 574 |
if (index < memb.size() && memb[index].builtin) |
| 575 |
{ |
| 576 |
if (builtin) |
| 577 |
*builtin = memb[index].builtin_type; |
| 578 |
return true; |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
return false; |
| 583 |
} |
| 584 |
|
| 585 |
bool Compiler::is_scalar(const SPIRType &type) const |
| 586 |
{ |
| 587 |
return type.basetype != SPIRType::Struct && type.vecsize == 1 && type.columns == 1; |
| 588 |
} |
| 589 |
|
| 590 |
bool Compiler::is_vector(const SPIRType &type) const |
| 591 |
{ |
| 592 |
return type.vecsize > 1 && type.columns == 1; |
| 593 |
} |
| 594 |
|
| 595 |
bool Compiler::is_matrix(const SPIRType &type) const |
| 596 |
{ |
| 597 |
return type.vecsize > 1 && type.columns > 1; |
| 598 |
} |
| 599 |
|
| 600 |
bool Compiler::is_array(const SPIRType &type) const |
| 601 |
{ |
| 602 |
return !type.array.empty(); |
| 603 |
} |
| 604 |
|
| 605 |
ShaderResources Compiler::get_shader_resources() const |
| 606 |
{ |
| 607 |
return get_shader_resources(nullptr); |
| 608 |
} |
| 609 |
|
| 610 |
ShaderResources Compiler::get_shader_resources(const unordered_set<VariableID> &active_variables) const |
| 611 |
{ |
| 612 |
return get_shader_resources(&active_variables); |
| 613 |
} |
| 614 |
|
| 615 |
bool Compiler::InterfaceVariableAccessHandler::handle(Op opcode, const uint32_t *args, uint32_t length) |
| 616 |
{ |
| 617 |
uint32_t variable = 0; |
| 618 |
switch (opcode) |
| 619 |
{ |
| 620 |
// Need this first, otherwise, GCC complains about unhandled switch statements. |
| 621 |
default: |
| 622 |
break; |
| 623 |
|
| 624 |
case OpFunctionCall: |
| 625 |
{ |
| 626 |
// Invalid SPIR-V. |
| 627 |
if (length < 3) |
| 628 |
return false; |
| 629 |
|
| 630 |
uint32_t count = length - 3; |
| 631 |
args += 3; |
| 632 |
for (uint32_t i = 0; i < count; i++) |
| 633 |
{ |
| 634 |
auto *var = compiler.maybe_get<SPIRVariable>(args[i]); |
| 635 |
if (var && storage_class_is_interface(var->storage)) |
| 636 |
variables.insert(args[i]); |
| 637 |
} |
| 638 |
break; |
| 639 |
} |
| 640 |
|
| 641 |
case OpSelect: |
| 642 |
{ |
| 643 |
// Invalid SPIR-V. |
| 644 |
if (length < 5) |
| 645 |
return false; |
| 646 |
|
| 647 |
uint32_t count = length - 3; |
| 648 |
args += 3; |
| 649 |
for (uint32_t i = 0; i < count; i++) |
| 650 |
{ |
| 651 |
auto *var = compiler.maybe_get<SPIRVariable>(args[i]); |
| 652 |
if (var && storage_class_is_interface(var->storage)) |
| 653 |
variables.insert(args[i]); |
| 654 |
} |
| 655 |
break; |
| 656 |
} |
| 657 |
|
| 658 |
case OpPhi: |
| 659 |
{ |
| 660 |
// Invalid SPIR-V. |
| 661 |
if (length < 2) |
| 662 |
return false; |
| 663 |
|
| 664 |
uint32_t count = length - 2; |
| 665 |
args += 2; |
| 666 |
for (uint32_t i = 0; i < count; i += 2) |
| 667 |
{ |
| 668 |
auto *var = compiler.maybe_get<SPIRVariable>(args[i]); |
| 669 |
if (var && storage_class_is_interface(var->storage)) |
| 670 |
variables.insert(args[i]); |
| 671 |
} |
| 672 |
break; |
| 673 |
} |
| 674 |
|
| 675 |
case OpAtomicStore: |
| 676 |
case OpStore: |
| 677 |
// Invalid SPIR-V. |
| 678 |
if (length < 1) |
| 679 |
return false; |
| 680 |
variable = args[0]; |
| 681 |
break; |
| 682 |
|
| 683 |
case OpCopyMemory: |
| 684 |
{ |
| 685 |
if (length < 2) |
| 686 |
return false; |
| 687 |
|
| 688 |
auto *var = compiler.maybe_get<SPIRVariable>(args[0]); |
| 689 |
if (var && storage_class_is_interface(var->storage)) |
| 690 |
variables.insert(args[0]); |
| 691 |
|
| 692 |
var = compiler.maybe_get<SPIRVariable>(args[1]); |
| 693 |
if (var && storage_class_is_interface(var->storage)) |
| 694 |
variables.insert(args[1]); |
| 695 |
break; |
| 696 |
} |
| 697 |
|
| 698 |
case OpExtInst: |
| 699 |
{ |
| 700 |
if (length < 5) |
| 701 |
return false; |
| 702 |
auto &extension_set = compiler.get<SPIRExtension>(args[2]); |
| 703 |
switch (extension_set.ext) |
| 704 |
{ |
| 705 |
case SPIRExtension::GLSL: |
| 706 |
{ |
| 707 |
auto op = static_cast<GLSLstd450>(args[3]); |
| 708 |
|
| 709 |
switch (op) |
| 710 |
{ |
| 711 |
case GLSLstd450InterpolateAtCentroid: |
| 712 |
case GLSLstd450InterpolateAtSample: |
| 713 |
case GLSLstd450InterpolateAtOffset: |
| 714 |
{ |
| 715 |
auto *var = compiler.maybe_get<SPIRVariable>(args[4]); |
| 716 |
if (var && storage_class_is_interface(var->storage)) |
| 717 |
variables.insert(args[4]); |
| 718 |
break; |
| 719 |
} |
| 720 |
|
| 721 |
default: |
| 722 |
break; |
| 723 |
} |
| 724 |
break; |
| 725 |
} |
| 726 |
case SPIRExtension::SPV_AMD_shader_explicit_vertex_parameter: |
| 727 |
{ |
| 728 |
enum AMDShaderExplicitVertexParameter |
| 729 |
{ |
| 730 |
InterpolateAtVertexAMD = 1 |
| 731 |
}; |
| 732 |
|
| 733 |
auto op = static_cast<AMDShaderExplicitVertexParameter>(args[3]); |
| 734 |
|
| 735 |
switch (op) |
| 736 |
{ |
| 737 |
case InterpolateAtVertexAMD: |
| 738 |
{ |
| 739 |
auto *var = compiler.maybe_get<SPIRVariable>(args[4]); |
| 740 |
if (var && storage_class_is_interface(var->storage)) |
| 741 |
variables.insert(args[4]); |
| 742 |
break; |
| 743 |
} |
| 744 |
|
| 745 |
default: |
| 746 |
break; |
| 747 |
} |
| 748 |
break; |
| 749 |
} |
| 750 |
default: |
| 751 |
break; |
| 752 |
} |
| 753 |
break; |
| 754 |
} |
| 755 |
|
| 756 |
case OpAccessChain: |
| 757 |
case OpInBoundsAccessChain: |
| 758 |
case OpPtrAccessChain: |
| 759 |
case OpLoad: |
| 760 |
case OpCopyObject: |
| 761 |
case OpImageTexelPointer: |
| 762 |
case OpAtomicLoad: |
| 763 |
case OpAtomicExchange: |
| 764 |
case OpAtomicCompareExchange: |
| 765 |
case OpAtomicCompareExchangeWeak: |
| 766 |
case OpAtomicIIncrement: |
| 767 |
case OpAtomicIDecrement: |
| 768 |
case OpAtomicIAdd: |
| 769 |
case OpAtomicISub: |
| 770 |
case OpAtomicSMin: |
| 771 |
case OpAtomicUMin: |
| 772 |
case OpAtomicSMax: |
| 773 |
case OpAtomicUMax: |
| 774 |
case OpAtomicAnd: |
| 775 |
case OpAtomicOr: |
| 776 |
case OpAtomicXor: |
| 777 |
case OpArrayLength: |
| 778 |
// Invalid SPIR-V. |
| 779 |
if (length < 3) |
| 780 |
return false; |
| 781 |
variable = args[2]; |
| 782 |
break; |
| 783 |
} |
| 784 |
|
| 785 |
if (variable) |
| 786 |
{ |
| 787 |
auto *var = compiler.maybe_get<SPIRVariable>(variable); |
| 788 |
if (var && storage_class_is_interface(var->storage)) |
| 789 |
variables.insert(variable); |
| 790 |
} |
| 791 |
return true; |
| 792 |
} |
| 793 |
|
| 794 |
unordered_set<VariableID> Compiler::get_active_interface_variables() const |
| 795 |
{ |
| 796 |
// Traverse the call graph and find all interface variables which are in use. |
| 797 |
unordered_set<VariableID> variables; |
| 798 |
InterfaceVariableAccessHandler handler(*this, variables); |
| 799 |
traverse_all_reachable_opcodes(get<SPIRFunction>(ir.default_entry_point), handler); |
| 800 |
|
| 801 |
// Make sure we preserve output variables which are only initialized, but never accessed by any code. |
| 802 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, const SPIRVariable &var) { |
| 803 |
if (var.storage == StorageClassOutput && var.initializer != ID(0)) |
| 804 |
variables.insert(var.self); |
| 805 |
}); |
| 806 |
|
| 807 |
// If we needed to create one, we'll need it. |
| 808 |
if (dummy_sampler_id) |
| 809 |
variables.insert(dummy_sampler_id); |
| 810 |
|
| 811 |
return variables; |
| 812 |
} |
| 813 |
|
| 814 |
void Compiler::set_enabled_interface_variables(std::unordered_set<VariableID> active_variables) |
| 815 |
{ |
| 816 |
active_interface_variables = move(active_variables); |
| 817 |
check_active_interface_variables = true; |
| 818 |
} |
| 819 |
|
| 820 |
ShaderResources Compiler::get_shader_resources(const unordered_set<VariableID> *active_variables) const |
| 821 |
{ |
| 822 |
ShaderResources res; |
| 823 |
|
| 824 |
bool ssbo_instance_name = reflection_ssbo_instance_name_is_significant(); |
| 825 |
|
| 826 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, const SPIRVariable &var) { |
| 827 |
auto &type = this->get<SPIRType>(var.basetype); |
| 828 |
|
| 829 |
// It is possible for uniform storage classes to be passed as function parameters, so detect |
| 830 |
// that. To detect function parameters, check of StorageClass of variable is function scope. |
| 831 |
if (var.storage == StorageClassFunction || !type.pointer || is_builtin_variable(var)) |
| 832 |
return; |
| 833 |
|
| 834 |
if (active_variables && active_variables->find(var.self) == end(*active_variables)) |
| 835 |
return; |
| 836 |
|
| 837 |
// Input |
| 838 |
if (var.storage == StorageClassInput && interface_variable_exists_in_entry_point(var.self)) |
| 839 |
{ |
| 840 |
if (has_decoration(type.self, DecorationBlock)) |
| 841 |
{ |
| 842 |
res.stage_inputs.push_back( |
| 843 |
{ var.self, var.basetype, type.self, get_remapped_declared_block_name(var.self, false) }); |
| 844 |
} |
| 845 |
else |
| 846 |
res.stage_inputs.push_back({ var.self, var.basetype, type.self, get_name(var.self) }); |
| 847 |
} |
| 848 |
// Subpass inputs |
| 849 |
else if (var.storage == StorageClassUniformConstant && type.image.dim == DimSubpassData) |
| 850 |
{ |
| 851 |
res.subpass_inputs.push_back({ var.self, var.basetype, type.self, get_name(var.self) }); |
| 852 |
} |
| 853 |
// Outputs |
| 854 |
else if (var.storage == StorageClassOutput && interface_variable_exists_in_entry_point(var.self)) |
| 855 |
{ |
| 856 |
if (has_decoration(type.self, DecorationBlock)) |
| 857 |
{ |
| 858 |
res.stage_outputs.push_back( |
| 859 |
{ var.self, var.basetype, type.self, get_remapped_declared_block_name(var.self, false) }); |
| 860 |
} |
| 861 |
else |
| 862 |
res.stage_outputs.push_back({ var.self, var.basetype, type.self, get_name(var.self) }); |
| 863 |
} |
| 864 |
// UBOs |
| 865 |
else if (type.storage == StorageClassUniform && has_decoration(type.self, DecorationBlock)) |
| 866 |
{ |
| 867 |
res.uniform_buffers.push_back( |
| 868 |
{ var.self, var.basetype, type.self, get_remapped_declared_block_name(var.self, false) }); |
| 869 |
} |
| 870 |
// Old way to declare SSBOs. |
| 871 |
else if (type.storage == StorageClassUniform && has_decoration(type.self, DecorationBufferBlock)) |
| 872 |
{ |
| 873 |
res.storage_buffers.push_back( |
| 874 |
{ var.self, var.basetype, type.self, get_remapped_declared_block_name(var.self, ssbo_instance_name) }); |
| 875 |
} |
| 876 |
// Modern way to declare SSBOs. |
| 877 |
else if (type.storage == StorageClassStorageBuffer) |
| 878 |
{ |
| 879 |
res.storage_buffers.push_back( |
| 880 |
{ var.self, var.basetype, type.self, get_remapped_declared_block_name(var.self, ssbo_instance_name) }); |
| 881 |
} |
| 882 |
// Push constant blocks |
| 883 |
else if (type.storage == StorageClassPushConstant) |
| 884 |
{ |
| 885 |
// There can only be one push constant block, but keep the vector in case this restriction is lifted |
| 886 |
// in the future. |
| 887 |
res.push_constant_buffers.push_back({ var.self, var.basetype, type.self, get_name(var.self) }); |
| 888 |
} |
| 889 |
// Images |
| 890 |
else if (type.storage == StorageClassUniformConstant && type.basetype == SPIRType::Image && |
| 891 |
type.image.sampled == 2) |
| 892 |
{ |
| 893 |
res.storage_images.push_back({ var.self, var.basetype, type.self, get_name(var.self) }); |
| 894 |
} |
| 895 |
// Separate images |
| 896 |
else if (type.storage == StorageClassUniformConstant && type.basetype == SPIRType::Image && |
| 897 |
type.image.sampled == 1) |
| 898 |
{ |
| 899 |
res.separate_images.push_back({ var.self, var.basetype, type.self, get_name(var.self) }); |
| 900 |
} |
| 901 |
// Separate samplers |
| 902 |
else if (type.storage == StorageClassUniformConstant && type.basetype == SPIRType::Sampler) |
| 903 |
{ |
| 904 |
res.separate_samplers.push_back({ var.self, var.basetype, type.self, get_name(var.self) }); |
| 905 |
} |
| 906 |
// Textures |
| 907 |
else if (type.storage == StorageClassUniformConstant && type.basetype == SPIRType::SampledImage) |
| 908 |
{ |
| 909 |
res.sampled_images.push_back({ var.self, var.basetype, type.self, get_name(var.self) }); |
| 910 |
} |
| 911 |
// Atomic counters |
| 912 |
else if (type.storage == StorageClassAtomicCounter) |
| 913 |
{ |
| 914 |
res.atomic_counters.push_back({ var.self, var.basetype, type.self, get_name(var.self) }); |
| 915 |
} |
| 916 |
// Acceleration structures |
| 917 |
else if (type.storage == StorageClassUniformConstant && type.basetype == SPIRType::AccelerationStructure) |
| 918 |
{ |
| 919 |
res.acceleration_structures.push_back({ var.self, var.basetype, type.self, get_name(var.self) }); |
| 920 |
} |
| 921 |
}); |
| 922 |
|
| 923 |
return res; |
| 924 |
} |
| 925 |
|
| 926 |
bool Compiler::type_is_block_like(const SPIRType &type) const |
| 927 |
{ |
| 928 |
if (type.basetype != SPIRType::Struct) |
| 929 |
return false; |
| 930 |
|
| 931 |
if (has_decoration(type.self, DecorationBlock) || has_decoration(type.self, DecorationBufferBlock)) |
| 932 |
{ |
| 933 |
return true; |
| 934 |
} |
| 935 |
|
| 936 |
// Block-like types may have Offset decorations. |
| 937 |
for (uint32_t i = 0; i < uint32_t(type.member_types.size()); i++) |
| 938 |
if (has_member_decoration(type.self, i, DecorationOffset)) |
| 939 |
return true; |
| 940 |
|
| 941 |
return false; |
| 942 |
} |
| 943 |
|
| 944 |
void Compiler::parse_fixup() |
| 945 |
{ |
| 946 |
// Figure out specialization constants for work group sizes. |
| 947 |
for (auto id_ : ir.ids_for_constant_or_variable) |
| 948 |
{ |
| 949 |
auto &id = ir.ids[id_]; |
| 950 |
|
| 951 |
if (id.get_type() == TypeConstant) |
| 952 |
{ |
| 953 |
auto &c = id.get<SPIRConstant>(); |
| 954 |
if (ir.meta[c.self].decoration.builtin && ir.meta[c.self].decoration.builtin_type == BuiltInWorkgroupSize) |
| 955 |
{ |
| 956 |
// In current SPIR-V, there can be just one constant like this. |
| 957 |
// All entry points will receive the constant value. |
| 958 |
for (auto &entry : ir.entry_points) |
| 959 |
{ |
| 960 |
entry.second.workgroup_size.constant = c.self; |
| 961 |
entry.second.workgroup_size.x = c.scalar(0, 0); |
| 962 |
entry.second.workgroup_size.y = c.scalar(0, 1); |
| 963 |
entry.second.workgroup_size.z = c.scalar(0, 2); |
| 964 |
} |
| 965 |
} |
| 966 |
} |
| 967 |
else if (id.get_type() == TypeVariable) |
| 968 |
{ |
| 969 |
auto &var = id.get<SPIRVariable>(); |
| 970 |
if (var.storage == StorageClassPrivate || var.storage == StorageClassWorkgroup || |
| 971 |
var.storage == StorageClassOutput) |
| 972 |
global_variables.push_back(var.self); |
| 973 |
if (variable_storage_is_aliased(var)) |
| 974 |
aliased_variables.push_back(var.self); |
| 975 |
} |
| 976 |
} |
| 977 |
} |
| 978 |
|
| 979 |
void Compiler::update_name_cache(unordered_set<string> &cache_primary, const unordered_set<string> &cache_secondary, |
| 980 |
string &name) |
| 981 |
{ |
| 982 |
if (name.empty()) |
| 983 |
return; |
| 984 |
|
| 985 |
const auto find_name = [&](const string &n) -> bool { |
| 986 |
if (cache_primary.find(n) != end(cache_primary)) |
| 987 |
return true; |
| 988 |
|
| 989 |
if (&cache_primary != &cache_secondary) |
| 990 |
if (cache_secondary.find(n) != end(cache_secondary)) |
| 991 |
return true; |
| 992 |
|
| 993 |
return false; |
| 994 |
}; |
| 995 |
|
| 996 |
const auto insert_name = [&](const string &n) { cache_primary.insert(n); }; |
| 997 |
|
| 998 |
if (!find_name(name)) |
| 999 |
{ |
| 1000 |
insert_name(name); |
| 1001 |
return; |
| 1002 |
} |
| 1003 |
|
| 1004 |
uint32_t counter = 0; |
| 1005 |
auto tmpname = name; |
| 1006 |
|
| 1007 |
bool use_linked_underscore = true; |
| 1008 |
|
| 1009 |
if (tmpname == "_") |
| 1010 |
{ |
| 1011 |
// We cannot just append numbers, as we will end up creating internally reserved names. |
| 1012 |
// Make it like _0_<counter> instead. |
| 1013 |
tmpname += "0"; |
| 1014 |
} |
| 1015 |
else if (tmpname.back() == '_') |
| 1016 |
{ |
| 1017 |
// The last_character is an underscore, so we don't need to link in underscore. |
| 1018 |
// This would violate double underscore rules. |
| 1019 |
use_linked_underscore = false; |
| 1020 |
} |
| 1021 |
|
| 1022 |
// If there is a collision (very rare), |
| 1023 |
// keep tacking on extra identifier until it's unique. |
| 1024 |
do |
| 1025 |
{ |
| 1026 |
counter++; |
| 1027 |
name = tmpname + (use_linked_underscore ? "_" : "") + convert_to_string(counter); |
| 1028 |
} while (find_name(name)); |
| 1029 |
insert_name(name); |
| 1030 |
} |
| 1031 |
|
| 1032 |
void Compiler::update_name_cache(unordered_set<string> &cache, string &name) |
| 1033 |
{ |
| 1034 |
update_name_cache(cache, cache, name); |
| 1035 |
} |
| 1036 |
|
| 1037 |
void Compiler::set_name(ID id, const std::string &name) |
| 1038 |
{ |
| 1039 |
ir.set_name(id, name); |
| 1040 |
} |
| 1041 |
|
| 1042 |
const SPIRType &Compiler::get_type(TypeID id) const |
| 1043 |
{ |
| 1044 |
return get<SPIRType>(id); |
| 1045 |
} |
| 1046 |
|
| 1047 |
const SPIRType &Compiler::get_type_from_variable(VariableID id) const |
| 1048 |
{ |
| 1049 |
return get<SPIRType>(get<SPIRVariable>(id).basetype); |
| 1050 |
} |
| 1051 |
|
| 1052 |
uint32_t Compiler::get_pointee_type_id(uint32_t type_id) const |
| 1053 |
{ |
| 1054 |
auto *p_type = &get<SPIRType>(type_id); |
| 1055 |
if (p_type->pointer) |
| 1056 |
{ |
| 1057 |
assert(p_type->parent_type); |
| 1058 |
type_id = p_type->parent_type; |
| 1059 |
} |
| 1060 |
return type_id; |
| 1061 |
} |
| 1062 |
|
| 1063 |
const SPIRType &Compiler::get_pointee_type(const SPIRType &type) const |
| 1064 |
{ |
| 1065 |
auto *p_type = &type; |
| 1066 |
if (p_type->pointer) |
| 1067 |
{ |
| 1068 |
assert(p_type->parent_type); |
| 1069 |
p_type = &get<SPIRType>(p_type->parent_type); |
| 1070 |
} |
| 1071 |
return *p_type; |
| 1072 |
} |
| 1073 |
|
| 1074 |
const SPIRType &Compiler::get_pointee_type(uint32_t type_id) const |
| 1075 |
{ |
| 1076 |
return get_pointee_type(get<SPIRType>(type_id)); |
| 1077 |
} |
| 1078 |
|
| 1079 |
uint32_t Compiler::get_variable_data_type_id(const SPIRVariable &var) const |
| 1080 |
{ |
| 1081 |
if (var.phi_variable) |
| 1082 |
return var.basetype; |
| 1083 |
return get_pointee_type_id(var.basetype); |
| 1084 |
} |
| 1085 |
|
| 1086 |
SPIRType &Compiler::get_variable_data_type(const SPIRVariable &var) |
| 1087 |
{ |
| 1088 |
return get<SPIRType>(get_variable_data_type_id(var)); |
| 1089 |
} |
| 1090 |
|
| 1091 |
const SPIRType &Compiler::get_variable_data_type(const SPIRVariable &var) const |
| 1092 |
{ |
| 1093 |
return get<SPIRType>(get_variable_data_type_id(var)); |
| 1094 |
} |
| 1095 |
|
| 1096 |
SPIRType &Compiler::get_variable_element_type(const SPIRVariable &var) |
| 1097 |
{ |
| 1098 |
SPIRType *type = &get_variable_data_type(var); |
| 1099 |
if (is_array(*type)) |
| 1100 |
type = &get<SPIRType>(type->parent_type); |
| 1101 |
return *type; |
| 1102 |
} |
| 1103 |
|
| 1104 |
const SPIRType &Compiler::get_variable_element_type(const SPIRVariable &var) const |
| 1105 |
{ |
| 1106 |
const SPIRType *type = &get_variable_data_type(var); |
| 1107 |
if (is_array(*type)) |
| 1108 |
type = &get<SPIRType>(type->parent_type); |
| 1109 |
return *type; |
| 1110 |
} |
| 1111 |
|
| 1112 |
bool Compiler::is_sampled_image_type(const SPIRType &type) |
| 1113 |
{ |
| 1114 |
return (type.basetype == SPIRType::Image || type.basetype == SPIRType::SampledImage) && type.image.sampled == 1 && |
| 1115 |
type.image.dim != DimBuffer; |
| 1116 |
} |
| 1117 |
|
| 1118 |
void Compiler::set_member_decoration_string(TypeID id, uint32_t index, spv::Decoration decoration, |
| 1119 |
const std::string &argument) |
| 1120 |
{ |
| 1121 |
ir.set_member_decoration_string(id, index, decoration, argument); |
| 1122 |
} |
| 1123 |
|
| 1124 |
void Compiler::set_member_decoration(TypeID id, uint32_t index, Decoration decoration, uint32_t argument) |
| 1125 |
{ |
| 1126 |
ir.set_member_decoration(id, index, decoration, argument); |
| 1127 |
} |
| 1128 |
|
| 1129 |
void Compiler::set_member_name(TypeID id, uint32_t index, const std::string &name) |
| 1130 |
{ |
| 1131 |
ir.set_member_name(id, index, name); |
| 1132 |
} |
| 1133 |
|
| 1134 |
const std::string &Compiler::get_member_name(TypeID id, uint32_t index) const |
| 1135 |
{ |
| 1136 |
return ir.get_member_name(id, index); |
| 1137 |
} |
| 1138 |
|
| 1139 |
void Compiler::set_qualified_name(uint32_t id, const string &name) |
| 1140 |
{ |
| 1141 |
ir.meta[id].decoration.qualified_alias = name; |
| 1142 |
} |
| 1143 |
|
| 1144 |
void Compiler::set_member_qualified_name(uint32_t type_id, uint32_t index, const std::string &name) |
| 1145 |
{ |
| 1146 |
ir.meta[type_id].members.resize(max(ir.meta[type_id].members.size(), size_t(index) + 1)); |
| 1147 |
ir.meta[type_id].members[index].qualified_alias = name; |
| 1148 |
} |
| 1149 |
|
| 1150 |
const string &Compiler::get_member_qualified_name(TypeID type_id, uint32_t index) const |
| 1151 |
{ |
| 1152 |
auto *m = ir.find_meta(type_id); |
| 1153 |
if (m && index < m->members.size()) |
| 1154 |
return m->members[index].qualified_alias; |
| 1155 |
else |
| 1156 |
return ir.get_empty_string(); |
| 1157 |
} |
| 1158 |
|
| 1159 |
uint32_t Compiler::get_member_decoration(TypeID id, uint32_t index, Decoration decoration) const |
| 1160 |
{ |
| 1161 |
return ir.get_member_decoration(id, index, decoration); |
| 1162 |
} |
| 1163 |
|
| 1164 |
const Bitset &Compiler::get_member_decoration_bitset(TypeID id, uint32_t index) const |
| 1165 |
{ |
| 1166 |
return ir.get_member_decoration_bitset(id, index); |
| 1167 |
} |
| 1168 |
|
| 1169 |
bool Compiler::has_member_decoration(TypeID id, uint32_t index, Decoration decoration) const |
| 1170 |
{ |
| 1171 |
return ir.has_member_decoration(id, index, decoration); |
| 1172 |
} |
| 1173 |
|
| 1174 |
void Compiler::unset_member_decoration(TypeID id, uint32_t index, Decoration decoration) |
| 1175 |
{ |
| 1176 |
ir.unset_member_decoration(id, index, decoration); |
| 1177 |
} |
| 1178 |
|
| 1179 |
void Compiler::set_decoration_string(ID id, spv::Decoration decoration, const std::string &argument) |
| 1180 |
{ |
| 1181 |
ir.set_decoration_string(id, decoration, argument); |
| 1182 |
} |
| 1183 |
|
| 1184 |
void Compiler::set_decoration(ID id, Decoration decoration, uint32_t argument) |
| 1185 |
{ |
| 1186 |
ir.set_decoration(id, decoration, argument); |
| 1187 |
} |
| 1188 |
|
| 1189 |
void Compiler::set_extended_decoration(uint32_t id, ExtendedDecorations decoration, uint32_t value) |
| 1190 |
{ |
| 1191 |
auto &dec = ir.meta[id].decoration; |
| 1192 |
dec.extended.flags.set(decoration); |
| 1193 |
dec.extended.values[decoration] = value; |
| 1194 |
} |
| 1195 |
|
| 1196 |
void Compiler::set_extended_member_decoration(uint32_t type, uint32_t index, ExtendedDecorations decoration, |
| 1197 |
uint32_t value) |
| 1198 |
{ |
| 1199 |
ir.meta[type].members.resize(max(ir.meta[type].members.size(), size_t(index) + 1)); |
| 1200 |
auto &dec = ir.meta[type].members[index]; |
| 1201 |
dec.extended.flags.set(decoration); |
| 1202 |
dec.extended.values[decoration] = value; |
| 1203 |
} |
| 1204 |
|
| 1205 |
static uint32_t get_default_extended_decoration(ExtendedDecorations decoration) |
| 1206 |
{ |
| 1207 |
switch (decoration) |
| 1208 |
{ |
| 1209 |
case SPIRVCrossDecorationResourceIndexPrimary: |
| 1210 |
case SPIRVCrossDecorationResourceIndexSecondary: |
| 1211 |
case SPIRVCrossDecorationResourceIndexTertiary: |
| 1212 |
case SPIRVCrossDecorationResourceIndexQuaternary: |
| 1213 |
case SPIRVCrossDecorationInterfaceMemberIndex: |
| 1214 |
return ~(0u); |
| 1215 |
|
| 1216 |
default: |
| 1217 |
return 0; |
| 1218 |
} |
| 1219 |
} |
| 1220 |
|
| 1221 |
uint32_t Compiler::get_extended_decoration(uint32_t id, ExtendedDecorations decoration) const |
| 1222 |
{ |
| 1223 |
auto *m = ir.find_meta(id); |
| 1224 |
if (!m) |
| 1225 |
return 0; |
| 1226 |
|
| 1227 |
auto &dec = m->decoration; |
| 1228 |
|
| 1229 |
if (!dec.extended.flags.get(decoration)) |
| 1230 |
return get_default_extended_decoration(decoration); |
| 1231 |
|
| 1232 |
return dec.extended.values[decoration]; |
| 1233 |
} |
| 1234 |
|
| 1235 |
uint32_t Compiler::get_extended_member_decoration(uint32_t type, uint32_t index, ExtendedDecorations decoration) const |
| 1236 |
{ |
| 1237 |
auto *m = ir.find_meta(type); |
| 1238 |
if (!m) |
| 1239 |
return 0; |
| 1240 |
|
| 1241 |
if (index >= m->members.size()) |
| 1242 |
return 0; |
| 1243 |
|
| 1244 |
auto &dec = m->members[index]; |
| 1245 |
if (!dec.extended.flags.get(decoration)) |
| 1246 |
return get_default_extended_decoration(decoration); |
| 1247 |
return dec.extended.values[decoration]; |
| 1248 |
} |
| 1249 |
|
| 1250 |
bool Compiler::has_extended_decoration(uint32_t id, ExtendedDecorations decoration) const |
| 1251 |
{ |
| 1252 |
auto *m = ir.find_meta(id); |
| 1253 |
if (!m) |
| 1254 |
return false; |
| 1255 |
|
| 1256 |
auto &dec = m->decoration; |
| 1257 |
return dec.extended.flags.get(decoration); |
| 1258 |
} |
| 1259 |
|
| 1260 |
bool Compiler::has_extended_member_decoration(uint32_t type, uint32_t index, ExtendedDecorations decoration) const |
| 1261 |
{ |
| 1262 |
auto *m = ir.find_meta(type); |
| 1263 |
if (!m) |
| 1264 |
return false; |
| 1265 |
|
| 1266 |
if (index >= m->members.size()) |
| 1267 |
return false; |
| 1268 |
|
| 1269 |
auto &dec = m->members[index]; |
| 1270 |
return dec.extended.flags.get(decoration); |
| 1271 |
} |
| 1272 |
|
| 1273 |
void Compiler::unset_extended_decoration(uint32_t id, ExtendedDecorations decoration) |
| 1274 |
{ |
| 1275 |
auto &dec = ir.meta[id].decoration; |
| 1276 |
dec.extended.flags.clear(decoration); |
| 1277 |
dec.extended.values[decoration] = 0; |
| 1278 |
} |
| 1279 |
|
| 1280 |
void Compiler::unset_extended_member_decoration(uint32_t type, uint32_t index, ExtendedDecorations decoration) |
| 1281 |
{ |
| 1282 |
ir.meta[type].members.resize(max(ir.meta[type].members.size(), size_t(index) + 1)); |
| 1283 |
auto &dec = ir.meta[type].members[index]; |
| 1284 |
dec.extended.flags.clear(decoration); |
| 1285 |
dec.extended.values[decoration] = 0; |
| 1286 |
} |
| 1287 |
|
| 1288 |
StorageClass Compiler::get_storage_class(VariableID id) const |
| 1289 |
{ |
| 1290 |
return get<SPIRVariable>(id).storage; |
| 1291 |
} |
| 1292 |
|
| 1293 |
const std::string &Compiler::get_name(ID id) const |
| 1294 |
{ |
| 1295 |
return ir.get_name(id); |
| 1296 |
} |
| 1297 |
|
| 1298 |
const std::string Compiler::get_fallback_name(ID id) const |
| 1299 |
{ |
| 1300 |
return join("_", id); |
| 1301 |
} |
| 1302 |
|
| 1303 |
const std::string Compiler::get_block_fallback_name(VariableID id) const |
| 1304 |
{ |
| 1305 |
auto &var = get<SPIRVariable>(id); |
| 1306 |
if (get_name(id).empty()) |
| 1307 |
return join("_", get<SPIRType>(var.basetype).self, "_", id); |
| 1308 |
else |
| 1309 |
return get_name(id); |
| 1310 |
} |
| 1311 |
|
| 1312 |
const Bitset &Compiler::get_decoration_bitset(ID id) const |
| 1313 |
{ |
| 1314 |
return ir.get_decoration_bitset(id); |
| 1315 |
} |
| 1316 |
|
| 1317 |
bool Compiler::has_decoration(ID id, Decoration decoration) const |
| 1318 |
{ |
| 1319 |
return ir.has_decoration(id, decoration); |
| 1320 |
} |
| 1321 |
|
| 1322 |
const string &Compiler::get_decoration_string(ID id, Decoration decoration) const |
| 1323 |
{ |
| 1324 |
return ir.get_decoration_string(id, decoration); |
| 1325 |
} |
| 1326 |
|
| 1327 |
const string &Compiler::get_member_decoration_string(TypeID id, uint32_t index, Decoration decoration) const |
| 1328 |
{ |
| 1329 |
return ir.get_member_decoration_string(id, index, decoration); |
| 1330 |
} |
| 1331 |
|
| 1332 |
uint32_t Compiler::get_decoration(ID id, Decoration decoration) const |
| 1333 |
{ |
| 1334 |
return ir.get_decoration(id, decoration); |
| 1335 |
} |
| 1336 |
|
| 1337 |
void Compiler::unset_decoration(ID id, Decoration decoration) |
| 1338 |
{ |
| 1339 |
ir.unset_decoration(id, decoration); |
| 1340 |
} |
| 1341 |
|
| 1342 |
bool Compiler::get_binary_offset_for_decoration(VariableID id, spv::Decoration decoration, uint32_t &word_offset) const |
| 1343 |
{ |
| 1344 |
auto *m = ir.find_meta(id); |
| 1345 |
if (!m) |
| 1346 |
return false; |
| 1347 |
|
| 1348 |
auto &word_offsets = m->decoration_word_offset; |
| 1349 |
auto itr = word_offsets.find(decoration); |
| 1350 |
if (itr == end(word_offsets)) |
| 1351 |
return false; |
| 1352 |
|
| 1353 |
word_offset = itr->second; |
| 1354 |
return true; |
| 1355 |
} |
| 1356 |
|
| 1357 |
bool Compiler::block_is_loop_candidate(const SPIRBlock &block, SPIRBlock::Method method) const |
| 1358 |
{ |
| 1359 |
// Tried and failed. |
| 1360 |
if (block.disable_block_optimization || block.complex_continue) |
| 1361 |
return false; |
| 1362 |
|
| 1363 |
if (method == SPIRBlock::MergeToSelectForLoop || method == SPIRBlock::MergeToSelectContinueForLoop) |
| 1364 |
{ |
| 1365 |
// Try to detect common for loop pattern |
| 1366 |
// which the code backend can use to create cleaner code. |
| 1367 |
// for(;;) { if (cond) { some_body; } else { break; } } |
| 1368 |
// is the pattern we're looking for. |
| 1369 |
const auto *false_block = maybe_get<SPIRBlock>(block.false_block); |
| 1370 |
const auto *true_block = maybe_get<SPIRBlock>(block.true_block); |
| 1371 |
const auto *merge_block = maybe_get<SPIRBlock>(block.merge_block); |
| 1372 |
|
| 1373 |
bool false_block_is_merge = block.false_block == block.merge_block || |
| 1374 |
(false_block && merge_block && execution_is_noop(*false_block, *merge_block)); |
| 1375 |
|
| 1376 |
bool true_block_is_merge = block.true_block == block.merge_block || |
| 1377 |
(true_block && merge_block && execution_is_noop(*true_block, *merge_block)); |
| 1378 |
|
| 1379 |
bool positive_candidate = |
| 1380 |
block.true_block != block.merge_block && block.true_block != block.self && false_block_is_merge; |
| 1381 |
|
| 1382 |
bool negative_candidate = |
| 1383 |
block.false_block != block.merge_block && block.false_block != block.self && true_block_is_merge; |
| 1384 |
|
| 1385 |
bool ret = block.terminator == SPIRBlock::Select && block.merge == SPIRBlock::MergeLoop && |
| 1386 |
(positive_candidate || negative_candidate); |
| 1387 |
|
| 1388 |
if (ret && positive_candidate && method == SPIRBlock::MergeToSelectContinueForLoop) |
| 1389 |
ret = block.true_block == block.continue_block; |
| 1390 |
else if (ret && negative_candidate && method == SPIRBlock::MergeToSelectContinueForLoop) |
| 1391 |
ret = block.false_block == block.continue_block; |
| 1392 |
|
| 1393 |
// If we have OpPhi which depends on branches which came from our own block, |
| 1394 |
// we need to flush phi variables in else block instead of a trivial break, |
| 1395 |
// so we cannot assume this is a for loop candidate. |
| 1396 |
if (ret) |
| 1397 |
{ |
| 1398 |
for (auto &phi : block.phi_variables) |
| 1399 |
if (phi.parent == block.self) |
| 1400 |
return false; |
| 1401 |
|
| 1402 |
auto *merge = maybe_get<SPIRBlock>(block.merge_block); |
| 1403 |
if (merge) |
| 1404 |
for (auto &phi : merge->phi_variables) |
| 1405 |
if (phi.parent == block.self) |
| 1406 |
return false; |
| 1407 |
} |
| 1408 |
return ret; |
| 1409 |
} |
| 1410 |
else if (method == SPIRBlock::MergeToDirectForLoop) |
| 1411 |
{ |
| 1412 |
// Empty loop header that just sets up merge target |
| 1413 |
// and branches to loop body. |
| 1414 |
bool ret = block.terminator == SPIRBlock::Direct && block.merge == SPIRBlock::MergeLoop && block.ops.empty(); |
| 1415 |
|
| 1416 |
if (!ret) |
| 1417 |
return false; |
| 1418 |
|
| 1419 |
auto &child = get<SPIRBlock>(block.next_block); |
| 1420 |
|
| 1421 |
const auto *false_block = maybe_get<SPIRBlock>(child.false_block); |
| 1422 |
const auto *true_block = maybe_get<SPIRBlock>(child.true_block); |
| 1423 |
const auto *merge_block = maybe_get<SPIRBlock>(block.merge_block); |
| 1424 |
|
| 1425 |
bool false_block_is_merge = child.false_block == block.merge_block || |
| 1426 |
(false_block && merge_block && execution_is_noop(*false_block, *merge_block)); |
| 1427 |
|
| 1428 |
bool true_block_is_merge = child.true_block == block.merge_block || |
| 1429 |
(true_block && merge_block && execution_is_noop(*true_block, *merge_block)); |
| 1430 |
|
| 1431 |
bool positive_candidate = |
| 1432 |
child.true_block != block.merge_block && child.true_block != block.self && false_block_is_merge; |
| 1433 |
|
| 1434 |
bool negative_candidate = |
| 1435 |
child.false_block != block.merge_block && child.false_block != block.self && true_block_is_merge; |
| 1436 |
|
| 1437 |
ret = child.terminator == SPIRBlock::Select && child.merge == SPIRBlock::MergeNone && |
| 1438 |
(positive_candidate || negative_candidate); |
| 1439 |
|
| 1440 |
// If we have OpPhi which depends on branches which came from our own block, |
| 1441 |
// we need to flush phi variables in else block instead of a trivial break, |
| 1442 |
// so we cannot assume this is a for loop candidate. |
| 1443 |
if (ret) |
| 1444 |
{ |
| 1445 |
for (auto &phi : block.phi_variables) |
| 1446 |
if (phi.parent == block.self || phi.parent == child.self) |
| 1447 |
return false; |
| 1448 |
|
| 1449 |
for (auto &phi : child.phi_variables) |
| 1450 |
if (phi.parent == block.self) |
| 1451 |
return false; |
| 1452 |
|
| 1453 |
auto *merge = maybe_get<SPIRBlock>(block.merge_block); |
| 1454 |
if (merge) |
| 1455 |
for (auto &phi : merge->phi_variables) |
| 1456 |
if (phi.parent == block.self || phi.parent == child.false_block) |
| 1457 |
return false; |
| 1458 |
} |
| 1459 |
|
| 1460 |
return ret; |
| 1461 |
} |
| 1462 |
else |
| 1463 |
return false; |
| 1464 |
} |
| 1465 |
|
| 1466 |
bool Compiler::execution_is_noop(const SPIRBlock &from, const SPIRBlock &to) const |
| 1467 |
{ |
| 1468 |
if (!execution_is_branchless(from, to)) |
| 1469 |
return false; |
| 1470 |
|
| 1471 |
auto *start = &from; |
| 1472 |
for (;;) |
| 1473 |
{ |
| 1474 |
if (start->self == to.self) |
| 1475 |
return true; |
| 1476 |
|
| 1477 |
if (!start->ops.empty()) |
| 1478 |
return false; |
| 1479 |
|
| 1480 |
auto &next = get<SPIRBlock>(start->next_block); |
| 1481 |
// Flushing phi variables does not count as noop. |
| 1482 |
for (auto &phi : next.phi_variables) |
| 1483 |
if (phi.parent == start->self) |
| 1484 |
return false; |
| 1485 |
|
| 1486 |
start = &next; |
| 1487 |
} |
| 1488 |
} |
| 1489 |
|
| 1490 |
bool Compiler::execution_is_branchless(const SPIRBlock &from, const SPIRBlock &to) const |
| 1491 |
{ |
| 1492 |
auto *start = &from; |
| 1493 |
for (;;) |
| 1494 |
{ |
| 1495 |
if (start->self == to.self) |
| 1496 |
return true; |
| 1497 |
|
| 1498 |
if (start->terminator == SPIRBlock::Direct && start->merge == SPIRBlock::MergeNone) |
| 1499 |
start = &get<SPIRBlock>(start->next_block); |
| 1500 |
else |
| 1501 |
return false; |
| 1502 |
} |
| 1503 |
} |
| 1504 |
|
| 1505 |
bool Compiler::execution_is_direct_branch(const SPIRBlock &from, const SPIRBlock &to) const |
| 1506 |
{ |
| 1507 |
return from.terminator == SPIRBlock::Direct && from.merge == SPIRBlock::MergeNone && from.next_block == to.self; |
| 1508 |
} |
| 1509 |
|
| 1510 |
SPIRBlock::ContinueBlockType Compiler::continue_block_type(const SPIRBlock &block) const |
| 1511 |
{ |
| 1512 |
// The block was deemed too complex during code emit, pick conservative fallback paths. |
| 1513 |
if (block.complex_continue) |
| 1514 |
return SPIRBlock::ComplexLoop; |
| 1515 |
|
| 1516 |
// In older glslang output continue block can be equal to the loop header. |
| 1517 |
// In this case, execution is clearly branchless, so just assume a while loop header here. |
| 1518 |
if (block.merge == SPIRBlock::MergeLoop) |
| 1519 |
return SPIRBlock::WhileLoop; |
| 1520 |
|
| 1521 |
if (block.loop_dominator == BlockID(SPIRBlock::NoDominator)) |
| 1522 |
{ |
| 1523 |
// Continue block is never reached from CFG. |
| 1524 |
return SPIRBlock::ComplexLoop; |
| 1525 |
} |
| 1526 |
|
| 1527 |
auto &dominator = get<SPIRBlock>(block.loop_dominator); |
| 1528 |
|
| 1529 |
if (execution_is_noop(block, dominator)) |
| 1530 |
return SPIRBlock::WhileLoop; |
| 1531 |
else if (execution_is_branchless(block, dominator)) |
| 1532 |
return SPIRBlock::ForLoop; |
| 1533 |
else |
| 1534 |
{ |
| 1535 |
const auto *false_block = maybe_get<SPIRBlock>(block.false_block); |
| 1536 |
const auto *true_block = maybe_get<SPIRBlock>(block.true_block); |
| 1537 |
const auto *merge_block = maybe_get<SPIRBlock>(dominator.merge_block); |
| 1538 |
|
| 1539 |
// If we need to flush Phi in this block, we cannot have a DoWhile loop. |
| 1540 |
bool flush_phi_to_false = false_block && flush_phi_required(block.self, block.false_block); |
| 1541 |
bool flush_phi_to_true = true_block && flush_phi_required(block.self, block.true_block); |
| 1542 |
if (flush_phi_to_false || flush_phi_to_true) |
| 1543 |
return SPIRBlock::ComplexLoop; |
| 1544 |
|
| 1545 |
bool positive_do_while = block.true_block == dominator.self && |
| 1546 |
(block.false_block == dominator.merge_block || |
| 1547 |
(false_block && merge_block && execution_is_noop(*false_block, *merge_block))); |
| 1548 |
|
| 1549 |
bool negative_do_while = block.false_block == dominator.self && |
| 1550 |
(block.true_block == dominator.merge_block || |
| 1551 |
(true_block && merge_block && execution_is_noop(*true_block, *merge_block))); |
| 1552 |
|
| 1553 |
if (block.merge == SPIRBlock::MergeNone && block.terminator == SPIRBlock::Select && |
| 1554 |
(positive_do_while || negative_do_while)) |
| 1555 |
{ |
| 1556 |
return SPIRBlock::DoWhileLoop; |
| 1557 |
} |
| 1558 |
else |
| 1559 |
return SPIRBlock::ComplexLoop; |
| 1560 |
} |
| 1561 |
} |
| 1562 |
|
| 1563 |
bool Compiler::traverse_all_reachable_opcodes(const SPIRBlock &block, OpcodeHandler &handler) const |
| 1564 |
{ |
| 1565 |
handler.set_current_block(block); |
| 1566 |
handler.rearm_current_block(block); |
| 1567 |
|
| 1568 |
// Ideally, perhaps traverse the CFG instead of all blocks in order to eliminate dead blocks, |
| 1569 |
// but this shouldn't be a problem in practice unless the SPIR-V is doing insane things like recursing |
| 1570 |
// inside dead blocks ... |
| 1571 |
for (auto &i : block.ops) |
| 1572 |
{ |
| 1573 |
auto ops = stream(i); |
| 1574 |
auto op = static_cast<Op>(i.op); |
| 1575 |
|
| 1576 |
if (!handler.handle(op, ops, i.length)) |
| 1577 |
return false; |
| 1578 |
|
| 1579 |
if (op == OpFunctionCall) |
| 1580 |
{ |
| 1581 |
auto &func = get<SPIRFunction>(ops[2]); |
| 1582 |
if (handler.follow_function_call(func)) |
| 1583 |
{ |
| 1584 |
if (!handler.begin_function_scope(ops, i.length)) |
| 1585 |
return false; |
| 1586 |
if (!traverse_all_reachable_opcodes(get<SPIRFunction>(ops[2]), handler)) |
| 1587 |
return false; |
| 1588 |
if (!handler.end_function_scope(ops, i.length)) |
| 1589 |
return false; |
| 1590 |
|
| 1591 |
handler.rearm_current_block(block); |
| 1592 |
} |
| 1593 |
} |
| 1594 |
} |
| 1595 |
|
| 1596 |
return true; |
| 1597 |
} |
| 1598 |
|
| 1599 |
bool Compiler::traverse_all_reachable_opcodes(const SPIRFunction &func, OpcodeHandler &handler) const |
| 1600 |
{ |
| 1601 |
for (auto block : func.blocks) |
| 1602 |
if (!traverse_all_reachable_opcodes(get<SPIRBlock>(block), handler)) |
| 1603 |
return false; |
| 1604 |
|
| 1605 |
return true; |
| 1606 |
} |
| 1607 |
|
| 1608 |
uint32_t Compiler::type_struct_member_offset(const SPIRType &type, uint32_t index) const |
| 1609 |
{ |
| 1610 |
auto *type_meta = ir.find_meta(type.self); |
| 1611 |
if (type_meta) |
| 1612 |
{ |
| 1613 |
// Decoration must be set in valid SPIR-V, otherwise throw. |
| 1614 |
auto &dec = type_meta->members[index]; |
| 1615 |
if (dec.decoration_flags.get(DecorationOffset)) |
| 1616 |
return dec.offset; |
| 1617 |
else |
| 1618 |
SPIRV_CROSS_THROW("Struct member does not have Offset set."); |
| 1619 |
} |
| 1620 |
else |
| 1621 |
SPIRV_CROSS_THROW("Struct member does not have Offset set."); |
| 1622 |
} |
| 1623 |
|
| 1624 |
uint32_t Compiler::type_struct_member_array_stride(const SPIRType &type, uint32_t index) const |
| 1625 |
{ |
| 1626 |
auto *type_meta = ir.find_meta(type.member_types[index]); |
| 1627 |
if (type_meta) |
| 1628 |
{ |
| 1629 |
// Decoration must be set in valid SPIR-V, otherwise throw. |
| 1630 |
// ArrayStride is part of the array type not OpMemberDecorate. |
| 1631 |
auto &dec = type_meta->decoration; |
| 1632 |
if (dec.decoration_flags.get(DecorationArrayStride)) |
| 1633 |
return dec.array_stride; |
| 1634 |
else |
| 1635 |
SPIRV_CROSS_THROW("Struct member does not have ArrayStride set."); |
| 1636 |
} |
| 1637 |
else |
| 1638 |
SPIRV_CROSS_THROW("Struct member does not have ArrayStride set."); |
| 1639 |
} |
| 1640 |
|
| 1641 |
uint32_t Compiler::type_struct_member_matrix_stride(const SPIRType &type, uint32_t index) const |
| 1642 |
{ |
| 1643 |
auto *type_meta = ir.find_meta(type.self); |
| 1644 |
if (type_meta) |
| 1645 |
{ |
| 1646 |
// Decoration must be set in valid SPIR-V, otherwise throw. |
| 1647 |
// MatrixStride is part of OpMemberDecorate. |
| 1648 |
auto &dec = type_meta->members[index]; |
| 1649 |
if (dec.decoration_flags.get(DecorationMatrixStride)) |
| 1650 |
return dec.matrix_stride; |
| 1651 |
else |
| 1652 |
SPIRV_CROSS_THROW("Struct member does not have MatrixStride set."); |
| 1653 |
} |
| 1654 |
else |
| 1655 |
SPIRV_CROSS_THROW("Struct member does not have MatrixStride set."); |
| 1656 |
} |
| 1657 |
|
| 1658 |
size_t Compiler::get_declared_struct_size(const SPIRType &type) const |
| 1659 |
{ |
| 1660 |
if (type.member_types.empty()) |
| 1661 |
SPIRV_CROSS_THROW("Declared struct in block cannot be empty."); |
| 1662 |
|
| 1663 |
uint32_t last = uint32_t(type.member_types.size() - 1); |
| 1664 |
size_t offset = type_struct_member_offset(type, last); |
| 1665 |
size_t size = get_declared_struct_member_size(type, last); |
| 1666 |
return offset + size; |
| 1667 |
} |
| 1668 |
|
| 1669 |
size_t Compiler::get_declared_struct_size_runtime_array(const SPIRType &type, size_t array_size) const |
| 1670 |
{ |
| 1671 |
if (type.member_types.empty()) |
| 1672 |
SPIRV_CROSS_THROW("Declared struct in block cannot be empty."); |
| 1673 |
|
| 1674 |
size_t size = get_declared_struct_size(type); |
| 1675 |
auto &last_type = get<SPIRType>(type.member_types.back()); |
| 1676 |
if (!last_type.array.empty() && last_type.array_size_literal[0] && last_type.array[0] == 0) // Runtime array |
| 1677 |
size += array_size * type_struct_member_array_stride(type, uint32_t(type.member_types.size() - 1)); |
| 1678 |
|
| 1679 |
return size; |
| 1680 |
} |
| 1681 |
|
| 1682 |
uint32_t Compiler::evaluate_spec_constant_u32(const SPIRConstantOp &spec) const |
| 1683 |
{ |
| 1684 |
auto &result_type = get<SPIRType>(spec.basetype); |
| 1685 |
if (result_type.basetype != SPIRType::UInt && result_type.basetype != SPIRType::Int && |
| 1686 |
result_type.basetype != SPIRType::Boolean) |
| 1687 |
{ |
| 1688 |
SPIRV_CROSS_THROW( |
| 1689 |
"Only 32-bit integers and booleans are currently supported when evaluating specialization constants.\n"); |
| 1690 |
} |
| 1691 |
|
| 1692 |
if (!is_scalar(result_type)) |
| 1693 |
SPIRV_CROSS_THROW("Spec constant evaluation must be a scalar.\n"); |
| 1694 |
|
| 1695 |
uint32_t value = 0; |
| 1696 |
|
| 1697 |
const auto eval_u32 = [&](uint32_t id) -> uint32_t { |
| 1698 |
auto &type = expression_type(id); |
| 1699 |
if (type.basetype != SPIRType::UInt && type.basetype != SPIRType::Int && type.basetype != SPIRType::Boolean) |
| 1700 |
{ |
| 1701 |
SPIRV_CROSS_THROW("Only 32-bit integers and booleans are currently supported when evaluating " |
| 1702 |
"specialization constants.\n"); |
| 1703 |
} |
| 1704 |
|
| 1705 |
if (!is_scalar(type)) |
| 1706 |
SPIRV_CROSS_THROW("Spec constant evaluation must be a scalar.\n"); |
| 1707 |
if (const auto *c = this->maybe_get<SPIRConstant>(id)) |
| 1708 |
return c->scalar(); |
| 1709 |
else |
| 1710 |
return evaluate_spec_constant_u32(this->get<SPIRConstantOp>(id)); |
| 1711 |
}; |
| 1712 |
|
| 1713 |
#define binary_spec_op(op, binary_op) \ |
| 1714 |
case Op##op: \ |
| 1715 |
value = eval_u32(spec.arguments[0]) binary_op eval_u32(spec.arguments[1]); \ |
| 1716 |
break |
| 1717 |
#define binary_spec_op_cast(op, binary_op, type) \ |
| 1718 |
case Op##op: \ |
| 1719 |
value = uint32_t(type(eval_u32(spec.arguments[0])) binary_op type(eval_u32(spec.arguments[1]))); \ |
| 1720 |
break |
| 1721 |
|
| 1722 |
// Support the basic opcodes which are typically used when computing array sizes. |
| 1723 |
switch (spec.opcode) |
| 1724 |
{ |
| 1725 |
binary_spec_op(IAdd, +); |
| 1726 |
binary_spec_op(ISub, -); |
| 1727 |
binary_spec_op(IMul, *); |
| 1728 |
binary_spec_op(BitwiseAnd, &); |
| 1729 |
binary_spec_op(BitwiseOr, |); |
| 1730 |
binary_spec_op(BitwiseXor, ^); |
| 1731 |
binary_spec_op(LogicalAnd, &); |
| 1732 |
binary_spec_op(LogicalOr, |); |
| 1733 |
binary_spec_op(ShiftLeftLogical, <<); |
| 1734 |
binary_spec_op(ShiftRightLogical, >>); |
| 1735 |
binary_spec_op_cast(ShiftRightArithmetic, >>, int32_t); |
| 1736 |
binary_spec_op(LogicalEqual, ==); |
| 1737 |
binary_spec_op(LogicalNotEqual, !=); |
| 1738 |
binary_spec_op(IEqual, ==); |
| 1739 |
binary_spec_op(INotEqual, !=); |
| 1740 |
binary_spec_op(ULessThan, <); |
| 1741 |
binary_spec_op(ULessThanEqual, <=); |
| 1742 |
binary_spec_op(UGreaterThan, >); |
| 1743 |
binary_spec_op(UGreaterThanEqual, >=); |
| 1744 |
binary_spec_op_cast(SLessThan, <, int32_t); |
| 1745 |
binary_spec_op_cast(SLessThanEqual, <=, int32_t); |
| 1746 |
binary_spec_op_cast(SGreaterThan, >, int32_t); |
| 1747 |
binary_spec_op_cast(SGreaterThanEqual, >=, int32_t); |
| 1748 |
#undef binary_spec_op |
| 1749 |
#undef binary_spec_op_cast |
| 1750 |
|
| 1751 |
case OpLogicalNot: |
| 1752 |
value = uint32_t(!eval_u32(spec.arguments[0])); |
| 1753 |
break; |
| 1754 |
|
| 1755 |
case OpNot: |
| 1756 |
value = ~eval_u32(spec.arguments[0]); |
| 1757 |
break; |
| 1758 |
|
| 1759 |
case OpSNegate: |
| 1760 |
value = uint32_t(-int32_t(eval_u32(spec.arguments[0]))); |
| 1761 |
break; |
| 1762 |
|
| 1763 |
case OpSelect: |
| 1764 |
value = eval_u32(spec.arguments[0]) ? eval_u32(spec.arguments[1]) : eval_u32(spec.arguments[2]); |
| 1765 |
break; |
| 1766 |
|
| 1767 |
case OpUMod: |
| 1768 |
{ |
| 1769 |
uint32_t a = eval_u32(spec.arguments[0]); |
| 1770 |
uint32_t b = eval_u32(spec.arguments[1]); |
| 1771 |
if (b == 0) |
| 1772 |
SPIRV_CROSS_THROW("Undefined behavior in UMod, b == 0.\n"); |
| 1773 |
value = a % b; |
| 1774 |
break; |
| 1775 |
} |
| 1776 |
|
| 1777 |
case OpSRem: |
| 1778 |
{ |
| 1779 |
auto a = int32_t(eval_u32(spec.arguments[0])); |
| 1780 |
auto b = int32_t(eval_u32(spec.arguments[1])); |
| 1781 |
if (b == 0) |
| 1782 |
SPIRV_CROSS_THROW("Undefined behavior in SRem, b == 0.\n"); |
| 1783 |
value = a % b; |
| 1784 |
break; |
| 1785 |
} |
| 1786 |
|
| 1787 |
case OpSMod: |
| 1788 |
{ |
| 1789 |
auto a = int32_t(eval_u32(spec.arguments[0])); |
| 1790 |
auto b = int32_t(eval_u32(spec.arguments[1])); |
| 1791 |
if (b == 0) |
| 1792 |
SPIRV_CROSS_THROW("Undefined behavior in SMod, b == 0.\n"); |
| 1793 |
auto v = a % b; |
| 1794 |
|
| 1795 |
// Makes sure we match the sign of b, not a. |
| 1796 |
if ((b < 0 && v > 0) || (b > 0 && v < 0)) |
| 1797 |
v += b; |
| 1798 |
value = v; |
| 1799 |
break; |
| 1800 |
} |
| 1801 |
|
| 1802 |
case OpUDiv: |
| 1803 |
{ |
| 1804 |
uint32_t a = eval_u32(spec.arguments[0]); |
| 1805 |
uint32_t b = eval_u32(spec.arguments[1]); |
| 1806 |
if (b == 0) |
| 1807 |
SPIRV_CROSS_THROW("Undefined behavior in UDiv, b == 0.\n"); |
| 1808 |
value = a / b; |
| 1809 |
break; |
| 1810 |
} |
| 1811 |
|
| 1812 |
case OpSDiv: |
| 1813 |
{ |
| 1814 |
auto a = int32_t(eval_u32(spec.arguments[0])); |
| 1815 |
auto b = int32_t(eval_u32(spec.arguments[1])); |
| 1816 |
if (b == 0) |
| 1817 |
SPIRV_CROSS_THROW("Undefined behavior in SDiv, b == 0.\n"); |
| 1818 |
value = a / b; |
| 1819 |
break; |
| 1820 |
} |
| 1821 |
|
| 1822 |
default: |
| 1823 |
SPIRV_CROSS_THROW("Unsupported spec constant opcode for evaluation.\n"); |
| 1824 |
} |
| 1825 |
|
| 1826 |
return value; |
| 1827 |
} |
| 1828 |
|
| 1829 |
uint32_t Compiler::evaluate_constant_u32(uint32_t id) const |
| 1830 |
{ |
| 1831 |
if (const auto *c = maybe_get<SPIRConstant>(id)) |
| 1832 |
return c->scalar(); |
| 1833 |
else |
| 1834 |
return evaluate_spec_constant_u32(get<SPIRConstantOp>(id)); |
| 1835 |
} |
| 1836 |
|
| 1837 |
size_t Compiler::get_declared_struct_member_size(const SPIRType &struct_type, uint32_t index) const |
| 1838 |
{ |
| 1839 |
if (struct_type.member_types.empty()) |
| 1840 |
SPIRV_CROSS_THROW("Declared struct in block cannot be empty."); |
| 1841 |
|
| 1842 |
auto &flags = get_member_decoration_bitset(struct_type.self, index); |
| 1843 |
auto &type = get<SPIRType>(struct_type.member_types[index]); |
| 1844 |
|
| 1845 |
switch (type.basetype) |
| 1846 |
{ |
| 1847 |
case SPIRType::Unknown: |
| 1848 |
case SPIRType::Void: |
| 1849 |
case SPIRType::Boolean: // Bools are purely logical, and cannot be used for externally visible types. |
| 1850 |
case SPIRType::AtomicCounter: |
| 1851 |
case SPIRType::Image: |
| 1852 |
case SPIRType::SampledImage: |
| 1853 |
case SPIRType::Sampler: |
| 1854 |
SPIRV_CROSS_THROW("Querying size for object with opaque size."); |
| 1855 |
|
| 1856 |
default: |
| 1857 |
break; |
| 1858 |
} |
| 1859 |
|
| 1860 |
if (type.pointer && type.storage == StorageClassPhysicalStorageBuffer) |
| 1861 |
{ |
| 1862 |
// Check if this is a top-level pointer type, and not an array of pointers. |
| 1863 |
if (type.pointer_depth > get<SPIRType>(type.parent_type).pointer_depth) |
| 1864 |
return 8; |
| 1865 |
} |
| 1866 |
|
| 1867 |
if (!type.array.empty()) |
| 1868 |
{ |
| 1869 |
// For arrays, we can use ArrayStride to get an easy check. |
| 1870 |
bool array_size_literal = type.array_size_literal.back(); |
| 1871 |
uint32_t array_size = array_size_literal ? type.array.back() : evaluate_constant_u32(type.array.back()); |
| 1872 |
return type_struct_member_array_stride(struct_type, index) * array_size; |
| 1873 |
} |
| 1874 |
else if (type.basetype == SPIRType::Struct) |
| 1875 |
{ |
| 1876 |
return get_declared_struct_size(type); |
| 1877 |
} |
| 1878 |
else |
| 1879 |
{ |
| 1880 |
unsigned vecsize = type.vecsize; |
| 1881 |
unsigned columns = type.columns; |
| 1882 |
|
| 1883 |
// Vectors. |
| 1884 |
if (columns == 1) |
| 1885 |
{ |
| 1886 |
size_t component_size = type.width / 8; |
| 1887 |
return vecsize * component_size; |
| 1888 |
} |
| 1889 |
else |
| 1890 |
{ |
| 1891 |
uint32_t matrix_stride = type_struct_member_matrix_stride(struct_type, index); |
| 1892 |
|
| 1893 |
// Per SPIR-V spec, matrices must be tightly packed and aligned up for vec3 accesses. |
| 1894 |
if (flags.get(DecorationRowMajor)) |
| 1895 |
return matrix_stride * vecsize; |
| 1896 |
else if (flags.get(DecorationColMajor)) |
| 1897 |
return matrix_stride * columns; |
| 1898 |
else |
| 1899 |
SPIRV_CROSS_THROW("Either row-major or column-major must be declared for matrices."); |
| 1900 |
} |
| 1901 |
} |
| 1902 |
} |
| 1903 |
|
| 1904 |
bool Compiler::BufferAccessHandler::handle(Op opcode, const uint32_t *args, uint32_t length) |
| 1905 |
{ |
| 1906 |
if (opcode != OpAccessChain && opcode != OpInBoundsAccessChain && opcode != OpPtrAccessChain) |
| 1907 |
return true; |
| 1908 |
|
| 1909 |
bool ptr_chain = (opcode == OpPtrAccessChain); |
| 1910 |
|
| 1911 |
// Invalid SPIR-V. |
| 1912 |
if (length < (ptr_chain ? 5u : 4u)) |
| 1913 |
return false; |
| 1914 |
|
| 1915 |
if (args[2] != id) |
| 1916 |
return true; |
| 1917 |
|
| 1918 |
// Don't bother traversing the entire access chain tree yet. |
| 1919 |
// If we access a struct member, assume we access the entire member. |
| 1920 |
uint32_t index = compiler.get<SPIRConstant>(args[ptr_chain ? 4 : 3]).scalar(); |
| 1921 |
|
| 1922 |
// Seen this index already. |
| 1923 |
if (seen.find(index) != end(seen)) |
| 1924 |
return true; |
| 1925 |
seen.insert(index); |
| 1926 |
|
| 1927 |
auto &type = compiler.expression_type(id); |
| 1928 |
uint32_t offset = compiler.type_struct_member_offset(type, index); |
| 1929 |
|
| 1930 |
size_t range; |
| 1931 |
// If we have another member in the struct, deduce the range by looking at the next member. |
| 1932 |
// This is okay since structs in SPIR-V can have padding, but Offset decoration must be |
| 1933 |
// monotonically increasing. |
| 1934 |
// Of course, this doesn't take into account if the SPIR-V for some reason decided to add |
| 1935 |
// very large amounts of padding, but that's not really a big deal. |
| 1936 |
if (index + 1 < type.member_types.size()) |
| 1937 |
{ |
| 1938 |
range = compiler.type_struct_member_offset(type, index + 1) - offset; |
| 1939 |
} |
| 1940 |
else |
| 1941 |
{ |
| 1942 |
// No padding, so just deduce it from the size of the member directly. |
| 1943 |
range = compiler.get_declared_struct_member_size(type, index); |
| 1944 |
} |
| 1945 |
|
| 1946 |
ranges.push_back({ index, offset, range }); |
| 1947 |
return true; |
| 1948 |
} |
| 1949 |
|
| 1950 |
SmallVector<BufferRange> Compiler::get_active_buffer_ranges(VariableID id) const |
| 1951 |
{ |
| 1952 |
SmallVector<BufferRange> ranges; |
| 1953 |
BufferAccessHandler handler(*this, ranges, id); |
| 1954 |
traverse_all_reachable_opcodes(get<SPIRFunction>(ir.default_entry_point), handler); |
| 1955 |
return ranges; |
| 1956 |
} |
| 1957 |
|
| 1958 |
bool Compiler::types_are_logically_equivalent(const SPIRType &a, const SPIRType &b) const |
| 1959 |
{ |
| 1960 |
if (a.basetype != b.basetype) |
| 1961 |
return false; |
| 1962 |
if (a.width != b.width) |
| 1963 |
return false; |
| 1964 |
if (a.vecsize != b.vecsize) |
| 1965 |
return false; |
| 1966 |
if (a.columns != b.columns) |
| 1967 |
return false; |
| 1968 |
if (a.array.size() != b.array.size()) |
| 1969 |
return false; |
| 1970 |
|
| 1971 |
size_t array_count = a.array.size(); |
| 1972 |
if (array_count && memcmp(a.array.data(), b.array.data(), array_count * sizeof(uint32_t)) != 0) |
| 1973 |
return false; |
| 1974 |
|
| 1975 |
if (a.basetype == SPIRType::Image || a.basetype == SPIRType::SampledImage) |
| 1976 |
{ |
| 1977 |
if (memcmp(&a.image, &b.image, sizeof(SPIRType::Image)) != 0) |
| 1978 |
return false; |
| 1979 |
} |
| 1980 |
|
| 1981 |
if (a.member_types.size() != b.member_types.size()) |
| 1982 |
return false; |
| 1983 |
|
| 1984 |
size_t member_types = a.member_types.size(); |
| 1985 |
for (size_t i = 0; i < member_types; i++) |
| 1986 |
{ |
| 1987 |
if (!types_are_logically_equivalent(get<SPIRType>(a.member_types[i]), get<SPIRType>(b.member_types[i]))) |
| 1988 |
return false; |
| 1989 |
} |
| 1990 |
|
| 1991 |
return true; |
| 1992 |
} |
| 1993 |
|
| 1994 |
const Bitset &Compiler::get_execution_mode_bitset() const |
| 1995 |
{ |
| 1996 |
return get_entry_point().flags; |
| 1997 |
} |
| 1998 |
|
| 1999 |
void Compiler::set_execution_mode(ExecutionMode mode, uint32_t arg0, uint32_t arg1, uint32_t arg2) |
| 2000 |
{ |
| 2001 |
auto &execution = get_entry_point(); |
| 2002 |
|
| 2003 |
execution.flags.set(mode); |
| 2004 |
switch (mode) |
| 2005 |
{ |
| 2006 |
case ExecutionModeLocalSize: |
| 2007 |
execution.workgroup_size.x = arg0; |
| 2008 |
execution.workgroup_size.y = arg1; |
| 2009 |
execution.workgroup_size.z = arg2; |
| 2010 |
break; |
| 2011 |
|
| 2012 |
case ExecutionModeInvocations: |
| 2013 |
execution.invocations = arg0; |
| 2014 |
break; |
| 2015 |
|
| 2016 |
case ExecutionModeOutputVertices: |
| 2017 |
execution.output_vertices = arg0; |
| 2018 |
break; |
| 2019 |
|
| 2020 |
default: |
| 2021 |
break; |
| 2022 |
} |
| 2023 |
} |
| 2024 |
|
| 2025 |
void Compiler::unset_execution_mode(ExecutionMode mode) |
| 2026 |
{ |
| 2027 |
auto &execution = get_entry_point(); |
| 2028 |
execution.flags.clear(mode); |
| 2029 |
} |
| 2030 |
|
| 2031 |
uint32_t Compiler::get_work_group_size_specialization_constants(SpecializationConstant &x, SpecializationConstant &y, |
| 2032 |
SpecializationConstant &z) const |
| 2033 |
{ |
| 2034 |
auto &execution = get_entry_point(); |
| 2035 |
x = { 0, 0 }; |
| 2036 |
y = { 0, 0 }; |
| 2037 |
z = { 0, 0 }; |
| 2038 |
|
| 2039 |
if (execution.workgroup_size.constant != 0) |
| 2040 |
{ |
| 2041 |
auto &c = get<SPIRConstant>(execution.workgroup_size.constant); |
| 2042 |
|
| 2043 |
if (c.m.c[0].id[0] != ID(0)) |
| 2044 |
{ |
| 2045 |
x.id = c.m.c[0].id[0]; |
| 2046 |
x.constant_id = get_decoration(c.m.c[0].id[0], DecorationSpecId); |
| 2047 |
} |
| 2048 |
|
| 2049 |
if (c.m.c[0].id[1] != ID(0)) |
| 2050 |
{ |
| 2051 |
y.id = c.m.c[0].id[1]; |
| 2052 |
y.constant_id = get_decoration(c.m.c[0].id[1], DecorationSpecId); |
| 2053 |
} |
| 2054 |
|
| 2055 |
if (c.m.c[0].id[2] != ID(0)) |
| 2056 |
{ |
| 2057 |
z.id = c.m.c[0].id[2]; |
| 2058 |
z.constant_id = get_decoration(c.m.c[0].id[2], DecorationSpecId); |
| 2059 |
} |
| 2060 |
} |
| 2061 |
|
| 2062 |
return execution.workgroup_size.constant; |
| 2063 |
} |
| 2064 |
|
| 2065 |
uint32_t Compiler::get_execution_mode_argument(spv::ExecutionMode mode, uint32_t index) const |
| 2066 |
{ |
| 2067 |
auto &execution = get_entry_point(); |
| 2068 |
switch (mode) |
| 2069 |
{ |
| 2070 |
case ExecutionModeLocalSize: |
| 2071 |
switch (index) |
| 2072 |
{ |
| 2073 |
case 0: |
| 2074 |
return execution.workgroup_size.x; |
| 2075 |
case 1: |
| 2076 |
return execution.workgroup_size.y; |
| 2077 |
case 2: |
| 2078 |
return execution.workgroup_size.z; |
| 2079 |
default: |
| 2080 |
return 0; |
| 2081 |
} |
| 2082 |
|
| 2083 |
case ExecutionModeInvocations: |
| 2084 |
return execution.invocations; |
| 2085 |
|
| 2086 |
case ExecutionModeOutputVertices: |
| 2087 |
return execution.output_vertices; |
| 2088 |
|
| 2089 |
default: |
| 2090 |
return 0; |
| 2091 |
} |
| 2092 |
} |
| 2093 |
|
| 2094 |
ExecutionModel Compiler::get_execution_model() const |
| 2095 |
{ |
| 2096 |
auto &execution = get_entry_point(); |
| 2097 |
return execution.model; |
| 2098 |
} |
| 2099 |
|
| 2100 |
bool Compiler::is_tessellation_shader(ExecutionModel model) |
| 2101 |
{ |
| 2102 |
return model == ExecutionModelTessellationControl || model == ExecutionModelTessellationEvaluation; |
| 2103 |
} |
| 2104 |
|
| 2105 |
bool Compiler::is_vertex_like_shader() const |
| 2106 |
{ |
| 2107 |
auto model = get_execution_model(); |
| 2108 |
return model == ExecutionModelVertex || model == ExecutionModelGeometry || |
| 2109 |
model == ExecutionModelTessellationControl || model == ExecutionModelTessellationEvaluation; |
| 2110 |
} |
| 2111 |
|
| 2112 |
bool Compiler::is_tessellation_shader() const |
| 2113 |
{ |
| 2114 |
return is_tessellation_shader(get_execution_model()); |
| 2115 |
} |
| 2116 |
|
| 2117 |
void Compiler::set_remapped_variable_state(VariableID id, bool remap_enable) |
| 2118 |
{ |
| 2119 |
get<SPIRVariable>(id).remapped_variable = remap_enable; |
| 2120 |
} |
| 2121 |
|
| 2122 |
bool Compiler::get_remapped_variable_state(VariableID id) const |
| 2123 |
{ |
| 2124 |
return get<SPIRVariable>(id).remapped_variable; |
| 2125 |
} |
| 2126 |
|
| 2127 |
void Compiler::set_subpass_input_remapped_components(VariableID id, uint32_t components) |
| 2128 |
{ |
| 2129 |
get<SPIRVariable>(id).remapped_components = components; |
| 2130 |
} |
| 2131 |
|
| 2132 |
uint32_t Compiler::get_subpass_input_remapped_components(VariableID id) const |
| 2133 |
{ |
| 2134 |
return get<SPIRVariable>(id).remapped_components; |
| 2135 |
} |
| 2136 |
|
| 2137 |
void Compiler::add_implied_read_expression(SPIRExpression &e, uint32_t source) |
| 2138 |
{ |
| 2139 |
auto itr = find(begin(e.implied_read_expressions), end(e.implied_read_expressions), ID(source)); |
| 2140 |
if (itr == end(e.implied_read_expressions)) |
| 2141 |
e.implied_read_expressions.push_back(source); |
| 2142 |
} |
| 2143 |
|
| 2144 |
void Compiler::add_implied_read_expression(SPIRAccessChain &e, uint32_t source) |
| 2145 |
{ |
| 2146 |
auto itr = find(begin(e.implied_read_expressions), end(e.implied_read_expressions), ID(source)); |
| 2147 |
if (itr == end(e.implied_read_expressions)) |
| 2148 |
e.implied_read_expressions.push_back(source); |
| 2149 |
} |
| 2150 |
|
| 2151 |
void Compiler::inherit_expression_dependencies(uint32_t dst, uint32_t source_expression) |
| 2152 |
{ |
| 2153 |
// Don't inherit any expression dependencies if the expression in dst |
| 2154 |
// is not a forwarded temporary. |
| 2155 |
if (forwarded_temporaries.find(dst) == end(forwarded_temporaries) || |
| 2156 |
forced_temporaries.find(dst) != end(forced_temporaries)) |
| 2157 |
{ |
| 2158 |
return; |
| 2159 |
} |
| 2160 |
|
| 2161 |
auto &e = get<SPIRExpression>(dst); |
| 2162 |
auto *phi = maybe_get<SPIRVariable>(source_expression); |
| 2163 |
if (phi && phi->phi_variable) |
| 2164 |
{ |
| 2165 |
// We have used a phi variable, which can change at the end of the block, |
| 2166 |
// so make sure we take a dependency on this phi variable. |
| 2167 |
phi->dependees.push_back(dst); |
| 2168 |
} |
| 2169 |
|
| 2170 |
auto *s = maybe_get<SPIRExpression>(source_expression); |
| 2171 |
if (!s) |
| 2172 |
return; |
| 2173 |
|
| 2174 |
auto &e_deps = e.expression_dependencies; |
| 2175 |
auto &s_deps = s->expression_dependencies; |
| 2176 |
|
| 2177 |
// If we depend on a expression, we also depend on all sub-dependencies from source. |
| 2178 |
e_deps.push_back(source_expression); |
| 2179 |
e_deps.insert(end(e_deps), begin(s_deps), end(s_deps)); |
| 2180 |
|
| 2181 |
// Eliminate duplicated dependencies. |
| 2182 |
sort(begin(e_deps), end(e_deps)); |
| 2183 |
e_deps.erase(unique(begin(e_deps), end(e_deps)), end(e_deps)); |
| 2184 |
} |
| 2185 |
|
| 2186 |
SmallVector<EntryPoint> Compiler::get_entry_points_and_stages() const |
| 2187 |
{ |
| 2188 |
SmallVector<EntryPoint> entries; |
| 2189 |
for (auto &entry : ir.entry_points) |
| 2190 |
entries.push_back({ entry.second.orig_name, entry.second.model }); |
| 2191 |
return entries; |
| 2192 |
} |
| 2193 |
|
| 2194 |
void Compiler::rename_entry_point(const std::string &old_name, const std::string &new_name, spv::ExecutionModel model) |
| 2195 |
{ |
| 2196 |
auto &entry = get_entry_point(old_name, model); |
| 2197 |
entry.orig_name = new_name; |
| 2198 |
entry.name = new_name; |
| 2199 |
} |
| 2200 |
|
| 2201 |
void Compiler::set_entry_point(const std::string &name, spv::ExecutionModel model) |
| 2202 |
{ |
| 2203 |
auto &entry = get_entry_point(name, model); |
| 2204 |
ir.default_entry_point = entry.self; |
| 2205 |
} |
| 2206 |
|
| 2207 |
SPIREntryPoint &Compiler::get_first_entry_point(const std::string &name) |
| 2208 |
{ |
| 2209 |
auto itr = find_if( |
| 2210 |
begin(ir.entry_points), end(ir.entry_points), |
| 2211 |
[&](const std::pair<uint32_t, SPIREntryPoint> &entry) -> bool { return entry.second.orig_name == name; }); |
| 2212 |
|
| 2213 |
if (itr == end(ir.entry_points)) |
| 2214 |
SPIRV_CROSS_THROW("Entry point does not exist."); |
| 2215 |
|
| 2216 |
return itr->second; |
| 2217 |
} |
| 2218 |
|
| 2219 |
const SPIREntryPoint &Compiler::get_first_entry_point(const std::string &name) const |
| 2220 |
{ |
| 2221 |
auto itr = find_if( |
| 2222 |
begin(ir.entry_points), end(ir.entry_points), |
| 2223 |
[&](const std::pair<uint32_t, SPIREntryPoint> &entry) -> bool { return entry.second.orig_name == name; }); |
| 2224 |
|
| 2225 |
if (itr == end(ir.entry_points)) |
| 2226 |
SPIRV_CROSS_THROW("Entry point does not exist."); |
| 2227 |
|
| 2228 |
return itr->second; |
| 2229 |
} |
| 2230 |
|
| 2231 |
SPIREntryPoint &Compiler::get_entry_point(const std::string &name, ExecutionModel model) |
| 2232 |
{ |
| 2233 |
auto itr = find_if(begin(ir.entry_points), end(ir.entry_points), |
| 2234 |
[&](const std::pair<uint32_t, SPIREntryPoint> &entry) -> bool { |
| 2235 |
return entry.second.orig_name == name && entry.second.model == model; |
| 2236 |
}); |
| 2237 |
|
| 2238 |
if (itr == end(ir.entry_points)) |
| 2239 |
SPIRV_CROSS_THROW("Entry point does not exist."); |
| 2240 |
|
| 2241 |
return itr->second; |
| 2242 |
} |
| 2243 |
|
| 2244 |
const SPIREntryPoint &Compiler::get_entry_point(const std::string &name, ExecutionModel model) const |
| 2245 |
{ |
| 2246 |
auto itr = find_if(begin(ir.entry_points), end(ir.entry_points), |
| 2247 |
[&](const std::pair<uint32_t, SPIREntryPoint> &entry) -> bool { |
| 2248 |
return entry.second.orig_name == name && entry.second.model == model; |
| 2249 |
}); |
| 2250 |
|
| 2251 |
if (itr == end(ir.entry_points)) |
| 2252 |
SPIRV_CROSS_THROW("Entry point does not exist."); |
| 2253 |
|
| 2254 |
return itr->second; |
| 2255 |
} |
| 2256 |
|
| 2257 |
const string &Compiler::get_cleansed_entry_point_name(const std::string &name, ExecutionModel model) const |
| 2258 |
{ |
| 2259 |
return get_entry_point(name, model).name; |
| 2260 |
} |
| 2261 |
|
| 2262 |
const SPIREntryPoint &Compiler::get_entry_point() const |
| 2263 |
{ |
| 2264 |
return ir.entry_points.find(ir.default_entry_point)->second; |
| 2265 |
} |
| 2266 |
|
| 2267 |
SPIREntryPoint &Compiler::get_entry_point() |
| 2268 |
{ |
| 2269 |
return ir.entry_points.find(ir.default_entry_point)->second; |
| 2270 |
} |
| 2271 |
|
| 2272 |
bool Compiler::interface_variable_exists_in_entry_point(uint32_t id) const |
| 2273 |
{ |
| 2274 |
auto &var = get<SPIRVariable>(id); |
| 2275 |
if (var.storage != StorageClassInput && var.storage != StorageClassOutput && |
| 2276 |
var.storage != StorageClassUniformConstant) |
| 2277 |
SPIRV_CROSS_THROW("Only Input, Output variables and Uniform constants are part of a shader linking interface."); |
| 2278 |
|
| 2279 |
// This is to avoid potential problems with very old glslang versions which did |
| 2280 |
// not emit input/output interfaces properly. |
| 2281 |
// We can assume they only had a single entry point, and single entry point |
| 2282 |
// shaders could easily be assumed to use every interface variable anyways. |
| 2283 |
if (ir.entry_points.size() <= 1) |
| 2284 |
return true; |
| 2285 |
|
| 2286 |
auto &execution = get_entry_point(); |
| 2287 |
return find(begin(execution.interface_variables), end(execution.interface_variables), VariableID(id)) != |
| 2288 |
end(execution.interface_variables); |
| 2289 |
} |
| 2290 |
|
| 2291 |
void Compiler::CombinedImageSamplerHandler::push_remap_parameters(const SPIRFunction &func, const uint32_t *args, |
| 2292 |
uint32_t length) |
| 2293 |
{ |
| 2294 |
// If possible, pipe through a remapping table so that parameters know |
| 2295 |
// which variables they actually bind to in this scope. |
| 2296 |
unordered_map<uint32_t, uint32_t> remapping; |
| 2297 |
for (uint32_t i = 0; i < length; i++) |
| 2298 |
remapping[func.arguments[i].id] = remap_parameter(args[i]); |
| 2299 |
parameter_remapping.push(move(remapping)); |
| 2300 |
} |
| 2301 |
|
| 2302 |
void Compiler::CombinedImageSamplerHandler::pop_remap_parameters() |
| 2303 |
{ |
| 2304 |
parameter_remapping.pop(); |
| 2305 |
} |
| 2306 |
|
| 2307 |
uint32_t Compiler::CombinedImageSamplerHandler::remap_parameter(uint32_t id) |
| 2308 |
{ |
| 2309 |
auto *var = compiler.maybe_get_backing_variable(id); |
| 2310 |
if (var) |
| 2311 |
id = var->self; |
| 2312 |
|
| 2313 |
if (parameter_remapping.empty()) |
| 2314 |
return id; |
| 2315 |
|
| 2316 |
auto &remapping = parameter_remapping.top(); |
| 2317 |
auto itr = remapping.find(id); |
| 2318 |
if (itr != end(remapping)) |
| 2319 |
return itr->second; |
| 2320 |
else |
| 2321 |
return id; |
| 2322 |
} |
| 2323 |
|
| 2324 |
bool Compiler::CombinedImageSamplerHandler::begin_function_scope(const uint32_t *args, uint32_t length) |
| 2325 |
{ |
| 2326 |
if (length < 3) |
| 2327 |
return false; |
| 2328 |
|
| 2329 |
auto &callee = compiler.get<SPIRFunction>(args[2]); |
| 2330 |
args += 3; |
| 2331 |
length -= 3; |
| 2332 |
push_remap_parameters(callee, args, length); |
| 2333 |
functions.push(&callee); |
| 2334 |
return true; |
| 2335 |
} |
| 2336 |
|
| 2337 |
bool Compiler::CombinedImageSamplerHandler::end_function_scope(const uint32_t *args, uint32_t length) |
| 2338 |
{ |
| 2339 |
if (length < 3) |
| 2340 |
return false; |
| 2341 |
|
| 2342 |
auto &callee = compiler.get<SPIRFunction>(args[2]); |
| 2343 |
args += 3; |
| 2344 |
|
| 2345 |
// There are two types of cases we have to handle, |
| 2346 |
// a callee might call sampler2D(texture2D, sampler) directly where |
| 2347 |
// one or more parameters originate from parameters. |
| 2348 |
// Alternatively, we need to provide combined image samplers to our callees, |
| 2349 |
// and in this case we need to add those as well. |
| 2350 |
|
| 2351 |
pop_remap_parameters(); |
| 2352 |
|
| 2353 |
// Our callee has now been processed at least once. |
| 2354 |
// No point in doing it again. |
| 2355 |
callee.do_combined_parameters = false; |
| 2356 |
|
| 2357 |
auto ¶ms = functions.top()->combined_parameters; |
| 2358 |
functions.pop(); |
| 2359 |
if (functions.empty()) |
| 2360 |
return true; |
| 2361 |
|
| 2362 |
auto &caller = *functions.top(); |
| 2363 |
if (caller.do_combined_parameters) |
| 2364 |
{ |
| 2365 |
for (auto ¶m : params) |
| 2366 |
{ |
| 2367 |
VariableID image_id = param.global_image ? param.image_id : VariableID(args[param.image_id]); |
| 2368 |
VariableID sampler_id = param.global_sampler ? param.sampler_id : VariableID(args[param.sampler_id]); |
| 2369 |
|
| 2370 |
auto *i = compiler.maybe_get_backing_variable(image_id); |
| 2371 |
auto *s = compiler.maybe_get_backing_variable(sampler_id); |
| 2372 |
if (i) |
| 2373 |
image_id = i->self; |
| 2374 |
if (s) |
| 2375 |
sampler_id = s->self; |
| 2376 |
|
| 2377 |
register_combined_image_sampler(caller, 0, image_id, sampler_id, param.depth); |
| 2378 |
} |
| 2379 |
} |
| 2380 |
|
| 2381 |
return true; |
| 2382 |
} |
| 2383 |
|
| 2384 |
void Compiler::CombinedImageSamplerHandler::register_combined_image_sampler(SPIRFunction &caller, |
| 2385 |
VariableID combined_module_id, |
| 2386 |
VariableID image_id, VariableID sampler_id, |
| 2387 |
bool depth) |
| 2388 |
{ |
| 2389 |
// We now have a texture ID and a sampler ID which will either be found as a global |
| 2390 |
// or a parameter in our own function. If both are global, they will not need a parameter, |
| 2391 |
// otherwise, add it to our list. |
| 2392 |
SPIRFunction::CombinedImageSamplerParameter param = { |
| 2393 |
0u, image_id, sampler_id, true, true, depth, |
| 2394 |
}; |
| 2395 |
|
| 2396 |
auto texture_itr = find_if(begin(caller.arguments), end(caller.arguments), |
| 2397 |
[image_id](const SPIRFunction::Parameter &p) { return p.id == image_id; }); |
| 2398 |
auto sampler_itr = find_if(begin(caller.arguments), end(caller.arguments), |
| 2399 |
[sampler_id](const SPIRFunction::Parameter &p) { return p.id == sampler_id; }); |
| 2400 |
|
| 2401 |
if (texture_itr != end(caller.arguments)) |
| 2402 |
{ |
| 2403 |
param.global_image = false; |
| 2404 |
param.image_id = uint32_t(texture_itr - begin(caller.arguments)); |
| 2405 |
} |
| 2406 |
|
| 2407 |
if (sampler_itr != end(caller.arguments)) |
| 2408 |
{ |
| 2409 |
param.global_sampler = false; |
| 2410 |
param.sampler_id = uint32_t(sampler_itr - begin(caller.arguments)); |
| 2411 |
} |
| 2412 |
|
| 2413 |
if (param.global_image && param.global_sampler) |
| 2414 |
return; |
| 2415 |
|
| 2416 |
auto itr = find_if(begin(caller.combined_parameters), end(caller.combined_parameters), |
| 2417 |
[¶m](const SPIRFunction::CombinedImageSamplerParameter &p) { |
| 2418 |
return param.image_id == p.image_id && param.sampler_id == p.sampler_id && |
| 2419 |
param.global_image == p.global_image && param.global_sampler == p.global_sampler; |
| 2420 |
}); |
| 2421 |
|
| 2422 |
if (itr == end(caller.combined_parameters)) |
| 2423 |
{ |
| 2424 |
uint32_t id = compiler.ir.increase_bound_by(3); |
| 2425 |
auto type_id = id + 0; |
| 2426 |
auto ptr_type_id = id + 1; |
| 2427 |
auto combined_id = id + 2; |
| 2428 |
auto &base = compiler.expression_type(image_id); |
| 2429 |
auto &type = compiler.set<SPIRType>(type_id); |
| 2430 |
auto &ptr_type = compiler.set<SPIRType>(ptr_type_id); |
| 2431 |
|
| 2432 |
type = base; |
| 2433 |
type.self = type_id; |
| 2434 |
type.basetype = SPIRType::SampledImage; |
| 2435 |
type.pointer = false; |
| 2436 |
type.storage = StorageClassGeneric; |
| 2437 |
type.image.depth = depth; |
| 2438 |
|
| 2439 |
ptr_type = type; |
| 2440 |
ptr_type.pointer = true; |
| 2441 |
ptr_type.storage = StorageClassUniformConstant; |
| 2442 |
ptr_type.parent_type = type_id; |
| 2443 |
|
| 2444 |
// Build new variable. |
| 2445 |
compiler.set<SPIRVariable>(combined_id, ptr_type_id, StorageClassFunction, 0); |
| 2446 |
|
| 2447 |
// Inherit RelaxedPrecision. |
| 2448 |
// If any of OpSampledImage, underlying image or sampler are marked, inherit the decoration. |
| 2449 |
bool relaxed_precision = |
| 2450 |
compiler.has_decoration(sampler_id, DecorationRelaxedPrecision) || |
| 2451 |
compiler.has_decoration(image_id, DecorationRelaxedPrecision) || |
| 2452 |
(combined_module_id && compiler.has_decoration(combined_module_id, DecorationRelaxedPrecision)); |
| 2453 |
|
| 2454 |
if (relaxed_precision) |
| 2455 |
compiler.set_decoration(combined_id, DecorationRelaxedPrecision); |
| 2456 |
|
| 2457 |
param.id = combined_id; |
| 2458 |
|
| 2459 |
compiler.set_name(combined_id, |
| 2460 |
join("SPIRV_Cross_Combined", compiler.to_name(image_id), compiler.to_name(sampler_id))); |
| 2461 |
|
| 2462 |
caller.combined_parameters.push_back(param); |
| 2463 |
caller.shadow_arguments.push_back({ ptr_type_id, combined_id, 0u, 0u, true }); |
| 2464 |
} |
| 2465 |
} |
| 2466 |
|
| 2467 |
bool Compiler::DummySamplerForCombinedImageHandler::handle(Op opcode, const uint32_t *args, uint32_t length) |
| 2468 |
{ |
| 2469 |
if (need_dummy_sampler) |
| 2470 |
{ |
| 2471 |
// No need to traverse further, we know the result. |
| 2472 |
return false; |
| 2473 |
} |
| 2474 |
|
| 2475 |
switch (opcode) |
| 2476 |
{ |
| 2477 |
case OpLoad: |
| 2478 |
{ |
| 2479 |
if (length < 3) |
| 2480 |
return false; |
| 2481 |
|
| 2482 |
uint32_t result_type = args[0]; |
| 2483 |
|
| 2484 |
auto &type = compiler.get<SPIRType>(result_type); |
| 2485 |
bool separate_image = |
| 2486 |
type.basetype == SPIRType::Image && type.image.sampled == 1 && type.image.dim != DimBuffer; |
| 2487 |
|
| 2488 |
// If not separate image, don't bother. |
| 2489 |
if (!separate_image) |
| 2490 |
return true; |
| 2491 |
|
| 2492 |
uint32_t id = args[1]; |
| 2493 |
uint32_t ptr = args[2]; |
| 2494 |
compiler.set<SPIRExpression>(id, "", result_type, true); |
| 2495 |
compiler.register_read(id, ptr, true); |
| 2496 |
break; |
| 2497 |
} |
| 2498 |
|
| 2499 |
case OpImageFetch: |
| 2500 |
case OpImageQuerySizeLod: |
| 2501 |
case OpImageQuerySize: |
| 2502 |
case OpImageQueryLevels: |
| 2503 |
case OpImageQuerySamples: |
| 2504 |
{ |
| 2505 |
// If we are fetching or querying LOD from a plain OpTypeImage, we must pre-combine with our dummy sampler. |
| 2506 |
auto *var = compiler.maybe_get_backing_variable(args[2]); |
| 2507 |
if (var) |
| 2508 |
{ |
| 2509 |
auto &type = compiler.get<SPIRType>(var->basetype); |
| 2510 |
if (type.basetype == SPIRType::Image && type.image.sampled == 1 && type.image.dim != DimBuffer) |
| 2511 |
need_dummy_sampler = true; |
| 2512 |
} |
| 2513 |
|
| 2514 |
break; |
| 2515 |
} |
| 2516 |
|
| 2517 |
case OpInBoundsAccessChain: |
| 2518 |
case OpAccessChain: |
| 2519 |
case OpPtrAccessChain: |
| 2520 |
{ |
| 2521 |
if (length < 3) |
| 2522 |
return false; |
| 2523 |
|
| 2524 |
uint32_t result_type = args[0]; |
| 2525 |
auto &type = compiler.get<SPIRType>(result_type); |
| 2526 |
bool separate_image = |
| 2527 |
type.basetype == SPIRType::Image && type.image.sampled == 1 && type.image.dim != DimBuffer; |
| 2528 |
if (!separate_image) |
| 2529 |
return true; |
| 2530 |
|
| 2531 |
uint32_t id = args[1]; |
| 2532 |
uint32_t ptr = args[2]; |
| 2533 |
compiler.set<SPIRExpression>(id, "", result_type, true); |
| 2534 |
compiler.register_read(id, ptr, true); |
| 2535 |
|
| 2536 |
// Other backends might use SPIRAccessChain for this later. |
| 2537 |
compiler.ir.ids[id].set_allow_type_rewrite(); |
| 2538 |
break; |
| 2539 |
} |
| 2540 |
|
| 2541 |
default: |
| 2542 |
break; |
| 2543 |
} |
| 2544 |
|
| 2545 |
return true; |
| 2546 |
} |
| 2547 |
|
| 2548 |
bool Compiler::CombinedImageSamplerHandler::handle(Op opcode, const uint32_t *args, uint32_t length) |
| 2549 |
{ |
| 2550 |
// We need to figure out where samplers and images are loaded from, so do only the bare bones compilation we need. |
| 2551 |
bool is_fetch = false; |
| 2552 |
|
| 2553 |
switch (opcode) |
| 2554 |
{ |
| 2555 |
case OpLoad: |
| 2556 |
{ |
| 2557 |
if (length < 3) |
| 2558 |
return false; |
| 2559 |
|
| 2560 |
uint32_t result_type = args[0]; |
| 2561 |
|
| 2562 |
auto &type = compiler.get<SPIRType>(result_type); |
| 2563 |
bool separate_image = type.basetype == SPIRType::Image && type.image.sampled == 1; |
| 2564 |
bool separate_sampler = type.basetype == SPIRType::Sampler; |
| 2565 |
|
| 2566 |
// If not separate image or sampler, don't bother. |
| 2567 |
if (!separate_image && !separate_sampler) |
| 2568 |
return true; |
| 2569 |
|
| 2570 |
uint32_t id = args[1]; |
| 2571 |
uint32_t ptr = args[2]; |
| 2572 |
compiler.set<SPIRExpression>(id, "", result_type, true); |
| 2573 |
compiler.register_read(id, ptr, true); |
| 2574 |
return true; |
| 2575 |
} |
| 2576 |
|
| 2577 |
case OpInBoundsAccessChain: |
| 2578 |
case OpAccessChain: |
| 2579 |
case OpPtrAccessChain: |
| 2580 |
{ |
| 2581 |
if (length < 3) |
| 2582 |
return false; |
| 2583 |
|
| 2584 |
// Technically, it is possible to have arrays of textures and arrays of samplers and combine them, but this becomes essentially |
| 2585 |
// impossible to implement, since we don't know which concrete sampler we are accessing. |
| 2586 |
// One potential way is to create a combinatorial explosion where N textures and M samplers are combined into N * M sampler2Ds, |
| 2587 |
// but this seems ridiculously complicated for a problem which is easy to work around. |
| 2588 |
// Checking access chains like this assumes we don't have samplers or textures inside uniform structs, but this makes no sense. |
| 2589 |
|
| 2590 |
uint32_t result_type = args[0]; |
| 2591 |
|
| 2592 |
auto &type = compiler.get<SPIRType>(result_type); |
| 2593 |
bool separate_image = type.basetype == SPIRType::Image && type.image.sampled == 1; |
| 2594 |
bool separate_sampler = type.basetype == SPIRType::Sampler; |
| 2595 |
if (separate_sampler) |
| 2596 |
SPIRV_CROSS_THROW( |
| 2597 |
"Attempting to use arrays or structs of separate samplers. This is not possible to statically " |
| 2598 |
"remap to plain GLSL."); |
| 2599 |
|
| 2600 |
if (separate_image) |
| 2601 |
{ |
| 2602 |
uint32_t id = args[1]; |
| 2603 |
uint32_t ptr = args[2]; |
| 2604 |
compiler.set<SPIRExpression>(id, "", result_type, true); |
| 2605 |
compiler.register_read(id, ptr, true); |
| 2606 |
} |
| 2607 |
return true; |
| 2608 |
} |
| 2609 |
|
| 2610 |
case OpImageFetch: |
| 2611 |
case OpImageQuerySizeLod: |
| 2612 |
case OpImageQuerySize: |
| 2613 |
case OpImageQueryLevels: |
| 2614 |
case OpImageQuerySamples: |
| 2615 |
{ |
| 2616 |
// If we are fetching from a plain OpTypeImage or querying LOD, we must pre-combine with our dummy sampler. |
| 2617 |
auto *var = compiler.maybe_get_backing_variable(args[2]); |
| 2618 |
if (!var) |
| 2619 |
return true; |
| 2620 |
|
| 2621 |
auto &type = compiler.get<SPIRType>(var->basetype); |
| 2622 |
if (type.basetype == SPIRType::Image && type.image.sampled == 1 && type.image.dim != DimBuffer) |
| 2623 |
{ |
| 2624 |
if (compiler.dummy_sampler_id == 0) |
| 2625 |
SPIRV_CROSS_THROW("texelFetch without sampler was found, but no dummy sampler has been created with " |
| 2626 |
"build_dummy_sampler_for_combined_images()."); |
| 2627 |
|
| 2628 |
// Do it outside. |
| 2629 |
is_fetch = true; |
| 2630 |
break; |
| 2631 |
} |
| 2632 |
|
| 2633 |
return true; |
| 2634 |
} |
| 2635 |
|
| 2636 |
case OpSampledImage: |
| 2637 |
// Do it outside. |
| 2638 |
break; |
| 2639 |
|
| 2640 |
default: |
| 2641 |
return true; |
| 2642 |
} |
| 2643 |
|
| 2644 |
// Registers sampler2D calls used in case they are parameters so |
| 2645 |
// that their callees know which combined image samplers to propagate down the call stack. |
| 2646 |
if (!functions.empty()) |
| 2647 |
{ |
| 2648 |
auto &callee = *functions.top(); |
| 2649 |
if (callee.do_combined_parameters) |
| 2650 |
{ |
| 2651 |
uint32_t image_id = args[2]; |
| 2652 |
|
| 2653 |
auto *image = compiler.maybe_get_backing_variable(image_id); |
| 2654 |
if (image) |
| 2655 |
image_id = image->self; |
| 2656 |
|
| 2657 |
uint32_t sampler_id = is_fetch ? compiler.dummy_sampler_id : args[3]; |
| 2658 |
auto *sampler = compiler.maybe_get_backing_variable(sampler_id); |
| 2659 |
if (sampler) |
| 2660 |
sampler_id = sampler->self; |
| 2661 |
|
| 2662 |
uint32_t combined_id = args[1]; |
| 2663 |
|
| 2664 |
auto &combined_type = compiler.get<SPIRType>(args[0]); |
| 2665 |
register_combined_image_sampler(callee, combined_id, image_id, sampler_id, combined_type.image.depth); |
| 2666 |
} |
| 2667 |
} |
| 2668 |
|
| 2669 |
// For function calls, we need to remap IDs which are function parameters into global variables. |
| 2670 |
// This information is statically known from the current place in the call stack. |
| 2671 |
// Function parameters are not necessarily pointers, so if we don't have a backing variable, remapping will know |
| 2672 |
// which backing variable the image/sample came from. |
| 2673 |
VariableID image_id = remap_parameter(args[2]); |
| 2674 |
VariableID sampler_id = is_fetch ? compiler.dummy_sampler_id : remap_parameter(args[3]); |
| 2675 |
|
| 2676 |
auto itr = find_if(begin(compiler.combined_image_samplers), end(compiler.combined_image_samplers), |
| 2677 |
[image_id, sampler_id](const CombinedImageSampler &combined) { |
| 2678 |
return combined.image_id == image_id && combined.sampler_id == sampler_id; |
| 2679 |
}); |
| 2680 |
|
| 2681 |
if (itr == end(compiler.combined_image_samplers)) |
| 2682 |
{ |
| 2683 |
uint32_t sampled_type; |
| 2684 |
uint32_t combined_module_id; |
| 2685 |
if (is_fetch) |
| 2686 |
{ |
| 2687 |
// Have to invent the sampled image type. |
| 2688 |
sampled_type = compiler.ir.increase_bound_by(1); |
| 2689 |
auto &type = compiler.set<SPIRType>(sampled_type); |
| 2690 |
type = compiler.expression_type(args[2]); |
| 2691 |
type.self = sampled_type; |
| 2692 |
type.basetype = SPIRType::SampledImage; |
| 2693 |
type.image.depth = false; |
| 2694 |
combined_module_id = 0; |
| 2695 |
} |
| 2696 |
else |
| 2697 |
{ |
| 2698 |
sampled_type = args[0]; |
| 2699 |
combined_module_id = args[1]; |
| 2700 |
} |
| 2701 |
|
| 2702 |
auto id = compiler.ir.increase_bound_by(2); |
| 2703 |
auto type_id = id + 0; |
| 2704 |
auto combined_id = id + 1; |
| 2705 |
|
| 2706 |
// Make a new type, pointer to OpTypeSampledImage, so we can make a variable of this type. |
| 2707 |
// We will probably have this type lying around, but it doesn't hurt to make duplicates for internal purposes. |
| 2708 |
auto &type = compiler.set<SPIRType>(type_id); |
| 2709 |
auto &base = compiler.get<SPIRType>(sampled_type); |
| 2710 |
type = base; |
| 2711 |
type.pointer = true; |
| 2712 |
type.storage = StorageClassUniformConstant; |
| 2713 |
type.parent_type = type_id; |
| 2714 |
|
| 2715 |
// Build new variable. |
| 2716 |
compiler.set<SPIRVariable>(combined_id, type_id, StorageClassUniformConstant, 0); |
| 2717 |
|
| 2718 |
// Inherit RelaxedPrecision (and potentially other useful flags if deemed relevant). |
| 2719 |
// If any of OpSampledImage, underlying image or sampler are marked, inherit the decoration. |
| 2720 |
bool relaxed_precision = |
| 2721 |
(sampler_id && compiler.has_decoration(sampler_id, DecorationRelaxedPrecision)) || |
| 2722 |
(image_id && compiler.has_decoration(image_id, DecorationRelaxedPrecision)) || |
| 2723 |
(combined_module_id && compiler.has_decoration(combined_module_id, DecorationRelaxedPrecision)); |
| 2724 |
|
| 2725 |
if (relaxed_precision) |
| 2726 |
compiler.set_decoration(combined_id, DecorationRelaxedPrecision); |
| 2727 |
|
| 2728 |
// Propagate the array type for the original image as well. |
| 2729 |
auto *var = compiler.maybe_get_backing_variable(image_id); |
| 2730 |
if (var) |
| 2731 |
{ |
| 2732 |
auto &parent_type = compiler.get<SPIRType>(var->basetype); |
| 2733 |
type.array = parent_type.array; |
| 2734 |
type.array_size_literal = parent_type.array_size_literal; |
| 2735 |
} |
| 2736 |
|
| 2737 |
compiler.combined_image_samplers.push_back({ combined_id, image_id, sampler_id }); |
| 2738 |
} |
| 2739 |
|
| 2740 |
return true; |
| 2741 |
} |
| 2742 |
|
| 2743 |
VariableID Compiler::build_dummy_sampler_for_combined_images() |
| 2744 |
{ |
| 2745 |
DummySamplerForCombinedImageHandler handler(*this); |
| 2746 |
traverse_all_reachable_opcodes(get<SPIRFunction>(ir.default_entry_point), handler); |
| 2747 |
if (handler.need_dummy_sampler) |
| 2748 |
{ |
| 2749 |
uint32_t offset = ir.increase_bound_by(3); |
| 2750 |
auto type_id = offset + 0; |
| 2751 |
auto ptr_type_id = offset + 1; |
| 2752 |
auto var_id = offset + 2; |
| 2753 |
|
| 2754 |
SPIRType sampler_type; |
| 2755 |
auto &sampler = set<SPIRType>(type_id); |
| 2756 |
sampler.basetype = SPIRType::Sampler; |
| 2757 |
|
| 2758 |
auto &ptr_sampler = set<SPIRType>(ptr_type_id); |
| 2759 |
ptr_sampler = sampler; |
| 2760 |
ptr_sampler.self = type_id; |
| 2761 |
ptr_sampler.storage = StorageClassUniformConstant; |
| 2762 |
ptr_sampler.pointer = true; |
| 2763 |
ptr_sampler.parent_type = type_id; |
| 2764 |
|
| 2765 |
set<SPIRVariable>(var_id, ptr_type_id, StorageClassUniformConstant, 0); |
| 2766 |
set_name(var_id, "SPIRV_Cross_DummySampler"); |
| 2767 |
dummy_sampler_id = var_id; |
| 2768 |
return var_id; |
| 2769 |
} |
| 2770 |
else |
| 2771 |
return 0; |
| 2772 |
} |
| 2773 |
|
| 2774 |
void Compiler::build_combined_image_samplers() |
| 2775 |
{ |
| 2776 |
ir.for_each_typed_id<SPIRFunction>([&](uint32_t, SPIRFunction &func) { |
| 2777 |
func.combined_parameters.clear(); |
| 2778 |
func.shadow_arguments.clear(); |
| 2779 |
func.do_combined_parameters = true; |
| 2780 |
}); |
| 2781 |
|
| 2782 |
combined_image_samplers.clear(); |
| 2783 |
CombinedImageSamplerHandler handler(*this); |
| 2784 |
traverse_all_reachable_opcodes(get<SPIRFunction>(ir.default_entry_point), handler); |
| 2785 |
} |
| 2786 |
|
| 2787 |
SmallVector<SpecializationConstant> Compiler::get_specialization_constants() const |
| 2788 |
{ |
| 2789 |
SmallVector<SpecializationConstant> spec_consts; |
| 2790 |
ir.for_each_typed_id<SPIRConstant>([&](uint32_t, const SPIRConstant &c) { |
| 2791 |
if (c.specialization && has_decoration(c.self, DecorationSpecId)) |
| 2792 |
spec_consts.push_back({ c.self, get_decoration(c.self, DecorationSpecId) }); |
| 2793 |
}); |
| 2794 |
return spec_consts; |
| 2795 |
} |
| 2796 |
|
| 2797 |
SPIRConstant &Compiler::get_constant(ConstantID id) |
| 2798 |
{ |
| 2799 |
return get<SPIRConstant>(id); |
| 2800 |
} |
| 2801 |
|
| 2802 |
const SPIRConstant &Compiler::get_constant(ConstantID id) const |
| 2803 |
{ |
| 2804 |
return get<SPIRConstant>(id); |
| 2805 |
} |
| 2806 |
|
| 2807 |
static bool exists_unaccessed_path_to_return(const CFG &cfg, uint32_t block, const unordered_set<uint32_t> &blocks) |
| 2808 |
{ |
| 2809 |
// This block accesses the variable. |
| 2810 |
if (blocks.find(block) != end(blocks)) |
| 2811 |
return false; |
| 2812 |
|
| 2813 |
// We are at the end of the CFG. |
| 2814 |
if (cfg.get_succeeding_edges(block).empty()) |
| 2815 |
return true; |
| 2816 |
|
| 2817 |
// If any of our successors have a path to the end, there exists a path from block. |
| 2818 |
for (auto &succ : cfg.get_succeeding_edges(block)) |
| 2819 |
if (exists_unaccessed_path_to_return(cfg, succ, blocks)) |
| 2820 |
return true; |
| 2821 |
|
| 2822 |
return false; |
| 2823 |
} |
| 2824 |
|
| 2825 |
void Compiler::analyze_parameter_preservation( |
| 2826 |
SPIRFunction &entry, const CFG &cfg, const unordered_map<uint32_t, unordered_set<uint32_t>> &variable_to_blocks, |
| 2827 |
const unordered_map<uint32_t, unordered_set<uint32_t>> &complete_write_blocks) |
| 2828 |
{ |
| 2829 |
for (auto &arg : entry.arguments) |
| 2830 |
{ |
| 2831 |
// Non-pointers are always inputs. |
| 2832 |
auto &type = get<SPIRType>(arg.type); |
| 2833 |
if (!type.pointer) |
| 2834 |
continue; |
| 2835 |
|
| 2836 |
// Opaque argument types are always in |
| 2837 |
bool potential_preserve; |
| 2838 |
switch (type.basetype) |
| 2839 |
{ |
| 2840 |
case SPIRType::Sampler: |
| 2841 |
case SPIRType::Image: |
| 2842 |
case SPIRType::SampledImage: |
| 2843 |
case SPIRType::AtomicCounter: |
| 2844 |
potential_preserve = false; |
| 2845 |
break; |
| 2846 |
|
| 2847 |
default: |
| 2848 |
potential_preserve = true; |
| 2849 |
break; |
| 2850 |
} |
| 2851 |
|
| 2852 |
if (!potential_preserve) |
| 2853 |
continue; |
| 2854 |
|
| 2855 |
auto itr = variable_to_blocks.find(arg.id); |
| 2856 |
if (itr == end(variable_to_blocks)) |
| 2857 |
{ |
| 2858 |
// Variable is never accessed. |
| 2859 |
continue; |
| 2860 |
} |
| 2861 |
|
| 2862 |
// We have accessed a variable, but there was no complete writes to that variable. |
| 2863 |
// We deduce that we must preserve the argument. |
| 2864 |
itr = complete_write_blocks.find(arg.id); |
| 2865 |
if (itr == end(complete_write_blocks)) |
| 2866 |
{ |
| 2867 |
arg.read_count++; |
| 2868 |
continue; |
| 2869 |
} |
| 2870 |
|
| 2871 |
// If there is a path through the CFG where no block completely writes to the variable, the variable will be in an undefined state |
| 2872 |
// when the function returns. We therefore need to implicitly preserve the variable in case there are writers in the function. |
| 2873 |
// Major case here is if a function is |
| 2874 |
// void foo(int &var) { if (cond) var = 10; } |
| 2875 |
// Using read/write counts, we will think it's just an out variable, but it really needs to be inout, |
| 2876 |
// because if we don't write anything whatever we put into the function must return back to the caller. |
| 2877 |
if (exists_unaccessed_path_to_return(cfg, entry.entry_block, itr->second)) |
| 2878 |
arg.read_count++; |
| 2879 |
} |
| 2880 |
} |
| 2881 |
|
| 2882 |
Compiler::AnalyzeVariableScopeAccessHandler::AnalyzeVariableScopeAccessHandler(Compiler &compiler_, |
| 2883 |
SPIRFunction &entry_) |
| 2884 |
: compiler(compiler_) |
| 2885 |
, entry(entry_) |
| 2886 |
{ |
| 2887 |
} |
| 2888 |
|
| 2889 |
bool Compiler::AnalyzeVariableScopeAccessHandler::follow_function_call(const SPIRFunction &) |
| 2890 |
{ |
| 2891 |
// Only analyze within this function. |
| 2892 |
return false; |
| 2893 |
} |
| 2894 |
|
| 2895 |
void Compiler::AnalyzeVariableScopeAccessHandler::set_current_block(const SPIRBlock &block) |
| 2896 |
{ |
| 2897 |
current_block = █ |
| 2898 |
|
| 2899 |
// If we're branching to a block which uses OpPhi, in GLSL |
| 2900 |
// this will be a variable write when we branch, |
| 2901 |
// so we need to track access to these variables as well to |
| 2902 |
// have a complete picture. |
| 2903 |
const auto test_phi = [this, &block](uint32_t to) { |
| 2904 |
auto &next = compiler.get<SPIRBlock>(to); |
| 2905 |
for (auto &phi : next.phi_variables) |
| 2906 |
{ |
| 2907 |
if (phi.parent == block.self) |
| 2908 |
{ |
| 2909 |
accessed_variables_to_block[phi.function_variable].insert(block.self); |
| 2910 |
// Phi variables are also accessed in our target branch block. |
| 2911 |
accessed_variables_to_block[phi.function_variable].insert(next.self); |
| 2912 |
|
| 2913 |
notify_variable_access(phi.local_variable, block.self); |
| 2914 |
} |
| 2915 |
} |
| 2916 |
}; |
| 2917 |
|
| 2918 |
switch (block.terminator) |
| 2919 |
{ |
| 2920 |
case SPIRBlock::Direct: |
| 2921 |
notify_variable_access(block.condition, block.self); |
| 2922 |
test_phi(block.next_block); |
| 2923 |
break; |
| 2924 |
|
| 2925 |
case SPIRBlock::Select: |
| 2926 |
notify_variable_access(block.condition, block.self); |
| 2927 |
test_phi(block.true_block); |
| 2928 |
test_phi(block.false_block); |
| 2929 |
break; |
| 2930 |
|
| 2931 |
case SPIRBlock::MultiSelect: |
| 2932 |
notify_variable_access(block.condition, block.self); |
| 2933 |
for (auto &target : block.cases) |
| 2934 |
test_phi(target.block); |
| 2935 |
if (block.default_block) |
| 2936 |
test_phi(block.default_block); |
| 2937 |
break; |
| 2938 |
|
| 2939 |
default: |
| 2940 |
break; |
| 2941 |
} |
| 2942 |
} |
| 2943 |
|
| 2944 |
void Compiler::AnalyzeVariableScopeAccessHandler::notify_variable_access(uint32_t id, uint32_t block) |
| 2945 |
{ |
| 2946 |
if (id == 0) |
| 2947 |
return; |
| 2948 |
|
| 2949 |
// Access chains used in multiple blocks mean hoisting all the variables used to construct the access chain as not all backends can use pointers. |
| 2950 |
auto itr = access_chain_children.find(id); |
| 2951 |
if (itr != end(access_chain_children)) |
| 2952 |
for (auto child_id : itr->second) |
| 2953 |
notify_variable_access(child_id, block); |
| 2954 |
|
| 2955 |
if (id_is_phi_variable(id)) |
| 2956 |
accessed_variables_to_block[id].insert(block); |
| 2957 |
else if (id_is_potential_temporary(id)) |
| 2958 |
accessed_temporaries_to_block[id].insert(block); |
| 2959 |
} |
| 2960 |
|
| 2961 |
bool Compiler::AnalyzeVariableScopeAccessHandler::id_is_phi_variable(uint32_t id) const |
| 2962 |
{ |
| 2963 |
if (id >= compiler.get_current_id_bound()) |
| 2964 |
return false; |
| 2965 |
auto *var = compiler.maybe_get<SPIRVariable>(id); |
| 2966 |
return var && var->phi_variable; |
| 2967 |
} |
| 2968 |
|
| 2969 |
bool Compiler::AnalyzeVariableScopeAccessHandler::id_is_potential_temporary(uint32_t id) const |
| 2970 |
{ |
| 2971 |
if (id >= compiler.get_current_id_bound()) |
| 2972 |
return false; |
| 2973 |
|
| 2974 |
// Temporaries are not created before we start emitting code. |
| 2975 |
return compiler.ir.ids[id].empty() || (compiler.ir.ids[id].get_type() == TypeExpression); |
| 2976 |
} |
| 2977 |
|
| 2978 |
bool Compiler::AnalyzeVariableScopeAccessHandler::handle(spv::Op op, const uint32_t *args, uint32_t length) |
| 2979 |
{ |
| 2980 |
// Keep track of the types of temporaries, so we can hoist them out as necessary. |
| 2981 |
uint32_t result_type, result_id; |
| 2982 |
if (compiler.instruction_to_result_type(result_type, result_id, op, args, length)) |
| 2983 |
result_id_to_type[result_id] = result_type; |
| 2984 |
|
| 2985 |
switch (op) |
| 2986 |
{ |
| 2987 |
case OpStore: |
| 2988 |
{ |
| 2989 |
if (length < 2) |
| 2990 |
return false; |
| 2991 |
|
| 2992 |
ID ptr = args[0]; |
| 2993 |
auto *var = compiler.maybe_get_backing_variable(ptr); |
| 2994 |
|
| 2995 |
// If we store through an access chain, we have a partial write. |
| 2996 |
if (var) |
| 2997 |
{ |
| 2998 |
accessed_variables_to_block[var->self].insert(current_block->self); |
| 2999 |
if (var->self == ptr) |
| 3000 |
complete_write_variables_to_block[var->self].insert(current_block->self); |
| 3001 |
else |
| 3002 |
partial_write_variables_to_block[var->self].insert(current_block->self); |
| 3003 |
} |
| 3004 |
|
| 3005 |
// args[0] might be an access chain we have to track use of. |
| 3006 |
notify_variable_access(args[0], current_block->self); |
| 3007 |
// Might try to store a Phi variable here. |
| 3008 |
notify_variable_access(args[1], current_block->self); |
| 3009 |
break; |
| 3010 |
} |
| 3011 |
|
| 3012 |
case OpAccessChain: |
| 3013 |
case OpInBoundsAccessChain: |
| 3014 |
case OpPtrAccessChain: |
| 3015 |
{ |
| 3016 |
if (length < 3) |
| 3017 |
return false; |
| 3018 |
|
| 3019 |
// Access chains used in multiple blocks mean hoisting all the variables used to construct the access chain as not all backends can use pointers. |
| 3020 |
uint32_t ptr = args[2]; |
| 3021 |
auto *var = compiler.maybe_get<SPIRVariable>(ptr); |
| 3022 |
if (var) |
| 3023 |
{ |
| 3024 |
accessed_variables_to_block[var->self].insert(current_block->self); |
| 3025 |
access_chain_children[args[1]].insert(var->self); |
| 3026 |
} |
| 3027 |
|
| 3028 |
// args[2] might be another access chain we have to track use of. |
| 3029 |
for (uint32_t i = 2; i < length; i++) |
| 3030 |
{ |
| 3031 |
notify_variable_access(args[i], current_block->self); |
| 3032 |
access_chain_children[args[1]].insert(args[i]); |
| 3033 |
} |
| 3034 |
|
| 3035 |
// Also keep track of the access chain pointer itself. |
| 3036 |
// In exceptionally rare cases, we can end up with a case where |
| 3037 |
// the access chain is generated in the loop body, but is consumed in continue block. |
| 3038 |
// This means we need complex loop workarounds, and we must detect this via CFG analysis. |
| 3039 |
notify_variable_access(args[1], current_block->self); |
| 3040 |
|
| 3041 |
// The result of an access chain is a fixed expression and is not really considered a temporary. |
| 3042 |
auto &e = compiler.set<SPIRExpression>(args[1], "", args[0], true); |
| 3043 |
auto *backing_variable = compiler.maybe_get_backing_variable(ptr); |
| 3044 |
e.loaded_from = backing_variable ? VariableID(backing_variable->self) : VariableID(0); |
| 3045 |
|
| 3046 |
// Other backends might use SPIRAccessChain for this later. |
| 3047 |
compiler.ir.ids[args[1]].set_allow_type_rewrite(); |
| 3048 |
access_chain_expressions.insert(args[1]); |
| 3049 |
break; |
| 3050 |
} |
| 3051 |
|
| 3052 |
case OpCopyMemory: |
| 3053 |
{ |
| 3054 |
if (length < 2) |
| 3055 |
return false; |
| 3056 |
|
| 3057 |
ID lhs = args[0]; |
| 3058 |
ID rhs = args[1]; |
| 3059 |
auto *var = compiler.maybe_get_backing_variable(lhs); |
| 3060 |
|
| 3061 |
// If we store through an access chain, we have a partial write. |
| 3062 |
if (var) |
| 3063 |
{ |
| 3064 |
accessed_variables_to_block[var->self].insert(current_block->self); |
| 3065 |
if (var->self == lhs) |
| 3066 |
complete_write_variables_to_block[var->self].insert(current_block->self); |
| 3067 |
else |
| 3068 |
partial_write_variables_to_block[var->self].insert(current_block->self); |
| 3069 |
} |
| 3070 |
|
| 3071 |
// args[0:1] might be access chains we have to track use of. |
| 3072 |
for (uint32_t i = 0; i < 2; i++) |
| 3073 |
notify_variable_access(args[i], current_block->self); |
| 3074 |
|
| 3075 |
var = compiler.maybe_get_backing_variable(rhs); |
| 3076 |
if (var) |
| 3077 |
accessed_variables_to_block[var->self].insert(current_block->self); |
| 3078 |
break; |
| 3079 |
} |
| 3080 |
|
| 3081 |
case OpCopyObject: |
| 3082 |
{ |
| 3083 |
if (length < 3) |
| 3084 |
return false; |
| 3085 |
|
| 3086 |
auto *var = compiler.maybe_get_backing_variable(args[2]); |
| 3087 |
if (var) |
| 3088 |
accessed_variables_to_block[var->self].insert(current_block->self); |
| 3089 |
|
| 3090 |
// Might be an access chain which we have to keep track of. |
| 3091 |
notify_variable_access(args[1], current_block->self); |
| 3092 |
if (access_chain_expressions.count(args[2])) |
| 3093 |
access_chain_expressions.insert(args[1]); |
| 3094 |
|
| 3095 |
// Might try to copy a Phi variable here. |
| 3096 |
notify_variable_access(args[2], current_block->self); |
| 3097 |
break; |
| 3098 |
} |
| 3099 |
|
| 3100 |
case OpLoad: |
| 3101 |
{ |
| 3102 |
if (length < 3) |
| 3103 |
return false; |
| 3104 |
uint32_t ptr = args[2]; |
| 3105 |
auto *var = compiler.maybe_get_backing_variable(ptr); |
| 3106 |
if (var) |
| 3107 |
accessed_variables_to_block[var->self].insert(current_block->self); |
| 3108 |
|
| 3109 |
// Loaded value is a temporary. |
| 3110 |
notify_variable_access(args[1], current_block->self); |
| 3111 |
|
| 3112 |
// Might be an access chain we have to track use of. |
| 3113 |
notify_variable_access(args[2], current_block->self); |
| 3114 |
break; |
| 3115 |
} |
| 3116 |
|
| 3117 |
case OpFunctionCall: |
| 3118 |
{ |
| 3119 |
if (length < 3) |
| 3120 |
return false; |
| 3121 |
|
| 3122 |
// Return value may be a temporary. |
| 3123 |
if (compiler.get_type(args[0]).basetype != SPIRType::Void) |
| 3124 |
notify_variable_access(args[1], current_block->self); |
| 3125 |
|
| 3126 |
length -= 3; |
| 3127 |
args += 3; |
| 3128 |
|
| 3129 |
for (uint32_t i = 0; i < length; i++) |
| 3130 |
{ |
| 3131 |
auto *var = compiler.maybe_get_backing_variable(args[i]); |
| 3132 |
if (var) |
| 3133 |
{ |
| 3134 |
accessed_variables_to_block[var->self].insert(current_block->self); |
| 3135 |
// Assume we can get partial writes to this variable. |
| 3136 |
partial_write_variables_to_block[var->self].insert(current_block->self); |
| 3137 |
} |
| 3138 |
|
| 3139 |
// Cannot easily prove if argument we pass to a function is completely written. |
| 3140 |
// Usually, functions write to a dummy variable, |
| 3141 |
// which is then copied to in full to the real argument. |
| 3142 |
|
| 3143 |
// Might try to copy a Phi variable here. |
| 3144 |
notify_variable_access(args[i], current_block->self); |
| 3145 |
} |
| 3146 |
break; |
| 3147 |
} |
| 3148 |
|
| 3149 |
case OpExtInst: |
| 3150 |
{ |
| 3151 |
for (uint32_t i = 4; i < length; i++) |
| 3152 |
notify_variable_access(args[i], current_block->self); |
| 3153 |
notify_variable_access(args[1], current_block->self); |
| 3154 |
break; |
| 3155 |
} |
| 3156 |
|
| 3157 |
case OpArrayLength: |
| 3158 |
case OpLine: |
| 3159 |
case OpNoLine: |
| 3160 |
// Uses literals, but cannot be a phi variable or temporary, so ignore. |
| 3161 |
break; |
| 3162 |
|
| 3163 |
// Atomics shouldn't be able to access function-local variables. |
| 3164 |
// Some GLSL builtins access a pointer. |
| 3165 |
|
| 3166 |
case OpCompositeInsert: |
| 3167 |
case OpVectorShuffle: |
| 3168 |
// Specialize for opcode which contains literals. |
| 3169 |
for (uint32_t i = 1; i < 4; i++) |
| 3170 |
notify_variable_access(args[i], current_block->self); |
| 3171 |
break; |
| 3172 |
|
| 3173 |
case OpCompositeExtract: |
| 3174 |
// Specialize for opcode which contains literals. |
| 3175 |
for (uint32_t i = 1; i < 3; i++) |
| 3176 |
notify_variable_access(args[i], current_block->self); |
| 3177 |
break; |
| 3178 |
|
| 3179 |
case OpImageWrite: |
| 3180 |
for (uint32_t i = 0; i < length; i++) |
| 3181 |
{ |
| 3182 |
// Argument 3 is a literal. |
| 3183 |
if (i != 3) |
| 3184 |
notify_variable_access(args[i], current_block->self); |
| 3185 |
} |
| 3186 |
break; |
| 3187 |
|
| 3188 |
case OpImageSampleImplicitLod: |
| 3189 |
case OpImageSampleExplicitLod: |
| 3190 |
case OpImageSparseSampleImplicitLod: |
| 3191 |
case OpImageSparseSampleExplicitLod: |
| 3192 |
case OpImageSampleProjImplicitLod: |
| 3193 |
case OpImageSampleProjExplicitLod: |
| 3194 |
case OpImageSparseSampleProjImplicitLod: |
| 3195 |
case OpImageSparseSampleProjExplicitLod: |
| 3196 |
case OpImageFetch: |
| 3197 |
case OpImageSparseFetch: |
| 3198 |
case OpImageRead: |
| 3199 |
case OpImageSparseRead: |
| 3200 |
for (uint32_t i = 1; i < length; i++) |
| 3201 |
{ |
| 3202 |
// Argument 4 is a literal. |
| 3203 |
if (i != 4) |
| 3204 |
notify_variable_access(args[i], current_block->self); |
| 3205 |
} |
| 3206 |
break; |
| 3207 |
|
| 3208 |
case OpImageSampleDrefImplicitLod: |
| 3209 |
case OpImageSampleDrefExplicitLod: |
| 3210 |
case OpImageSparseSampleDrefImplicitLod: |
| 3211 |
case OpImageSparseSampleDrefExplicitLod: |
| 3212 |
case OpImageSampleProjDrefImplicitLod: |
| 3213 |
case OpImageSampleProjDrefExplicitLod: |
| 3214 |
case OpImageSparseSampleProjDrefImplicitLod: |
| 3215 |
case OpImageSparseSampleProjDrefExplicitLod: |
| 3216 |
case OpImageGather: |
| 3217 |
case OpImageSparseGather: |
| 3218 |
case OpImageDrefGather: |
| 3219 |
case OpImageSparseDrefGather: |
| 3220 |
for (uint32_t i = 1; i < length; i++) |
| 3221 |
{ |
| 3222 |
// Argument 5 is a literal. |
| 3223 |
if (i != 5) |
| 3224 |
notify_variable_access(args[i], current_block->self); |
| 3225 |
} |
| 3226 |
break; |
| 3227 |
|
| 3228 |
default: |
| 3229 |
{ |
| 3230 |
// Rather dirty way of figuring out where Phi variables are used. |
| 3231 |
// As long as only IDs are used, we can scan through instructions and try to find any evidence that |
| 3232 |
// the ID of a variable has been used. |
| 3233 |
// There are potential false positives here where a literal is used in-place of an ID, |
| 3234 |
// but worst case, it does not affect the correctness of the compile. |
| 3235 |
// Exhaustive analysis would be better here, but it's not worth it for now. |
| 3236 |
for (uint32_t i = 0; i < length; i++) |
| 3237 |
notify_variable_access(args[i], current_block->self); |
| 3238 |
break; |
| 3239 |
} |
| 3240 |
} |
| 3241 |
return true; |
| 3242 |
} |
| 3243 |
|
| 3244 |
Compiler::StaticExpressionAccessHandler::StaticExpressionAccessHandler(Compiler &compiler_, uint32_t variable_id_) |
| 3245 |
: compiler(compiler_) |
| 3246 |
, variable_id(variable_id_) |
| 3247 |
{ |
| 3248 |
} |
| 3249 |
|
| 3250 |
bool Compiler::StaticExpressionAccessHandler::follow_function_call(const SPIRFunction &) |
| 3251 |
{ |
| 3252 |
return false; |
| 3253 |
} |
| 3254 |
|
| 3255 |
bool Compiler::StaticExpressionAccessHandler::handle(spv::Op op, const uint32_t *args, uint32_t length) |
| 3256 |
{ |
| 3257 |
switch (op) |
| 3258 |
{ |
| 3259 |
case OpStore: |
| 3260 |
if (length < 2) |
| 3261 |
return false; |
| 3262 |
if (args[0] == variable_id) |
| 3263 |
{ |
| 3264 |
static_expression = args[1]; |
| 3265 |
write_count++; |
| 3266 |
} |
| 3267 |
break; |
| 3268 |
|
| 3269 |
case OpLoad: |
| 3270 |
if (length < 3) |
| 3271 |
return false; |
| 3272 |
if (args[2] == variable_id && static_expression == 0) // Tried to read from variable before it was initialized. |
| 3273 |
return false; |
| 3274 |
break; |
| 3275 |
|
| 3276 |
case OpAccessChain: |
| 3277 |
case OpInBoundsAccessChain: |
| 3278 |
case OpPtrAccessChain: |
| 3279 |
if (length < 3) |
| 3280 |
return false; |
| 3281 |
if (args[2] == variable_id) // If we try to access chain our candidate variable before we store to it, bail. |
| 3282 |
return false; |
| 3283 |
break; |
| 3284 |
|
| 3285 |
default: |
| 3286 |
break; |
| 3287 |
} |
| 3288 |
|
| 3289 |
return true; |
| 3290 |
} |
| 3291 |
|
| 3292 |
void Compiler::find_function_local_luts(SPIRFunction &entry, const AnalyzeVariableScopeAccessHandler &handler, |
| 3293 |
bool single_function) |
| 3294 |
{ |
| 3295 |
auto &cfg = *function_cfgs.find(entry.self)->second; |
| 3296 |
|
| 3297 |
// For each variable which is statically accessed. |
| 3298 |
for (auto &accessed_var : handler.accessed_variables_to_block) |
| 3299 |
{ |
| 3300 |
auto &blocks = accessed_var.second; |
| 3301 |
auto &var = get<SPIRVariable>(accessed_var.first); |
| 3302 |
auto &type = expression_type(accessed_var.first); |
| 3303 |
|
| 3304 |
// Only consider function local variables here. |
| 3305 |
// If we only have a single function in our CFG, private storage is also fine, |
| 3306 |
// since it behaves like a function local variable. |
| 3307 |
bool allow_lut = var.storage == StorageClassFunction || (single_function && var.storage == StorageClassPrivate); |
| 3308 |
if (!allow_lut) |
| 3309 |
continue; |
| 3310 |
|
| 3311 |
// We cannot be a phi variable. |
| 3312 |
if (var.phi_variable) |
| 3313 |
continue; |
| 3314 |
|
| 3315 |
// Only consider arrays here. |
| 3316 |
if (type.array.empty()) |
| 3317 |
continue; |
| 3318 |
|
| 3319 |
// If the variable has an initializer, make sure it is a constant expression. |
| 3320 |
uint32_t static_constant_expression = 0; |
| 3321 |
if (var.initializer) |
| 3322 |
{ |
| 3323 |
if (ir.ids[var.initializer].get_type() != TypeConstant) |
| 3324 |
continue; |
| 3325 |
static_constant_expression = var.initializer; |
| 3326 |
|
| 3327 |
// There can be no stores to this variable, we have now proved we have a LUT. |
| 3328 |
if (handler.complete_write_variables_to_block.count(var.self) != 0 || |
| 3329 |
handler.partial_write_variables_to_block.count(var.self) != 0) |
| 3330 |
continue; |
| 3331 |
} |
| 3332 |
else |
| 3333 |
{ |
| 3334 |
// We can have one, and only one write to the variable, and that write needs to be a constant. |
| 3335 |
|
| 3336 |
// No partial writes allowed. |
| 3337 |
if (handler.partial_write_variables_to_block.count(var.self) != 0) |
| 3338 |
continue; |
| 3339 |
|
| 3340 |
auto itr = handler.complete_write_variables_to_block.find(var.self); |
| 3341 |
|
| 3342 |
// No writes? |
| 3343 |
if (itr == end(handler.complete_write_variables_to_block)) |
| 3344 |
continue; |
| 3345 |
|
| 3346 |
// We write to the variable in more than one block. |
| 3347 |
auto &write_blocks = itr->second; |
| 3348 |
if (write_blocks.size() != 1) |
| 3349 |
continue; |
| 3350 |
|
| 3351 |
// The write needs to happen in the dominating block. |
| 3352 |
DominatorBuilder builder(cfg); |
| 3353 |
for (auto &block : blocks) |
| 3354 |
builder.add_block(block); |
| 3355 |
uint32_t dominator = builder.get_dominator(); |
| 3356 |
|
| 3357 |
// The complete write happened in a branch or similar, cannot deduce static expression. |
| 3358 |
if (write_blocks.count(dominator) == 0) |
| 3359 |
continue; |
| 3360 |
|
| 3361 |
// Find the static expression for this variable. |
| 3362 |
StaticExpressionAccessHandler static_expression_handler(*this, var.self); |
| 3363 |
traverse_all_reachable_opcodes(get<SPIRBlock>(dominator), static_expression_handler); |
| 3364 |
|
| 3365 |
// We want one, and exactly one write |
| 3366 |
if (static_expression_handler.write_count != 1 || static_expression_handler.static_expression == 0) |
| 3367 |
continue; |
| 3368 |
|
| 3369 |
// Is it a constant expression? |
| 3370 |
if (ir.ids[static_expression_handler.static_expression].get_type() != TypeConstant) |
| 3371 |
continue; |
| 3372 |
|
| 3373 |
// We found a LUT! |
| 3374 |
static_constant_expression = static_expression_handler.static_expression; |
| 3375 |
} |
| 3376 |
|
| 3377 |
get<SPIRConstant>(static_constant_expression).is_used_as_lut = true; |
| 3378 |
var.static_expression = static_constant_expression; |
| 3379 |
var.statically_assigned = true; |
| 3380 |
var.remapped_variable = true; |
| 3381 |
} |
| 3382 |
} |
| 3383 |
|
| 3384 |
void Compiler::analyze_variable_scope(SPIRFunction &entry, AnalyzeVariableScopeAccessHandler &handler) |
| 3385 |
{ |
| 3386 |
// First, we map out all variable access within a function. |
| 3387 |
// Essentially a map of block -> { variables accessed in the basic block } |
| 3388 |
traverse_all_reachable_opcodes(entry, handler); |
| 3389 |
|
| 3390 |
auto &cfg = *function_cfgs.find(entry.self)->second; |
| 3391 |
|
| 3392 |
// Analyze if there are parameters which need to be implicitly preserved with an "in" qualifier. |
| 3393 |
analyze_parameter_preservation(entry, cfg, handler.accessed_variables_to_block, |
| 3394 |
handler.complete_write_variables_to_block); |
| 3395 |
|
| 3396 |
unordered_map<uint32_t, uint32_t> potential_loop_variables; |
| 3397 |
|
| 3398 |
// Find the loop dominator block for each block. |
| 3399 |
for (auto &block_id : entry.blocks) |
| 3400 |
{ |
| 3401 |
auto &block = get<SPIRBlock>(block_id); |
| 3402 |
|
| 3403 |
auto itr = ir.continue_block_to_loop_header.find(block_id); |
| 3404 |
if (itr != end(ir.continue_block_to_loop_header) && itr->second != block_id) |
| 3405 |
{ |
| 3406 |
// Continue block might be unreachable in the CFG, but we still like to know the loop dominator. |
| 3407 |
// Edge case is when continue block is also the loop header, don't set the dominator in this case. |
| 3408 |
block.loop_dominator = itr->second; |
| 3409 |
} |
| 3410 |
else |
| 3411 |
{ |
| 3412 |
uint32_t loop_dominator = cfg.find_loop_dominator(block_id); |
| 3413 |
if (loop_dominator != block_id) |
| 3414 |
block.loop_dominator = loop_dominator; |
| 3415 |
else |
| 3416 |
block.loop_dominator = SPIRBlock::NoDominator; |
| 3417 |
} |
| 3418 |
} |
| 3419 |
|
| 3420 |
// For each variable which is statically accessed. |
| 3421 |
for (auto &var : handler.accessed_variables_to_block) |
| 3422 |
{ |
| 3423 |
// Only deal with variables which are considered local variables in this function. |
| 3424 |
if (find(begin(entry.local_variables), end(entry.local_variables), VariableID(var.first)) == |
| 3425 |
end(entry.local_variables)) |
| 3426 |
continue; |
| 3427 |
|
| 3428 |
DominatorBuilder builder(cfg); |
| 3429 |
auto &blocks = var.second; |
| 3430 |
auto &type = expression_type(var.first); |
| 3431 |
|
| 3432 |
// Figure out which block is dominating all accesses of those variables. |
| 3433 |
for (auto &block : blocks) |
| 3434 |
{ |
| 3435 |
// If we're accessing a variable inside a continue block, this variable might be a loop variable. |
| 3436 |
// We can only use loop variables with scalars, as we cannot track static expressions for vectors. |
| 3437 |
if (is_continue(block)) |
| 3438 |
{ |
| 3439 |
// Potentially awkward case to check for. |
| 3440 |
// We might have a variable inside a loop, which is touched by the continue block, |
| 3441 |
// but is not actually a loop variable. |
| 3442 |
// The continue block is dominated by the inner part of the loop, which does not make sense in high-level |
| 3443 |
// language output because it will be declared before the body, |
| 3444 |
// so we will have to lift the dominator up to the relevant loop header instead. |
| 3445 |
builder.add_block(ir.continue_block_to_loop_header[block]); |
| 3446 |
|
| 3447 |
// Arrays or structs cannot be loop variables. |
| 3448 |
if (type.vecsize == 1 && type.columns == 1 && type.basetype != SPIRType::Struct && type.array.empty()) |
| 3449 |
{ |
| 3450 |
// The variable is used in multiple continue blocks, this is not a loop |
| 3451 |
// candidate, signal that by setting block to -1u. |
| 3452 |
auto &potential = potential_loop_variables[var.first]; |
| 3453 |
|
| 3454 |
if (potential == 0) |
| 3455 |
potential = block; |
| 3456 |
else |
| 3457 |
potential = ~(0u); |
| 3458 |
} |
| 3459 |
} |
| 3460 |
builder.add_block(block); |
| 3461 |
} |
| 3462 |
|
| 3463 |
builder.lift_continue_block_dominator(); |
| 3464 |
|
| 3465 |
// Add it to a per-block list of variables. |
| 3466 |
BlockID dominating_block = builder.get_dominator(); |
| 3467 |
|
| 3468 |
// For variables whose dominating block is inside a loop, there is a risk that these variables |
| 3469 |
// actually need to be preserved across loop iterations. We can express this by adding |
| 3470 |
// a "read" access to the loop header. |
| 3471 |
// In the dominating block, we must see an OpStore or equivalent as the first access of an OpVariable. |
| 3472 |
// Should that fail, we look for the outermost loop header and tack on an access there. |
| 3473 |
// Phi nodes cannot have this problem. |
| 3474 |
if (dominating_block) |
| 3475 |
{ |
| 3476 |
auto &variable = get<SPIRVariable>(var.first); |
| 3477 |
if (!variable.phi_variable) |
| 3478 |
{ |
| 3479 |
auto *block = &get<SPIRBlock>(dominating_block); |
| 3480 |
bool preserve = may_read_undefined_variable_in_block(*block, var.first); |
| 3481 |
if (preserve) |
| 3482 |
{ |
| 3483 |
// Find the outermost loop scope. |
| 3484 |
while (block->loop_dominator != BlockID(SPIRBlock::NoDominator)) |
| 3485 |
block = &get<SPIRBlock>(block->loop_dominator); |
| 3486 |
|
| 3487 |
if (block->self != dominating_block) |
| 3488 |
{ |
| 3489 |
builder.add_block(block->self); |
| 3490 |
dominating_block = builder.get_dominator(); |
| 3491 |
} |
| 3492 |
} |
| 3493 |
} |
| 3494 |
} |
| 3495 |
|
| 3496 |
// If all blocks here are dead code, this will be 0, so the variable in question |
| 3497 |
// will be completely eliminated. |
| 3498 |
if (dominating_block) |
| 3499 |
{ |
| 3500 |
auto &block = get<SPIRBlock>(dominating_block); |
| 3501 |
block.dominated_variables.push_back(var.first); |
| 3502 |
get<SPIRVariable>(var.first).dominator = dominating_block; |
| 3503 |
} |
| 3504 |
} |
| 3505 |
|
| 3506 |
for (auto &var : handler.accessed_temporaries_to_block) |
| 3507 |
{ |
| 3508 |
auto itr = handler.result_id_to_type.find(var.first); |
| 3509 |
|
| 3510 |
if (itr == end(handler.result_id_to_type)) |
| 3511 |
{ |
| 3512 |
// We found a false positive ID being used, ignore. |
| 3513 |
// This should probably be an assert. |
| 3514 |
continue; |
| 3515 |
} |
| 3516 |
|
| 3517 |
// There is no point in doing domination analysis for opaque types. |
| 3518 |
auto &type = get<SPIRType>(itr->second); |
| 3519 |
if (type_is_opaque_value(type)) |
| 3520 |
continue; |
| 3521 |
|
| 3522 |
DominatorBuilder builder(cfg); |
| 3523 |
bool force_temporary = false; |
| 3524 |
bool used_in_header_hoisted_continue_block = false; |
| 3525 |
|
| 3526 |
// Figure out which block is dominating all accesses of those temporaries. |
| 3527 |
auto &blocks = var.second; |
| 3528 |
for (auto &block : blocks) |
| 3529 |
{ |
| 3530 |
builder.add_block(block); |
| 3531 |
|
| 3532 |
if (blocks.size() != 1 && is_continue(block)) |
| 3533 |
{ |
| 3534 |
// The risk here is that inner loop can dominate the continue block. |
| 3535 |
// Any temporary we access in the continue block must be declared before the loop. |
| 3536 |
// This is moot for complex loops however. |
| 3537 |
auto &loop_header_block = get<SPIRBlock>(ir.continue_block_to_loop_header[block]); |
| 3538 |
assert(loop_header_block.merge == SPIRBlock::MergeLoop); |
| 3539 |
builder.add_block(loop_header_block.self); |
| 3540 |
used_in_header_hoisted_continue_block = true; |
| 3541 |
} |
| 3542 |
} |
| 3543 |
|
| 3544 |
uint32_t dominating_block = builder.get_dominator(); |
| 3545 |
|
| 3546 |
if (blocks.size() != 1 && is_single_block_loop(dominating_block)) |
| 3547 |
{ |
| 3548 |
// Awkward case, because the loop header is also the continue block, |
| 3549 |
// so hoisting to loop header does not help. |
| 3550 |
force_temporary = true; |
| 3551 |
} |
| 3552 |
|
| 3553 |
if (dominating_block) |
| 3554 |
{ |
| 3555 |
// If we touch a variable in the dominating block, this is the expected setup. |
| 3556 |
// SPIR-V normally mandates this, but we have extra cases for temporary use inside loops. |
| 3557 |
bool first_use_is_dominator = blocks.count(dominating_block) != 0; |
| 3558 |
|
| 3559 |
if (!first_use_is_dominator || force_temporary) |
| 3560 |
{ |
| 3561 |
if (handler.access_chain_expressions.count(var.first)) |
| 3562 |
{ |
| 3563 |
// Exceptionally rare case. |
| 3564 |
// We cannot declare temporaries of access chains (except on MSL perhaps with pointers). |
| 3565 |
// Rather than do that, we force the indexing expressions to be declared in the right scope by |
| 3566 |
// tracking their usage to that end. There is no temporary to hoist. |
| 3567 |
// However, we still need to observe declaration order of the access chain. |
| 3568 |
|
| 3569 |
if (used_in_header_hoisted_continue_block) |
| 3570 |
{ |
| 3571 |
// For this scenario, we used an access chain inside a continue block where we also registered an access to header block. |
| 3572 |
// This is a problem as we need to declare an access chain properly first with full definition. |
| 3573 |
// We cannot use temporaries for these expressions, |
| 3574 |
// so we must make sure the access chain is declared ahead of time. |
| 3575 |
// Force a complex for loop to deal with this. |
| 3576 |
// TODO: Out-of-order declaring for loops where continue blocks are emitted last might be another option. |
| 3577 |
auto &loop_header_block = get<SPIRBlock>(dominating_block); |
| 3578 |
assert(loop_header_block.merge == SPIRBlock::MergeLoop); |
| 3579 |
loop_header_block.complex_continue = true; |
| 3580 |
} |
| 3581 |
} |
| 3582 |
else |
| 3583 |
{ |
| 3584 |
// This should be very rare, but if we try to declare a temporary inside a loop, |
| 3585 |
// and that temporary is used outside the loop as well (spirv-opt inliner likes this) |
| 3586 |
// we should actually emit the temporary outside the loop. |
| 3587 |
hoisted_temporaries.insert(var.first); |
| 3588 |
forced_temporaries.insert(var.first); |
| 3589 |
|
| 3590 |
auto &block_temporaries = get<SPIRBlock>(dominating_block).declare_temporary; |
| 3591 |
block_temporaries.emplace_back(handler.result_id_to_type[var.first], var.first); |
| 3592 |
} |
| 3593 |
} |
| 3594 |
else if (blocks.size() > 1) |
| 3595 |
{ |
| 3596 |
// Keep track of the temporary as we might have to declare this temporary. |
| 3597 |
// This can happen if the loop header dominates a temporary, but we have a complex fallback loop. |
| 3598 |
// In this case, the header is actually inside the for (;;) {} block, and we have problems. |
| 3599 |
// What we need to do is hoist the temporaries outside the for (;;) {} block in case the header block |
| 3600 |
// declares the temporary. |
| 3601 |
auto &block_temporaries = get<SPIRBlock>(dominating_block).potential_declare_temporary; |
| 3602 |
block_temporaries.emplace_back(handler.result_id_to_type[var.first], var.first); |
| 3603 |
} |
| 3604 |
} |
| 3605 |
} |
| 3606 |
|
| 3607 |
unordered_set<uint32_t> seen_blocks; |
| 3608 |
|
| 3609 |
// Now, try to analyze whether or not these variables are actually loop variables. |
| 3610 |
for (auto &loop_variable : potential_loop_variables) |
| 3611 |
{ |
| 3612 |
auto &var = get<SPIRVariable>(loop_variable.first); |
| 3613 |
auto dominator = var.dominator; |
| 3614 |
BlockID block = loop_variable.second; |
| 3615 |
|
| 3616 |
// The variable was accessed in multiple continue blocks, ignore. |
| 3617 |
if (block == BlockID(~(0u)) || block == BlockID(0)) |
| 3618 |
continue; |
| 3619 |
|
| 3620 |
// Dead code. |
| 3621 |
if (dominator == ID(0)) |
| 3622 |
continue; |
| 3623 |
|
| 3624 |
BlockID header = 0; |
| 3625 |
|
| 3626 |
// Find the loop header for this block if we are a continue block. |
| 3627 |
{ |
| 3628 |
auto itr = ir.continue_block_to_loop_header.find(block); |
| 3629 |
if (itr != end(ir.continue_block_to_loop_header)) |
| 3630 |
{ |
| 3631 |
header = itr->second; |
| 3632 |
} |
| 3633 |
else if (get<SPIRBlock>(block).continue_block == block) |
| 3634 |
{ |
| 3635 |
// Also check for self-referential continue block. |
| 3636 |
header = block; |
| 3637 |
} |
| 3638 |
} |
| 3639 |
|
| 3640 |
assert(header); |
| 3641 |
auto &header_block = get<SPIRBlock>(header); |
| 3642 |
auto &blocks = handler.accessed_variables_to_block[loop_variable.first]; |
| 3643 |
|
| 3644 |
// If a loop variable is not used before the loop, it's probably not a loop variable. |
| 3645 |
bool has_accessed_variable = blocks.count(header) != 0; |
| 3646 |
|
| 3647 |
// Now, there are two conditions we need to meet for the variable to be a loop variable. |
| 3648 |
// 1. The dominating block must have a branch-free path to the loop header, |
| 3649 |
// this way we statically know which expression should be part of the loop variable initializer. |
| 3650 |
|
| 3651 |
// Walk from the dominator, if there is one straight edge connecting |
| 3652 |
// dominator and loop header, we statically know the loop initializer. |
| 3653 |
bool static_loop_init = true; |
| 3654 |
while (dominator != header) |
| 3655 |
{ |
| 3656 |
if (blocks.count(dominator) != 0) |
| 3657 |
has_accessed_variable = true; |
| 3658 |
|
| 3659 |
auto &succ = cfg.get_succeeding_edges(dominator); |
| 3660 |
if (succ.size() != 1) |
| 3661 |
{ |
| 3662 |
static_loop_init = false; |
| 3663 |
break; |
| 3664 |
} |
| 3665 |
|
| 3666 |
auto &pred = cfg.get_preceding_edges(succ.front()); |
| 3667 |
if (pred.size() != 1 || pred.front() != dominator) |
| 3668 |
{ |
| 3669 |
static_loop_init = false; |
| 3670 |
break; |
| 3671 |
} |
| 3672 |
|
| 3673 |
dominator = succ.front(); |
| 3674 |
} |
| 3675 |
|
| 3676 |
if (!static_loop_init || !has_accessed_variable) |
| 3677 |
continue; |
| 3678 |
|
| 3679 |
// The second condition we need to meet is that no access after the loop |
| 3680 |
// merge can occur. Walk the CFG to see if we find anything. |
| 3681 |
|
| 3682 |
seen_blocks.clear(); |
| 3683 |
cfg.walk_from(seen_blocks, header_block.merge_block, [&](uint32_t walk_block) -> bool { |
| 3684 |
// We found a block which accesses the variable outside the loop. |
| 3685 |
if (blocks.find(walk_block) != end(blocks)) |
| 3686 |
static_loop_init = false; |
| 3687 |
return true; |
| 3688 |
}); |
| 3689 |
|
| 3690 |
if (!static_loop_init) |
| 3691 |
continue; |
| 3692 |
|
| 3693 |
// We have a loop variable. |
| 3694 |
header_block.loop_variables.push_back(loop_variable.first); |
| 3695 |
// Need to sort here as variables come from an unordered container, and pushing stuff in wrong order |
| 3696 |
// will break reproducability in regression runs. |
| 3697 |
sort(begin(header_block.loop_variables), end(header_block.loop_variables)); |
| 3698 |
get<SPIRVariable>(loop_variable.first).loop_variable = true; |
| 3699 |
} |
| 3700 |
} |
| 3701 |
|
| 3702 |
bool Compiler::may_read_undefined_variable_in_block(const SPIRBlock &block, uint32_t var) |
| 3703 |
{ |
| 3704 |
for (auto &op : block.ops) |
| 3705 |
{ |
| 3706 |
auto *ops = stream(op); |
| 3707 |
switch (op.op) |
| 3708 |
{ |
| 3709 |
case OpStore: |
| 3710 |
case OpCopyMemory: |
| 3711 |
if (ops[0] == var) |
| 3712 |
return false; |
| 3713 |
break; |
| 3714 |
|
| 3715 |
case OpAccessChain: |
| 3716 |
case OpInBoundsAccessChain: |
| 3717 |
case OpPtrAccessChain: |
| 3718 |
// Access chains are generally used to partially read and write. It's too hard to analyze |
| 3719 |
// if all constituents are written fully before continuing, so just assume it's preserved. |
| 3720 |
// This is the same as the parameter preservation analysis. |
| 3721 |
if (ops[2] == var) |
| 3722 |
return true; |
| 3723 |
break; |
| 3724 |
|
| 3725 |
case OpSelect: |
| 3726 |
// Variable pointers. |
| 3727 |
// We might read before writing. |
| 3728 |
if (ops[3] == var || ops[4] == var) |
| 3729 |
return true; |
| 3730 |
break; |
| 3731 |
|
| 3732 |
case OpPhi: |
| 3733 |
{ |
| 3734 |
// Variable pointers. |
| 3735 |
// We might read before writing. |
| 3736 |
if (op.length < 2) |
| 3737 |
break; |
| 3738 |
|
| 3739 |
uint32_t count = op.length - 2; |
| 3740 |
for (uint32_t i = 0; i < count; i += 2) |
| 3741 |
if (ops[i + 2] == var) |
| 3742 |
return true; |
| 3743 |
break; |
| 3744 |
} |
| 3745 |
|
| 3746 |
case OpCopyObject: |
| 3747 |
case OpLoad: |
| 3748 |
if (ops[2] == var) |
| 3749 |
return true; |
| 3750 |
break; |
| 3751 |
|
| 3752 |
case OpFunctionCall: |
| 3753 |
{ |
| 3754 |
if (op.length < 3) |
| 3755 |
break; |
| 3756 |
|
| 3757 |
// May read before writing. |
| 3758 |
uint32_t count = op.length - 3; |
| 3759 |
for (uint32_t i = 0; i < count; i++) |
| 3760 |
if (ops[i + 3] == var) |
| 3761 |
return true; |
| 3762 |
break; |
| 3763 |
} |
| 3764 |
|
| 3765 |
default: |
| 3766 |
break; |
| 3767 |
} |
| 3768 |
} |
| 3769 |
|
| 3770 |
// Not accessed somehow, at least not in a usual fashion. |
| 3771 |
// It's likely accessed in a branch, so assume we must preserve. |
| 3772 |
return true; |
| 3773 |
} |
| 3774 |
|
| 3775 |
Bitset Compiler::get_buffer_block_flags(VariableID id) const |
| 3776 |
{ |
| 3777 |
return ir.get_buffer_block_flags(get<SPIRVariable>(id)); |
| 3778 |
} |
| 3779 |
|
| 3780 |
bool Compiler::get_common_basic_type(const SPIRType &type, SPIRType::BaseType &base_type) |
| 3781 |
{ |
| 3782 |
if (type.basetype == SPIRType::Struct) |
| 3783 |
{ |
| 3784 |
base_type = SPIRType::Unknown; |
| 3785 |
for (auto &member_type : type.member_types) |
| 3786 |
{ |
| 3787 |
SPIRType::BaseType member_base; |
| 3788 |
if (!get_common_basic_type(get<SPIRType>(member_type), member_base)) |
| 3789 |
return false; |
| 3790 |
|
| 3791 |
if (base_type == SPIRType::Unknown) |
| 3792 |
base_type = member_base; |
| 3793 |
else if (base_type != member_base) |
| 3794 |
return false; |
| 3795 |
} |
| 3796 |
return true; |
| 3797 |
} |
| 3798 |
else |
| 3799 |
{ |
| 3800 |
base_type = type.basetype; |
| 3801 |
return true; |
| 3802 |
} |
| 3803 |
} |
| 3804 |
|
| 3805 |
void Compiler::ActiveBuiltinHandler::handle_builtin(const SPIRType &type, BuiltIn builtin, |
| 3806 |
const Bitset &decoration_flags) |
| 3807 |
{ |
| 3808 |
// If used, we will need to explicitly declare a new array size for these builtins. |
| 3809 |
|
| 3810 |
if (builtin == BuiltInClipDistance) |
| 3811 |
{ |
| 3812 |
if (!type.array_size_literal[0]) |
| 3813 |
SPIRV_CROSS_THROW("Array size for ClipDistance must be a literal."); |
| 3814 |
uint32_t array_size = type.array[0]; |
| 3815 |
if (array_size == 0) |
| 3816 |
SPIRV_CROSS_THROW("Array size for ClipDistance must not be unsized."); |
| 3817 |
compiler.clip_distance_count = array_size; |
| 3818 |
} |
| 3819 |
else if (builtin == BuiltInCullDistance) |
| 3820 |
{ |
| 3821 |
if (!type.array_size_literal[0]) |
| 3822 |
SPIRV_CROSS_THROW("Array size for CullDistance must be a literal."); |
| 3823 |
uint32_t array_size = type.array[0]; |
| 3824 |
if (array_size == 0) |
| 3825 |
SPIRV_CROSS_THROW("Array size for CullDistance must not be unsized."); |
| 3826 |
compiler.cull_distance_count = array_size; |
| 3827 |
} |
| 3828 |
else if (builtin == BuiltInPosition) |
| 3829 |
{ |
| 3830 |
if (decoration_flags.get(DecorationInvariant)) |
| 3831 |
compiler.position_invariant = true; |
| 3832 |
} |
| 3833 |
} |
| 3834 |
|
| 3835 |
bool Compiler::ActiveBuiltinHandler::handle(spv::Op opcode, const uint32_t *args, uint32_t length) |
| 3836 |
{ |
| 3837 |
const auto add_if_builtin = [&](uint32_t id) { |
| 3838 |
// Only handles variables here. |
| 3839 |
// Builtins which are part of a block are handled in AccessChain. |
| 3840 |
auto *var = compiler.maybe_get<SPIRVariable>(id); |
| 3841 |
auto &decorations = compiler.ir.meta[id].decoration; |
| 3842 |
if (var && decorations.builtin) |
| 3843 |
{ |
| 3844 |
auto &type = compiler.get<SPIRType>(var->basetype); |
| 3845 |
auto &flags = |
| 3846 |
type.storage == StorageClassInput ? compiler.active_input_builtins : compiler.active_output_builtins; |
| 3847 |
flags.set(decorations.builtin_type); |
| 3848 |
handle_builtin(type, decorations.builtin_type, decorations.decoration_flags); |
| 3849 |
} |
| 3850 |
}; |
| 3851 |
|
| 3852 |
switch (opcode) |
| 3853 |
{ |
| 3854 |
case OpStore: |
| 3855 |
if (length < 1) |
| 3856 |
return false; |
| 3857 |
|
| 3858 |
add_if_builtin(args[0]); |
| 3859 |
break; |
| 3860 |
|
| 3861 |
case OpCopyMemory: |
| 3862 |
if (length < 2) |
| 3863 |
return false; |
| 3864 |
|
| 3865 |
add_if_builtin(args[0]); |
| 3866 |
add_if_builtin(args[1]); |
| 3867 |
break; |
| 3868 |
|
| 3869 |
case OpCopyObject: |
| 3870 |
case OpLoad: |
| 3871 |
if (length < 3) |
| 3872 |
return false; |
| 3873 |
|
| 3874 |
add_if_builtin(args[2]); |
| 3875 |
break; |
| 3876 |
|
| 3877 |
case OpSelect: |
| 3878 |
if (length < 5) |
| 3879 |
return false; |
| 3880 |
|
| 3881 |
add_if_builtin(args[3]); |
| 3882 |
add_if_builtin(args[4]); |
| 3883 |
break; |
| 3884 |
|
| 3885 |
case OpPhi: |
| 3886 |
{ |
| 3887 |
if (length < 2) |
| 3888 |
return false; |
| 3889 |
|
| 3890 |
uint32_t count = length - 2; |
| 3891 |
args += 2; |
| 3892 |
for (uint32_t i = 0; i < count; i += 2) |
| 3893 |
add_if_builtin(args[i]); |
| 3894 |
break; |
| 3895 |
} |
| 3896 |
|
| 3897 |
case OpFunctionCall: |
| 3898 |
{ |
| 3899 |
if (length < 3) |
| 3900 |
return false; |
| 3901 |
|
| 3902 |
uint32_t count = length - 3; |
| 3903 |
args += 3; |
| 3904 |
for (uint32_t i = 0; i < count; i++) |
| 3905 |
add_if_builtin(args[i]); |
| 3906 |
break; |
| 3907 |
} |
| 3908 |
|
| 3909 |
case OpAccessChain: |
| 3910 |
case OpInBoundsAccessChain: |
| 3911 |
case OpPtrAccessChain: |
| 3912 |
{ |
| 3913 |
if (length < 4) |
| 3914 |
return false; |
| 3915 |
|
| 3916 |
// Only consider global variables, cannot consider variables in functions yet, or other |
| 3917 |
// access chains as they have not been created yet. |
| 3918 |
auto *var = compiler.maybe_get<SPIRVariable>(args[2]); |
| 3919 |
if (!var) |
| 3920 |
break; |
| 3921 |
|
| 3922 |
// Required if we access chain into builtins like gl_GlobalInvocationID. |
| 3923 |
add_if_builtin(args[2]); |
| 3924 |
|
| 3925 |
// Start traversing type hierarchy at the proper non-pointer types. |
| 3926 |
auto *type = &compiler.get_variable_data_type(*var); |
| 3927 |
|
| 3928 |
auto &flags = |
| 3929 |
var->storage == StorageClassInput ? compiler.active_input_builtins : compiler.active_output_builtins; |
| 3930 |
|
| 3931 |
uint32_t count = length - 3; |
| 3932 |
args += 3; |
| 3933 |
for (uint32_t i = 0; i < count; i++) |
| 3934 |
{ |
| 3935 |
// Pointers |
| 3936 |
if (opcode == OpPtrAccessChain && i == 0) |
| 3937 |
{ |
| 3938 |
type = &compiler.get<SPIRType>(type->parent_type); |
| 3939 |
continue; |
| 3940 |
} |
| 3941 |
|
| 3942 |
// Arrays |
| 3943 |
if (!type->array.empty()) |
| 3944 |
{ |
| 3945 |
type = &compiler.get<SPIRType>(type->parent_type); |
| 3946 |
} |
| 3947 |
// Structs |
| 3948 |
else if (type->basetype == SPIRType::Struct) |
| 3949 |
{ |
| 3950 |
uint32_t index = compiler.get<SPIRConstant>(args[i]).scalar(); |
| 3951 |
|
| 3952 |
if (index < uint32_t(compiler.ir.meta[type->self].members.size())) |
| 3953 |
{ |
| 3954 |
auto &decorations = compiler.ir.meta[type->self].members[index]; |
| 3955 |
if (decorations.builtin) |
| 3956 |
{ |
| 3957 |
flags.set(decorations.builtin_type); |
| 3958 |
handle_builtin(compiler.get<SPIRType>(type->member_types[index]), decorations.builtin_type, |
| 3959 |
decorations.decoration_flags); |
| 3960 |
} |
| 3961 |
} |
| 3962 |
|
| 3963 |
type = &compiler.get<SPIRType>(type->member_types[index]); |
| 3964 |
} |
| 3965 |
else |
| 3966 |
{ |
| 3967 |
// No point in traversing further. We won't find any extra builtins. |
| 3968 |
break; |
| 3969 |
} |
| 3970 |
} |
| 3971 |
break; |
| 3972 |
} |
| 3973 |
|
| 3974 |
default: |
| 3975 |
break; |
| 3976 |
} |
| 3977 |
|
| 3978 |
return true; |
| 3979 |
} |
| 3980 |
|
| 3981 |
void Compiler::update_active_builtins() |
| 3982 |
{ |
| 3983 |
active_input_builtins.reset(); |
| 3984 |
active_output_builtins.reset(); |
| 3985 |
cull_distance_count = 0; |
| 3986 |
clip_distance_count = 0; |
| 3987 |
ActiveBuiltinHandler handler(*this); |
| 3988 |
traverse_all_reachable_opcodes(get<SPIRFunction>(ir.default_entry_point), handler); |
| 3989 |
} |
| 3990 |
|
| 3991 |
// Returns whether this shader uses a builtin of the storage class |
| 3992 |
bool Compiler::has_active_builtin(BuiltIn builtin, StorageClass storage) |
| 3993 |
{ |
| 3994 |
const Bitset *flags; |
| 3995 |
switch (storage) |
| 3996 |
{ |
| 3997 |
case StorageClassInput: |
| 3998 |
flags = &active_input_builtins; |
| 3999 |
break; |
| 4000 |
case StorageClassOutput: |
| 4001 |
flags = &active_output_builtins; |
| 4002 |
break; |
| 4003 |
|
| 4004 |
default: |
| 4005 |
return false; |
| 4006 |
} |
| 4007 |
return flags->get(builtin); |
| 4008 |
} |
| 4009 |
|
| 4010 |
void Compiler::analyze_image_and_sampler_usage() |
| 4011 |
{ |
| 4012 |
CombinedImageSamplerDrefHandler dref_handler(*this); |
| 4013 |
traverse_all_reachable_opcodes(get<SPIRFunction>(ir.default_entry_point), dref_handler); |
| 4014 |
|
| 4015 |
CombinedImageSamplerUsageHandler handler(*this, dref_handler.dref_combined_samplers); |
| 4016 |
traverse_all_reachable_opcodes(get<SPIRFunction>(ir.default_entry_point), handler); |
| 4017 |
|
| 4018 |
// Need to run this traversal twice. First time, we propagate any comparison sampler usage from leaf functions |
| 4019 |
// down to main(). |
| 4020 |
// In the second pass, we can propagate up forced depth state coming from main() up into leaf functions. |
| 4021 |
handler.dependency_hierarchy.clear(); |
| 4022 |
traverse_all_reachable_opcodes(get<SPIRFunction>(ir.default_entry_point), handler); |
| 4023 |
|
| 4024 |
comparison_ids = move(handler.comparison_ids); |
| 4025 |
need_subpass_input = handler.need_subpass_input; |
| 4026 |
|
| 4027 |
// Forward information from separate images and samplers into combined image samplers. |
| 4028 |
for (auto &combined : combined_image_samplers) |
| 4029 |
if (comparison_ids.count(combined.sampler_id)) |
| 4030 |
comparison_ids.insert(combined.combined_id); |
| 4031 |
} |
| 4032 |
|
| 4033 |
bool Compiler::CombinedImageSamplerDrefHandler::handle(spv::Op opcode, const uint32_t *args, uint32_t) |
| 4034 |
{ |
| 4035 |
// Mark all sampled images which are used with Dref. |
| 4036 |
switch (opcode) |
| 4037 |
{ |
| 4038 |
case OpImageSampleDrefExplicitLod: |
| 4039 |
case OpImageSampleDrefImplicitLod: |
| 4040 |
case OpImageSampleProjDrefExplicitLod: |
| 4041 |
case OpImageSampleProjDrefImplicitLod: |
| 4042 |
case OpImageSparseSampleProjDrefImplicitLod: |
| 4043 |
case OpImageSparseSampleDrefImplicitLod: |
| 4044 |
case OpImageSparseSampleProjDrefExplicitLod: |
| 4045 |
case OpImageSparseSampleDrefExplicitLod: |
| 4046 |
case OpImageDrefGather: |
| 4047 |
case OpImageSparseDrefGather: |
| 4048 |
dref_combined_samplers.insert(args[2]); |
| 4049 |
return true; |
| 4050 |
|
| 4051 |
default: |
| 4052 |
break; |
| 4053 |
} |
| 4054 |
|
| 4055 |
return true; |
| 4056 |
} |
| 4057 |
|
| 4058 |
const CFG &Compiler::get_cfg_for_current_function() const |
| 4059 |
{ |
| 4060 |
assert(current_function); |
| 4061 |
return get_cfg_for_function(current_function->self); |
| 4062 |
} |
| 4063 |
|
| 4064 |
const CFG &Compiler::get_cfg_for_function(uint32_t id) const |
| 4065 |
{ |
| 4066 |
auto cfg_itr = function_cfgs.find(id); |
| 4067 |
assert(cfg_itr != end(function_cfgs)); |
| 4068 |
assert(cfg_itr->second); |
| 4069 |
return *cfg_itr->second; |
| 4070 |
} |
| 4071 |
|
| 4072 |
void Compiler::build_function_control_flow_graphs_and_analyze() |
| 4073 |
{ |
| 4074 |
CFGBuilder handler(*this); |
| 4075 |
handler.function_cfgs[ir.default_entry_point].reset(new CFG(*this, get<SPIRFunction>(ir.default_entry_point))); |
| 4076 |
traverse_all_reachable_opcodes(get<SPIRFunction>(ir.default_entry_point), handler); |
| 4077 |
function_cfgs = move(handler.function_cfgs); |
| 4078 |
bool single_function = function_cfgs.size() <= 1; |
| 4079 |
|
| 4080 |
for (auto &f : function_cfgs) |
| 4081 |
{ |
| 4082 |
auto &func = get<SPIRFunction>(f.first); |
| 4083 |
AnalyzeVariableScopeAccessHandler scope_handler(*this, func); |
| 4084 |
analyze_variable_scope(func, scope_handler); |
| 4085 |
find_function_local_luts(func, scope_handler, single_function); |
| 4086 |
|
| 4087 |
// Check if we can actually use the loop variables we found in analyze_variable_scope. |
| 4088 |
// To use multiple initializers, we need the same type and qualifiers. |
| 4089 |
for (auto block : func.blocks) |
| 4090 |
{ |
| 4091 |
auto &b = get<SPIRBlock>(block); |
| 4092 |
if (b.loop_variables.size() < 2) |
| 4093 |
continue; |
| 4094 |
|
| 4095 |
auto &flags = get_decoration_bitset(b.loop_variables.front()); |
| 4096 |
uint32_t type = get<SPIRVariable>(b.loop_variables.front()).basetype; |
| 4097 |
bool invalid_initializers = false; |
| 4098 |
for (auto loop_variable : b.loop_variables) |
| 4099 |
{ |
| 4100 |
if (flags != get_decoration_bitset(loop_variable) || |
| 4101 |
type != get<SPIRVariable>(b.loop_variables.front()).basetype) |
| 4102 |
{ |
| 4103 |
invalid_initializers = true; |
| 4104 |
break; |
| 4105 |
} |
| 4106 |
} |
| 4107 |
|
| 4108 |
if (invalid_initializers) |
| 4109 |
{ |
| 4110 |
for (auto loop_variable : b.loop_variables) |
| 4111 |
get<SPIRVariable>(loop_variable).loop_variable = false; |
| 4112 |
b.loop_variables.clear(); |
| 4113 |
} |
| 4114 |
} |
| 4115 |
} |
| 4116 |
} |
| 4117 |
|
| 4118 |
Compiler::CFGBuilder::CFGBuilder(Compiler &compiler_) |
| 4119 |
: compiler(compiler_) |
| 4120 |
{ |
| 4121 |
} |
| 4122 |
|
| 4123 |
bool Compiler::CFGBuilder::handle(spv::Op, const uint32_t *, uint32_t) |
| 4124 |
{ |
| 4125 |
return true; |
| 4126 |
} |
| 4127 |
|
| 4128 |
bool Compiler::CFGBuilder::follow_function_call(const SPIRFunction &func) |
| 4129 |
{ |
| 4130 |
if (function_cfgs.find(func.self) == end(function_cfgs)) |
| 4131 |
{ |
| 4132 |
function_cfgs[func.self].reset(new CFG(compiler, func)); |
| 4133 |
return true; |
| 4134 |
} |
| 4135 |
else |
| 4136 |
return false; |
| 4137 |
} |
| 4138 |
|
| 4139 |
void Compiler::CombinedImageSamplerUsageHandler::add_dependency(uint32_t dst, uint32_t src) |
| 4140 |
{ |
| 4141 |
dependency_hierarchy[dst].insert(src); |
| 4142 |
// Propagate up any comparison state if we're loading from one such variable. |
| 4143 |
if (comparison_ids.count(src)) |
| 4144 |
comparison_ids.insert(dst); |
| 4145 |
} |
| 4146 |
|
| 4147 |
bool Compiler::CombinedImageSamplerUsageHandler::begin_function_scope(const uint32_t *args, uint32_t length) |
| 4148 |
{ |
| 4149 |
if (length < 3) |
| 4150 |
return false; |
| 4151 |
|
| 4152 |
auto &func = compiler.get<SPIRFunction>(args[2]); |
| 4153 |
const auto *arg = &args[3]; |
| 4154 |
length -= 3; |
| 4155 |
|
| 4156 |
for (uint32_t i = 0; i < length; i++) |
| 4157 |
{ |
| 4158 |
auto &argument = func.arguments[i]; |
| 4159 |
add_dependency(argument.id, arg[i]); |
| 4160 |
} |
| 4161 |
|
| 4162 |
return true; |
| 4163 |
} |
| 4164 |
|
| 4165 |
void Compiler::CombinedImageSamplerUsageHandler::add_hierarchy_to_comparison_ids(uint32_t id) |
| 4166 |
{ |
| 4167 |
// Traverse the variable dependency hierarchy and tag everything in its path with comparison ids. |
| 4168 |
comparison_ids.insert(id); |
| 4169 |
|
| 4170 |
for (auto &dep_id : dependency_hierarchy[id]) |
| 4171 |
add_hierarchy_to_comparison_ids(dep_id); |
| 4172 |
} |
| 4173 |
|
| 4174 |
bool Compiler::CombinedImageSamplerUsageHandler::handle(Op opcode, const uint32_t *args, uint32_t length) |
| 4175 |
{ |
| 4176 |
switch (opcode) |
| 4177 |
{ |
| 4178 |
case OpAccessChain: |
| 4179 |
case OpInBoundsAccessChain: |
| 4180 |
case OpPtrAccessChain: |
| 4181 |
case OpLoad: |
| 4182 |
{ |
| 4183 |
if (length < 3) |
| 4184 |
return false; |
| 4185 |
|
| 4186 |
add_dependency(args[1], args[2]); |
| 4187 |
|
| 4188 |
// Ideally defer this to OpImageRead, but then we'd need to track loaded IDs. |
| 4189 |
// If we load an image, we're going to use it and there is little harm in declaring an unused gl_FragCoord. |
| 4190 |
auto &type = compiler.get<SPIRType>(args[0]); |
| 4191 |
if (type.image.dim == DimSubpassData) |
| 4192 |
need_subpass_input = true; |
| 4193 |
|
| 4194 |
// If we load a SampledImage and it will be used with Dref, propagate the state up. |
| 4195 |
if (dref_combined_samplers.count(args[1]) != 0) |
| 4196 |
add_hierarchy_to_comparison_ids(args[1]); |
| 4197 |
break; |
| 4198 |
} |
| 4199 |
|
| 4200 |
case OpSampledImage: |
| 4201 |
{ |
| 4202 |
if (length < 4) |
| 4203 |
return false; |
| 4204 |
|
| 4205 |
uint32_t result_type = args[0]; |
| 4206 |
uint32_t result_id = args[1]; |
| 4207 |
auto &type = compiler.get<SPIRType>(result_type); |
| 4208 |
|
| 4209 |
// If the underlying resource has been used for comparison then duplicate loads of that resource must be too. |
| 4210 |
// This image must be a depth image. |
| 4211 |
uint32_t image = args[2]; |
| 4212 |
uint32_t sampler = args[3]; |
| 4213 |
|
| 4214 |
if (type.image.depth || dref_combined_samplers.count(result_id) != 0) |
| 4215 |
{ |
| 4216 |
add_hierarchy_to_comparison_ids(image); |
| 4217 |
|
| 4218 |
// This sampler must be a SamplerComparisonState, and not a regular SamplerState. |
| 4219 |
add_hierarchy_to_comparison_ids(sampler); |
| 4220 |
|
| 4221 |
// Mark the OpSampledImage itself as being comparison state. |
| 4222 |
comparison_ids.insert(result_id); |
| 4223 |
} |
| 4224 |
return true; |
| 4225 |
} |
| 4226 |
|
| 4227 |
default: |
| 4228 |
break; |
| 4229 |
} |
| 4230 |
|
| 4231 |
return true; |
| 4232 |
} |
| 4233 |
|
| 4234 |
bool Compiler::buffer_is_hlsl_counter_buffer(VariableID id) const |
| 4235 |
{ |
| 4236 |
auto *m = ir.find_meta(id); |
| 4237 |
return m && m->hlsl_is_magic_counter_buffer; |
| 4238 |
} |
| 4239 |
|
| 4240 |
bool Compiler::buffer_get_hlsl_counter_buffer(VariableID id, uint32_t &counter_id) const |
| 4241 |
{ |
| 4242 |
auto *m = ir.find_meta(id); |
| 4243 |
|
| 4244 |
// First, check for the proper decoration. |
| 4245 |
if (m && m->hlsl_magic_counter_buffer != 0) |
| 4246 |
{ |
| 4247 |
counter_id = m->hlsl_magic_counter_buffer; |
| 4248 |
return true; |
| 4249 |
} |
| 4250 |
else |
| 4251 |
return false; |
| 4252 |
} |
| 4253 |
|
| 4254 |
void Compiler::make_constant_null(uint32_t id, uint32_t type) |
| 4255 |
{ |
| 4256 |
auto &constant_type = get<SPIRType>(type); |
| 4257 |
|
| 4258 |
if (constant_type.pointer) |
| 4259 |
{ |
| 4260 |
auto &constant = set<SPIRConstant>(id, type); |
| 4261 |
constant.make_null(constant_type); |
| 4262 |
} |
| 4263 |
else if (!constant_type.array.empty()) |
| 4264 |
{ |
| 4265 |
assert(constant_type.parent_type); |
| 4266 |
uint32_t parent_id = ir.increase_bound_by(1); |
| 4267 |
make_constant_null(parent_id, constant_type.parent_type); |
| 4268 |
|
| 4269 |
if (!constant_type.array_size_literal.back()) |
| 4270 |
SPIRV_CROSS_THROW("Array size of OpConstantNull must be a literal."); |
| 4271 |
|
| 4272 |
SmallVector<uint32_t> elements(constant_type.array.back()); |
| 4273 |
for (uint32_t i = 0; i < constant_type.array.back(); i++) |
| 4274 |
elements[i] = parent_id; |
| 4275 |
set<SPIRConstant>(id, type, elements.data(), uint32_t(elements.size()), false); |
| 4276 |
} |
| 4277 |
else if (!constant_type.member_types.empty()) |
| 4278 |
{ |
| 4279 |
uint32_t member_ids = ir.increase_bound_by(uint32_t(constant_type.member_types.size())); |
| 4280 |
SmallVector<uint32_t> elements(constant_type.member_types.size()); |
| 4281 |
for (uint32_t i = 0; i < constant_type.member_types.size(); i++) |
| 4282 |
{ |
| 4283 |
make_constant_null(member_ids + i, constant_type.member_types[i]); |
| 4284 |
elements[i] = member_ids + i; |
| 4285 |
} |
| 4286 |
set<SPIRConstant>(id, type, elements.data(), uint32_t(elements.size()), false); |
| 4287 |
} |
| 4288 |
else |
| 4289 |
{ |
| 4290 |
auto &constant = set<SPIRConstant>(id, type); |
| 4291 |
constant.make_null(constant_type); |
| 4292 |
} |
| 4293 |
} |
| 4294 |
|
| 4295 |
const SmallVector<spv::Capability> &Compiler::get_declared_capabilities() const |
| 4296 |
{ |
| 4297 |
return ir.declared_capabilities; |
| 4298 |
} |
| 4299 |
|
| 4300 |
const SmallVector<std::string> &Compiler::get_declared_extensions() const |
| 4301 |
{ |
| 4302 |
return ir.declared_extensions; |
| 4303 |
} |
| 4304 |
|
| 4305 |
std::string Compiler::get_remapped_declared_block_name(VariableID id) const |
| 4306 |
{ |
| 4307 |
return get_remapped_declared_block_name(id, false); |
| 4308 |
} |
| 4309 |
|
| 4310 |
std::string Compiler::get_remapped_declared_block_name(uint32_t id, bool fallback_prefer_instance_name) const |
| 4311 |
{ |
| 4312 |
auto itr = declared_block_names.find(id); |
| 4313 |
if (itr != end(declared_block_names)) |
| 4314 |
{ |
| 4315 |
return itr->second; |
| 4316 |
} |
| 4317 |
else |
| 4318 |
{ |
| 4319 |
auto &var = get<SPIRVariable>(id); |
| 4320 |
|
| 4321 |
if (fallback_prefer_instance_name) |
| 4322 |
{ |
| 4323 |
return to_name(var.self); |
| 4324 |
} |
| 4325 |
else |
| 4326 |
{ |
| 4327 |
auto &type = get<SPIRType>(var.basetype); |
| 4328 |
auto *type_meta = ir.find_meta(type.self); |
| 4329 |
auto *block_name = type_meta ? &type_meta->decoration.alias : nullptr; |
| 4330 |
return (!block_name || block_name->empty()) ? get_block_fallback_name(id) : *block_name; |
| 4331 |
} |
| 4332 |
} |
| 4333 |
} |
| 4334 |
|
| 4335 |
bool Compiler::reflection_ssbo_instance_name_is_significant() const |
| 4336 |
{ |
| 4337 |
if (ir.source.known) |
| 4338 |
{ |
| 4339 |
// UAVs from HLSL source tend to be declared in a way where the type is reused |
| 4340 |
// but the instance name is significant, and that's the name we should report. |
| 4341 |
// For GLSL, SSBOs each have their own block type as that's how GLSL is written. |
| 4342 |
return ir.source.hlsl; |
| 4343 |
} |
| 4344 |
|
| 4345 |
unordered_set<uint32_t> ssbo_type_ids; |
| 4346 |
bool aliased_ssbo_types = false; |
| 4347 |
|
| 4348 |
// If we don't have any OpSource information, we need to perform some shaky heuristics. |
| 4349 |
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, const SPIRVariable &var) { |
| 4350 |
auto &type = this->get<SPIRType>(var.basetype); |
| 4351 |
if (!type.pointer || var.storage == StorageClassFunction) |
| 4352 |
return; |
| 4353 |
|
| 4354 |
bool ssbo = var.storage == StorageClassStorageBuffer || |
| 4355 |
(var.storage == StorageClassUniform && has_decoration(type.self, DecorationBufferBlock)); |
| 4356 |
|
| 4357 |
if (ssbo) |
| 4358 |
{ |
| 4359 |
if (ssbo_type_ids.count(type.self)) |
| 4360 |
aliased_ssbo_types = true; |
| 4361 |
else |
| 4362 |
ssbo_type_ids.insert(type.self); |
| 4363 |
} |
| 4364 |
}); |
| 4365 |
|
| 4366 |
// If the block name is aliased, assume we have HLSL-style UAV declarations. |
| 4367 |
return aliased_ssbo_types; |
| 4368 |
} |
| 4369 |
|
| 4370 |
bool Compiler::instruction_to_result_type(uint32_t &result_type, uint32_t &result_id, spv::Op op, const uint32_t *args, |
| 4371 |
uint32_t length) |
| 4372 |
{ |
| 4373 |
// Most instructions follow the pattern of <result-type> <result-id> <arguments>. |
| 4374 |
// There are some exceptions. |
| 4375 |
switch (op) |
| 4376 |
{ |
| 4377 |
case OpStore: |
| 4378 |
case OpCopyMemory: |
| 4379 |
case OpCopyMemorySized: |
| 4380 |
case OpImageWrite: |
| 4381 |
case OpAtomicStore: |
| 4382 |
case OpAtomicFlagClear: |
| 4383 |
case OpEmitStreamVertex: |
| 4384 |
case OpEndStreamPrimitive: |
| 4385 |
case OpControlBarrier: |
| 4386 |
case OpMemoryBarrier: |
| 4387 |
case OpGroupWaitEvents: |
| 4388 |
case OpRetainEvent: |
| 4389 |
case OpReleaseEvent: |
| 4390 |
case OpSetUserEventStatus: |
| 4391 |
case OpCaptureEventProfilingInfo: |
| 4392 |
case OpCommitReadPipe: |
| 4393 |
case OpCommitWritePipe: |
| 4394 |
case OpGroupCommitReadPipe: |
| 4395 |
case OpGroupCommitWritePipe: |
| 4396 |
case OpLine: |
| 4397 |
case OpNoLine: |
| 4398 |
return false; |
| 4399 |
|
| 4400 |
default: |
| 4401 |
if (length > 1 && maybe_get<SPIRType>(args[0]) != nullptr) |
| 4402 |
{ |
| 4403 |
result_type = args[0]; |
| 4404 |
result_id = args[1]; |
| 4405 |
return true; |
| 4406 |
} |
| 4407 |
else |
| 4408 |
return false; |
| 4409 |
} |
| 4410 |
} |
| 4411 |
|
| 4412 |
Bitset Compiler::combined_decoration_for_member(const SPIRType &type, uint32_t index) const |
| 4413 |
{ |
| 4414 |
Bitset flags; |
| 4415 |
auto *type_meta = ir.find_meta(type.self); |
| 4416 |
|
| 4417 |
if (type_meta) |
| 4418 |
{ |
| 4419 |
auto &members = type_meta->members; |
| 4420 |
if (index >= members.size()) |
| 4421 |
return flags; |
| 4422 |
auto &dec = members[index]; |
| 4423 |
|
| 4424 |
flags.merge_or(dec.decoration_flags); |
| 4425 |
|
| 4426 |
auto &member_type = get<SPIRType>(type.member_types[index]); |
| 4427 |
|
| 4428 |
// If our member type is a struct, traverse all the child members as well recursively. |
| 4429 |
auto &member_childs = member_type.member_types; |
| 4430 |
for (uint32_t i = 0; i < member_childs.size(); i++) |
| 4431 |
{ |
| 4432 |
auto &child_member_type = get<SPIRType>(member_childs[i]); |
| 4433 |
if (!child_member_type.pointer) |
| 4434 |
flags.merge_or(combined_decoration_for_member(member_type, i)); |
| 4435 |
} |
| 4436 |
} |
| 4437 |
|
| 4438 |
return flags; |
| 4439 |
} |
| 4440 |
|
| 4441 |
bool Compiler::is_desktop_only_format(spv::ImageFormat format) |
| 4442 |
{ |
| 4443 |
switch (format) |
| 4444 |
{ |
| 4445 |
// Desktop-only formats |
| 4446 |
case ImageFormatR11fG11fB10f: |
| 4447 |
case ImageFormatR16f: |
| 4448 |
case ImageFormatRgb10A2: |
| 4449 |
case ImageFormatR8: |
| 4450 |
case ImageFormatRg8: |
| 4451 |
case ImageFormatR16: |
| 4452 |
case ImageFormatRg16: |
| 4453 |
case ImageFormatRgba16: |
| 4454 |
case ImageFormatR16Snorm: |
| 4455 |
case ImageFormatRg16Snorm: |
| 4456 |
case ImageFormatRgba16Snorm: |
| 4457 |
case ImageFormatR8Snorm: |
| 4458 |
case ImageFormatRg8Snorm: |
| 4459 |
case ImageFormatR8ui: |
| 4460 |
case ImageFormatRg8ui: |
| 4461 |
case ImageFormatR16ui: |
| 4462 |
case ImageFormatRgb10a2ui: |
| 4463 |
case ImageFormatR8i: |
| 4464 |
case ImageFormatRg8i: |
| 4465 |
case ImageFormatR16i: |
| 4466 |
return true; |
| 4467 |
default: |
| 4468 |
break; |
| 4469 |
} |
| 4470 |
|
| 4471 |
return false; |
| 4472 |
} |
| 4473 |
|
| 4474 |
bool Compiler::image_is_comparison(const SPIRType &type, uint32_t id) const |
| 4475 |
{ |
| 4476 |
return type.image.depth || (comparison_ids.count(id) != 0); |
| 4477 |
} |
| 4478 |
|
| 4479 |
bool Compiler::type_is_opaque_value(const SPIRType &type) const |
| 4480 |
{ |
| 4481 |
return !type.pointer && (type.basetype == SPIRType::SampledImage || type.basetype == SPIRType::Image || |
| 4482 |
type.basetype == SPIRType::Sampler); |
| 4483 |
} |
| 4484 |
|
| 4485 |
// Make these member functions so we can easily break on any force_recompile events. |
| 4486 |
void Compiler::force_recompile() |
| 4487 |
{ |
| 4488 |
is_force_recompile = true; |
| 4489 |
} |
| 4490 |
|
| 4491 |
bool Compiler::is_forcing_recompilation() const |
| 4492 |
{ |
| 4493 |
return is_force_recompile; |
| 4494 |
} |
| 4495 |
|
| 4496 |
void Compiler::clear_force_recompile() |
| 4497 |
{ |
| 4498 |
is_force_recompile = false; |
| 4499 |
} |
| 4500 |
|
| 4501 |
Compiler::PhysicalStorageBufferPointerHandler::PhysicalStorageBufferPointerHandler(Compiler &compiler_) |
| 4502 |
: compiler(compiler_) |
| 4503 |
{ |
| 4504 |
} |
| 4505 |
|
| 4506 |
bool Compiler::PhysicalStorageBufferPointerHandler::handle(Op op, const uint32_t *args, uint32_t) |
| 4507 |
{ |
| 4508 |
if (op == OpConvertUToPtr || op == OpBitcast) |
| 4509 |
{ |
| 4510 |
auto &type = compiler.get<SPIRType>(args[0]); |
| 4511 |
if (type.storage == StorageClassPhysicalStorageBufferEXT && type.pointer && type.pointer_depth == 1) |
| 4512 |
{ |
| 4513 |
// If we need to cast to a pointer type which is not a block, we might need to synthesize ourselves |
| 4514 |
// a block type which wraps this POD type. |
| 4515 |
if (type.basetype != SPIRType::Struct) |
| 4516 |
types.insert(args[0]); |
| 4517 |
} |
| 4518 |
} |
| 4519 |
|
| 4520 |
return true; |
| 4521 |
} |
| 4522 |
|
| 4523 |
void Compiler::analyze_non_block_pointer_types() |
| 4524 |
{ |
| 4525 |
PhysicalStorageBufferPointerHandler handler(*this); |
| 4526 |
traverse_all_reachable_opcodes(get<SPIRFunction>(ir.default_entry_point), handler); |
| 4527 |
physical_storage_non_block_pointer_types.reserve(handler.types.size()); |
| 4528 |
for (auto type : handler.types) |
| 4529 |
physical_storage_non_block_pointer_types.push_back(type); |
| 4530 |
sort(begin(physical_storage_non_block_pointer_types), end(physical_storage_non_block_pointer_types)); |
| 4531 |
} |
| 4532 |
|
| 4533 |
bool Compiler::InterlockedResourceAccessPrepassHandler::handle(Op op, const uint32_t *, uint32_t) |
| 4534 |
{ |
| 4535 |
if (op == OpBeginInvocationInterlockEXT || op == OpEndInvocationInterlockEXT) |
| 4536 |
{ |
| 4537 |
if (interlock_function_id != 0 && interlock_function_id != call_stack.back()) |
| 4538 |
{ |
| 4539 |
// Most complex case, we have no sensible way of dealing with this |
| 4540 |
// other than taking the 100% conservative approach, exit early. |
| 4541 |
split_function_case = true; |
| 4542 |
return false; |
| 4543 |
} |
| 4544 |
else |
| 4545 |
{ |
| 4546 |
interlock_function_id = call_stack.back(); |
| 4547 |
// If this call is performed inside control flow we have a problem. |
| 4548 |
auto &cfg = compiler.get_cfg_for_function(interlock_function_id); |
| 4549 |
|
| 4550 |
uint32_t from_block_id = compiler.get<SPIRFunction>(interlock_function_id).entry_block; |
| 4551 |
bool outside_control_flow = cfg.node_terminates_control_flow_in_sub_graph(from_block_id, current_block_id); |
| 4552 |
if (!outside_control_flow) |
| 4553 |
control_flow_interlock = true; |
| 4554 |
} |
| 4555 |
} |
| 4556 |
return true; |
| 4557 |
} |
| 4558 |
|
| 4559 |
void Compiler::InterlockedResourceAccessPrepassHandler::rearm_current_block(const SPIRBlock &block) |
| 4560 |
{ |
| 4561 |
current_block_id = block.self; |
| 4562 |
} |
| 4563 |
|
| 4564 |
bool Compiler::InterlockedResourceAccessPrepassHandler::begin_function_scope(const uint32_t *args, uint32_t length) |
| 4565 |
{ |
| 4566 |
if (length < 3) |
| 4567 |
return false; |
| 4568 |
call_stack.push_back(args[2]); |
| 4569 |
return true; |
| 4570 |
} |
| 4571 |
|
| 4572 |
bool Compiler::InterlockedResourceAccessPrepassHandler::end_function_scope(const uint32_t *, uint32_t) |
| 4573 |
{ |
| 4574 |
call_stack.pop_back(); |
| 4575 |
return true; |
| 4576 |
} |
| 4577 |
|
| 4578 |
bool Compiler::InterlockedResourceAccessHandler::begin_function_scope(const uint32_t *args, uint32_t length) |
| 4579 |
{ |
| 4580 |
if (length < 3) |
| 4581 |
return false; |
| 4582 |
|
| 4583 |
if (args[2] == interlock_function_id) |
| 4584 |
call_stack_is_interlocked = true; |
| 4585 |
|
| 4586 |
call_stack.push_back(args[2]); |
| 4587 |
return true; |
| 4588 |
} |
| 4589 |
|
| 4590 |
bool Compiler::InterlockedResourceAccessHandler::end_function_scope(const uint32_t *, uint32_t) |
| 4591 |
{ |
| 4592 |
if (call_stack.back() == interlock_function_id) |
| 4593 |
call_stack_is_interlocked = false; |
| 4594 |
|
| 4595 |
call_stack.pop_back(); |
| 4596 |
return true; |
| 4597 |
} |
| 4598 |
|
| 4599 |
void Compiler::InterlockedResourceAccessHandler::access_potential_resource(uint32_t id) |
| 4600 |
{ |
| 4601 |
if ((use_critical_section && in_crit_sec) || (control_flow_interlock && call_stack_is_interlocked) || |
| 4602 |
split_function_case) |
| 4603 |
{ |
| 4604 |
compiler.interlocked_resources.insert(id); |
| 4605 |
} |
| 4606 |
} |
| 4607 |
|
| 4608 |
bool Compiler::InterlockedResourceAccessHandler::handle(Op opcode, const uint32_t *args, uint32_t length) |
| 4609 |
{ |
| 4610 |
// Only care about critical section analysis if we have simple case. |
| 4611 |
if (use_critical_section) |
| 4612 |
{ |
| 4613 |
if (opcode == OpBeginInvocationInterlockEXT) |
| 4614 |
{ |
| 4615 |
in_crit_sec = true; |
| 4616 |
return true; |
| 4617 |
} |
| 4618 |
|
| 4619 |
if (opcode == OpEndInvocationInterlockEXT) |
| 4620 |
{ |
| 4621 |
// End critical section--nothing more to do. |
| 4622 |
return false; |
| 4623 |
} |
| 4624 |
} |
| 4625 |
|
| 4626 |
// We need to figure out where images and buffers are loaded from, so do only the bare bones compilation we need. |
| 4627 |
switch (opcode) |
| 4628 |
{ |
| 4629 |
case OpLoad: |
| 4630 |
{ |
| 4631 |
if (length < 3) |
| 4632 |
return false; |
| 4633 |
|
| 4634 |
uint32_t ptr = args[2]; |
| 4635 |
auto *var = compiler.maybe_get_backing_variable(ptr); |
| 4636 |
|
| 4637 |
// We're only concerned with buffer and image memory here. |
| 4638 |
if (!var) |
| 4639 |
break; |
| 4640 |
|
| 4641 |
switch (var->storage) |
| 4642 |
{ |
| 4643 |
default: |
| 4644 |
break; |
| 4645 |
|
| 4646 |
case StorageClassUniformConstant: |
| 4647 |
{ |
| 4648 |
uint32_t result_type = args[0]; |
| 4649 |
uint32_t id = args[1]; |
| 4650 |
compiler.set<SPIRExpression>(id, "", result_type, true); |
| 4651 |
compiler.register_read(id, ptr, true); |
| 4652 |
break; |
| 4653 |
} |
| 4654 |
|
| 4655 |
case StorageClassUniform: |
| 4656 |
// Must have BufferBlock; we only care about SSBOs. |
| 4657 |
if (!compiler.has_decoration(compiler.get<SPIRType>(var->basetype).self, DecorationBufferBlock)) |
| 4658 |
break; |
| 4659 |
// fallthrough |
| 4660 |
case StorageClassStorageBuffer: |
| 4661 |
access_potential_resource(var->self); |
| 4662 |
break; |
| 4663 |
} |
| 4664 |
break; |
| 4665 |
} |
| 4666 |
|
| 4667 |
case OpInBoundsAccessChain: |
| 4668 |
case OpAccessChain: |
| 4669 |
case OpPtrAccessChain: |
| 4670 |
{ |
| 4671 |
if (length < 3) |
| 4672 |
return false; |
| 4673 |
|
| 4674 |
uint32_t result_type = args[0]; |
| 4675 |
|
| 4676 |
auto &type = compiler.get<SPIRType>(result_type); |
| 4677 |
if (type.storage == StorageClassUniform || type.storage == StorageClassUniformConstant || |
| 4678 |
type.storage == StorageClassStorageBuffer) |
| 4679 |
{ |
| 4680 |
uint32_t id = args[1]; |
| 4681 |
uint32_t ptr = args[2]; |
| 4682 |
compiler.set<SPIRExpression>(id, "", result_type, true); |
| 4683 |
compiler.register_read(id, ptr, true); |
| 4684 |
compiler.ir.ids[id].set_allow_type_rewrite(); |
| 4685 |
} |
| 4686 |
break; |
| 4687 |
} |
| 4688 |
|
| 4689 |
case OpImageTexelPointer: |
| 4690 |
{ |
| 4691 |
if (length < 3) |
| 4692 |
return false; |
| 4693 |
|
| 4694 |
uint32_t result_type = args[0]; |
| 4695 |
uint32_t id = args[1]; |
| 4696 |
uint32_t ptr = args[2]; |
| 4697 |
auto &e = compiler.set<SPIRExpression>(id, "", result_type, true); |
| 4698 |
auto *var = compiler.maybe_get_backing_variable(ptr); |
| 4699 |
if (var) |
| 4700 |
e.loaded_from = var->self; |
| 4701 |
break; |
| 4702 |
} |
| 4703 |
|
| 4704 |
case OpStore: |
| 4705 |
case OpImageWrite: |
| 4706 |
case OpAtomicStore: |
| 4707 |
{ |
| 4708 |
if (length < 1) |
| 4709 |
return false; |
| 4710 |
|
| 4711 |
uint32_t ptr = args[0]; |
| 4712 |
auto *var = compiler.maybe_get_backing_variable(ptr); |
| 4713 |
if (var && (var->storage == StorageClassUniform || var->storage == StorageClassUniformConstant || |
| 4714 |
var->storage == StorageClassStorageBuffer)) |
| 4715 |
{ |
| 4716 |
access_potential_resource(var->self); |
| 4717 |
} |
| 4718 |
|
| 4719 |
break; |
| 4720 |
} |
| 4721 |
|
| 4722 |
case OpCopyMemory: |
| 4723 |
{ |
| 4724 |
if (length < 2) |
| 4725 |
return false; |
| 4726 |
|
| 4727 |
uint32_t dst = args[0]; |
| 4728 |
uint32_t src = args[1]; |
| 4729 |
auto *dst_var = compiler.maybe_get_backing_variable(dst); |
| 4730 |
auto *src_var = compiler.maybe_get_backing_variable(src); |
| 4731 |
|
| 4732 |
if (dst_var && (dst_var->storage == StorageClassUniform || dst_var->storage == StorageClassStorageBuffer)) |
| 4733 |
access_potential_resource(dst_var->self); |
| 4734 |
|
| 4735 |
if (src_var) |
| 4736 |
{ |
| 4737 |
if (src_var->storage != StorageClassUniform && src_var->storage != StorageClassStorageBuffer) |
| 4738 |
break; |
| 4739 |
|
| 4740 |
if (src_var->storage == StorageClassUniform && |
| 4741 |
!compiler.has_decoration(compiler.get<SPIRType>(src_var->basetype).self, DecorationBufferBlock)) |
| 4742 |
{ |
| 4743 |
break; |
| 4744 |
} |
| 4745 |
|
| 4746 |
access_potential_resource(src_var->self); |
| 4747 |
} |
| 4748 |
|
| 4749 |
break; |
| 4750 |
} |
| 4751 |
|
| 4752 |
case OpImageRead: |
| 4753 |
case OpAtomicLoad: |
| 4754 |
{ |
| 4755 |
if (length < 3) |
| 4756 |
return false; |
| 4757 |
|
| 4758 |
uint32_t ptr = args[2]; |
| 4759 |
auto *var = compiler.maybe_get_backing_variable(ptr); |
| 4760 |
|
| 4761 |
// We're only concerned with buffer and image memory here. |
| 4762 |
if (!var) |
| 4763 |
break; |
| 4764 |
|
| 4765 |
switch (var->storage) |
| 4766 |
{ |
| 4767 |
default: |
| 4768 |
break; |
| 4769 |
|
| 4770 |
case StorageClassUniform: |
| 4771 |
// Must have BufferBlock; we only care about SSBOs. |
| 4772 |
if (!compiler.has_decoration(compiler.get<SPIRType>(var->basetype).self, DecorationBufferBlock)) |
| 4773 |
break; |
| 4774 |
// fallthrough |
| 4775 |
case StorageClassUniformConstant: |
| 4776 |
case StorageClassStorageBuffer: |
| 4777 |
access_potential_resource(var->self); |
| 4778 |
break; |
| 4779 |
} |
| 4780 |
break; |
| 4781 |
} |
| 4782 |
|
| 4783 |
case OpAtomicExchange: |
| 4784 |
case OpAtomicCompareExchange: |
| 4785 |
case OpAtomicIIncrement: |
| 4786 |
case OpAtomicIDecrement: |
| 4787 |
case OpAtomicIAdd: |
| 4788 |
case OpAtomicISub: |
| 4789 |
case OpAtomicSMin: |
| 4790 |
case OpAtomicUMin: |
| 4791 |
case OpAtomicSMax: |
| 4792 |
case OpAtomicUMax: |
| 4793 |
case OpAtomicAnd: |
| 4794 |
case OpAtomicOr: |
| 4795 |
case OpAtomicXor: |
| 4796 |
{ |
| 4797 |
if (length < 3) |
| 4798 |
return false; |
| 4799 |
|
| 4800 |
uint32_t ptr = args[2]; |
| 4801 |
auto *var = compiler.maybe_get_backing_variable(ptr); |
| 4802 |
if (var && (var->storage == StorageClassUniform || var->storage == StorageClassUniformConstant || |
| 4803 |
var->storage == StorageClassStorageBuffer)) |
| 4804 |
{ |
| 4805 |
access_potential_resource(var->self); |
| 4806 |
} |
| 4807 |
|
| 4808 |
break; |
| 4809 |
} |
| 4810 |
|
| 4811 |
default: |
| 4812 |
break; |
| 4813 |
} |
| 4814 |
|
| 4815 |
return true; |
| 4816 |
} |
| 4817 |
|
| 4818 |
void Compiler::analyze_interlocked_resource_usage() |
| 4819 |
{ |
| 4820 |
if (get_execution_model() == ExecutionModelFragment && |
| 4821 |
(get_entry_point().flags.get(ExecutionModePixelInterlockOrderedEXT) || |
| 4822 |
get_entry_point().flags.get(ExecutionModePixelInterlockUnorderedEXT) || |
| 4823 |
get_entry_point().flags.get(ExecutionModeSampleInterlockOrderedEXT) || |
| 4824 |
get_entry_point().flags.get(ExecutionModeSampleInterlockUnorderedEXT))) |
| 4825 |
{ |
| 4826 |
InterlockedResourceAccessPrepassHandler prepass_handler(*this, ir.default_entry_point); |
| 4827 |
traverse_all_reachable_opcodes(get<SPIRFunction>(ir.default_entry_point), prepass_handler); |
| 4828 |
|
| 4829 |
InterlockedResourceAccessHandler handler(*this, ir.default_entry_point); |
| 4830 |
handler.interlock_function_id = prepass_handler.interlock_function_id; |
| 4831 |
handler.split_function_case = prepass_handler.split_function_case; |
| 4832 |
handler.control_flow_interlock = prepass_handler.control_flow_interlock; |
| 4833 |
handler.use_critical_section = !handler.split_function_case && !handler.control_flow_interlock; |
| 4834 |
|
| 4835 |
traverse_all_reachable_opcodes(get<SPIRFunction>(ir.default_entry_point), handler); |
| 4836 |
|
| 4837 |
// For GLSL. If we hit any of these cases, we have to fall back to conservative approach. |
| 4838 |
interlocked_is_complex = |
| 4839 |
!handler.use_critical_section || handler.interlock_function_id != ir.default_entry_point; |
| 4840 |
} |
| 4841 |
} |
| 4842 |
|
| 4843 |
bool Compiler::type_is_array_of_pointers(const SPIRType &type) const |
| 4844 |
{ |
| 4845 |
if (!type.pointer) |
| 4846 |
return false; |
| 4847 |
|
| 4848 |
// If parent type has same pointer depth, we must have an array of pointers. |
| 4849 |
return type.pointer_depth == get<SPIRType>(type.parent_type).pointer_depth; |
| 4850 |
} |
| 4851 |
|
| 4852 |
bool Compiler::type_is_top_level_physical_pointer(const SPIRType &type) const |
| 4853 |
{ |
| 4854 |
return type.pointer && type.storage == StorageClassPhysicalStorageBuffer && |
| 4855 |
type.pointer_depth > get<SPIRType>(type.parent_type).pointer_depth; |
| 4856 |
} |
| 4857 |
|
| 4858 |
bool Compiler::flush_phi_required(BlockID from, BlockID to) const |
| 4859 |
{ |
| 4860 |
auto &child = get<SPIRBlock>(to); |
| 4861 |
for (auto &phi : child.phi_variables) |
| 4862 |
if (phi.parent == from) |
| 4863 |
return true; |
| 4864 |
return false; |
| 4865 |
} |
| 4866 |
|
| 4867 |
void Compiler::add_loop_level() |
| 4868 |
{ |
| 4869 |
current_loop_level++; |
| 4870 |
} |