Hash :
bbe68963
Author :
Date :
2024-10-21T15:11:27
Vulkan: Fix `precise` vs `mat4(...)[index]` Bug: angleproject:374801303 Change-Id: I45550abe406aaaf4d2c5eb5d7d694b2b30ab8e4e Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5949528 Reviewed-by: mohan maiya <m.maiya@samsung.com> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Charlie Lao <cclao@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
//
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// FindPreciseNodes.cpp: Propagates |precise| to AST nodes.
//
// The high level algorithm is as follows. For every node that "assigns" to a precise object,
// subobject (a precise struct whose field is being assigned) or superobject (a struct with a
// precise field), two things happen:
//
// - The operation is marked precise if it's an arithmetic operation
// - The right hand side of the assignment is made precise. If only a subobject is precise, only
// the corresponding subobject of the right hand side is made precise.
//
#include "compiler/translator/tree_util/FindPreciseNodes.h"
#include "common/hash_containers.h"
#include "common/hash_utils.h"
#include "compiler/translator/Compiler.h"
#include "compiler/translator/IntermNode.h"
#include "compiler/translator/Symbol.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
namespace sh
{
namespace
{
// An access chain applied to a variable. The |precise|-ness of a node does not change when
// indexing arrays, selecting matrix columns or swizzle vectors. This access chain thus only
// includes block field selections. The access chain is used to identify the part of an object
// that is or should be |precise|. If both a.b.c and a.b are precise, only a.b is ever considered.
class AccessChain
{
public:
AccessChain() = default;
bool operator==(const AccessChain &other) const { return mChain == other.mChain; }
const TVariable *build(TIntermTyped *lvalue);
const TVector<size_t> &getChain() const { return mChain; }
void reduceChain(size_t newSize)
{
ASSERT(newSize <= mChain.size());
mChain.resize(newSize);
}
void clear() { reduceChain(0); }
void push_back(size_t index) { mChain.push_back(index); }
void pop_front(size_t n);
void append(const AccessChain &other)
{
mChain.insert(mChain.end(), other.mChain.begin(), other.mChain.end());
}
bool removePrefix(const AccessChain &other);
private:
TVector<size_t> mChain;
};
bool IsIndexOp(TOperator op)
{
switch (op)
{
case EOpIndexDirect:
case EOpIndexDirectStruct:
case EOpIndexDirectInterfaceBlock:
case EOpIndexIndirect:
return true;
default:
return false;
}
}
const TVariable *AccessChain::build(TIntermTyped *lvalue)
{
if (lvalue->getAsSwizzleNode())
{
return build(lvalue->getAsSwizzleNode()->getOperand());
}
if (lvalue->getAsSymbolNode())
{
const TVariable *var = &lvalue->getAsSymbolNode()->variable();
// For fields of nameless interface blocks, add the field index too.
if (var->getType().getInterfaceBlock() != nullptr)
{
mChain.push_back(var->getType().getInterfaceBlockFieldIndex());
}
return var;
}
if (lvalue->getAsAggregate())
{
return nullptr;
}
TIntermBinary *binary = lvalue->getAsBinaryNode();
ASSERT(binary);
TOperator op = binary->getOp();
ASSERT(IsIndexOp(op));
const TVariable *var = build(binary->getLeft());
if (op == EOpIndexDirectStruct || op == EOpIndexDirectInterfaceBlock)
{
int fieldIndex = binary->getRight()->getAsConstantUnion()->getIConst(0);
mChain.push_back(fieldIndex);
}
return var;
}
void AccessChain::pop_front(size_t n)
{
std::rotate(mChain.begin(), mChain.begin() + n, mChain.end());
reduceChain(mChain.size() - n);
}
bool AccessChain::removePrefix(const AccessChain &other)
{
// First, make sure the common part of the two access chains match.
size_t commonSize = std::min(mChain.size(), other.mChain.size());
for (size_t index = 0; index < commonSize; ++index)
{
if (mChain[index] != other.mChain[index])
{
return false;
}
}
// Remove the common part from the access chain. If other is a deeper access chain, this access
// chain will become empty.
pop_front(commonSize);
return true;
}
AccessChain GetAssignmentAccessChain(TIntermOperator *node)
{
// The assignment is either a unary or a binary node, and the lvalue is always the first child.
AccessChain lvalueAccessChain;
lvalueAccessChain.build(node->getChildNode(0)->getAsTyped());
return lvalueAccessChain;
}
template <typename Traverser>
void TraverseIndexNodesOnly(TIntermNode *node, Traverser *traverser)
{
if (node->getAsSwizzleNode())
{
node = node->getAsSwizzleNode()->getOperand();
}
if (node->getAsSymbolNode() || node->getAsAggregate())
{
return;
}
TIntermBinary *binary = node->getAsBinaryNode();
ASSERT(binary);
TOperator op = binary->getOp();
ASSERT(IsIndexOp(op));
if (op == EOpIndexIndirect)
{
binary->getRight()->traverse(traverser);
}
TraverseIndexNodesOnly(binary->getLeft(), traverser);
}
// An object, which could be a sub-object of a variable.
struct ObjectAndAccessChain
{
const TVariable *variable;
AccessChain accessChain;
};
bool operator==(const ObjectAndAccessChain &a, const ObjectAndAccessChain &b)
{
return a.variable == b.variable && a.accessChain == b.accessChain;
}
struct ObjectAndAccessChainHash
{
size_t operator()(const ObjectAndAccessChain &object) const
{
size_t result = angle::ComputeGenericHash(&object.variable, sizeof(object.variable));
if (!object.accessChain.getChain().empty())
{
result =
result ^ angle::ComputeGenericHash(object.accessChain.getChain().data(),
object.accessChain.getChain().size() *
sizeof(object.accessChain.getChain()[0]));
}
return result;
}
};
// A map from variables to AST nodes that modify them (i.e. nodes where IsAssignment(op)).
using VariableToAssignmentNodeMap = angle::HashMap<const TVariable *, TVector<TIntermOperator *>>;
// A set of |return| nodes from functions with a |precise| return value.
using PreciseReturnNodes = angle::HashSet<TIntermBranch *>;
// A set of precise objects that need processing, or have been processed.
using PreciseObjectSet = angle::HashSet<ObjectAndAccessChain, ObjectAndAccessChainHash>;
struct ASTInfo
{
// Generic information about the tree:
VariableToAssignmentNodeMap variableAssignmentNodeMap;
// Information pertaining to |precise| expressions:
PreciseReturnNodes preciseReturnNodes;
PreciseObjectSet preciseObjectsToProcess;
PreciseObjectSet preciseObjectsVisited;
};
int GetObjectPreciseSubChainLength(const ObjectAndAccessChain &object)
{
const TType &type = object.variable->getType();
if (type.isPrecise())
{
return 0;
}
const TFieldListCollection *block = type.getInterfaceBlock();
if (block == nullptr)
{
block = type.getStruct();
}
const TVector<size_t> &accessChain = object.accessChain.getChain();
for (size_t length = 0; length < accessChain.size(); ++length)
{
ASSERT(block != nullptr);
const TField *field = block->fields()[accessChain[length]];
if (field->type()->isPrecise())
{
return static_cast<int>(length + 1);
}
block = field->type()->getStruct();
}
return -1;
}
void AddPreciseObject(ASTInfo *info, const ObjectAndAccessChain &object)
{
if (info->preciseObjectsVisited.count(object) > 0)
{
return;
}
info->preciseObjectsToProcess.insert(object);
info->preciseObjectsVisited.insert(object);
}
void AddPreciseSubObjects(ASTInfo *info, const ObjectAndAccessChain &object);
void AddObjectIfPrecise(ASTInfo *info, const ObjectAndAccessChain &object)
{
// See if the access chain is already precise, and if so add the minimum access chain that is
// precise.
int preciseSubChainLength = GetObjectPreciseSubChainLength(object);
if (preciseSubChainLength == -1)
{
// If the access chain is not precise, see if there are any fields of it that are precise,
// and add those individually.
AddPreciseSubObjects(info, object);
return;
}
ObjectAndAccessChain preciseObject = object;
preciseObject.accessChain.reduceChain(preciseSubChainLength);
AddPreciseObject(info, preciseObject);
}
void AddPreciseSubObjects(ASTInfo *info, const ObjectAndAccessChain &object)
{
const TFieldListCollection *block = object.variable->getType().getInterfaceBlock();
if (block == nullptr)
{
block = object.variable->getType().getStruct();
}
const TVector<size_t> &accessChain = object.accessChain.getChain();
for (size_t length = 0; length < accessChain.size(); ++length)
{
block = block->fields()[accessChain[length]]->type()->getStruct();
}
if (block == nullptr)
{
return;
}
for (size_t fieldIndex = 0; fieldIndex < block->fields().size(); ++fieldIndex)
{
ObjectAndAccessChain subObject = object;
subObject.accessChain.push_back(fieldIndex);
// If the field is precise, add it as a precise subobject. Otherwise recurse.
if (block->fields()[fieldIndex]->type()->isPrecise())
{
AddPreciseObject(info, subObject);
}
else
{
AddPreciseSubObjects(info, subObject);
}
}
}
bool IsArithmeticOp(TOperator op)
{
switch (op)
{
case EOpNegative:
case EOpPostIncrement:
case EOpPostDecrement:
case EOpPreIncrement:
case EOpPreDecrement:
case EOpAdd:
case EOpSub:
case EOpMul:
case EOpDiv:
case EOpIMod:
case EOpVectorTimesScalar:
case EOpVectorTimesMatrix:
case EOpMatrixTimesVector:
case EOpMatrixTimesScalar:
case EOpMatrixTimesMatrix:
case EOpAddAssign:
case EOpSubAssign:
case EOpMulAssign:
case EOpVectorTimesMatrixAssign:
case EOpVectorTimesScalarAssign:
case EOpMatrixTimesScalarAssign:
case EOpMatrixTimesMatrixAssign:
case EOpDivAssign:
case EOpIModAssign:
case EOpDot:
return true;
default:
return false;
}
}
// A traverser that gathers the following information, used to kick off processing:
//
// - For each variable, the AST nodes that modify it.
// - The set of |precise| return AST node.
// - The set of |precise| access chains assigned to.
//
class InfoGatherTraverser : public TIntermTraverser
{
public:
InfoGatherTraverser(ASTInfo *info) : TIntermTraverser(true, false, false), mInfo(info) {}
bool visitUnary(Visit visit, TIntermUnary *node) override
{
// If the node is an assignment (i.e. ++ and --), store the relevant information.
if (!IsAssignment(node->getOp()))
{
return true;
}
visitLvalue(node, node->getOperand());
return false;
}
bool visitBinary(Visit visit, TIntermBinary *node) override
{
if (IsAssignment(node->getOp()))
{
visitLvalue(node, node->getLeft());
node->getRight()->traverse(this);
return false;
}
return true;
}
bool visitDeclaration(Visit visit, TIntermDeclaration *node) override
{
const TIntermSequence &sequence = *(node->getSequence());
TIntermSymbol *symbol = sequence.front()->getAsSymbolNode();
TIntermBinary *initNode = sequence.front()->getAsBinaryNode();
TIntermTyped *initExpression = nullptr;
if (symbol == nullptr)
{
ASSERT(initNode->getOp() == EOpInitialize);
symbol = initNode->getLeft()->getAsSymbolNode();
initExpression = initNode->getRight();
}
ASSERT(symbol);
ObjectAndAccessChain object = {&symbol->variable(), {}};
AddObjectIfPrecise(mInfo, object);
if (initExpression)
{
mInfo->variableAssignmentNodeMap[object.variable].push_back(initNode);
// Visit the init expression, which may itself have assignments.
initExpression->traverse(this);
}
return false;
}
bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override
{
mCurrentFunction = node->getFunction();
for (size_t paramIndex = 0; paramIndex < mCurrentFunction->getParamCount(); ++paramIndex)
{
ObjectAndAccessChain param = {mCurrentFunction->getParam(paramIndex), {}};
AddObjectIfPrecise(mInfo, param);
}
return true;
}
bool visitBranch(Visit visit, TIntermBranch *node) override
{
if (node->getFlowOp() == EOpReturn && node->getChildCount() == 1 &&
mCurrentFunction->getReturnType().isPrecise())
{
mInfo->preciseReturnNodes.insert(node);
}
return true;
}
bool visitGlobalQualifierDeclaration(Visit visit,
TIntermGlobalQualifierDeclaration *node) override
{
if (node->isPrecise())
{
ObjectAndAccessChain preciseObject = {&node->getSymbol()->variable(), {}};
AddPreciseObject(mInfo, preciseObject);
}
return false;
}
private:
void visitLvalue(TIntermOperator *assignmentNode, TIntermTyped *lvalueNode)
{
AccessChain lvalueChain;
const TVariable *lvalueBase = lvalueChain.build(lvalueNode);
if (lvalueBase != nullptr)
{
mInfo->variableAssignmentNodeMap[lvalueBase].push_back(assignmentNode);
ObjectAndAccessChain lvalue = {lvalueBase, lvalueChain};
AddObjectIfPrecise(mInfo, lvalue);
}
TraverseIndexNodesOnly(lvalueNode, this);
}
ASTInfo *mInfo = nullptr;
const TFunction *mCurrentFunction = nullptr;
};
// A traverser that, given an access chain, traverses an expression and marks parts of it |precise|.
// For example, in the expression |Struct1(a, Struct2(b, c), d)|:
//
// - Given access chain [1], both |b| and |c| are marked precise.
// - Given access chain [1, 0], only |b| is marked precise.
//
// When access chain is empty, arithmetic nodes are marked |precise| and any access chains found in
// their children is recursively added for processing.
//
// The access chain given to the traverser is derived from the left hand side of an assignment,
// while the traverser is run on the right hand side.
class PropagatePreciseTraverser : public TIntermTraverser
{
public:
PropagatePreciseTraverser(ASTInfo *info) : TIntermTraverser(true, false, false), mInfo(info) {}
void propagatePrecise(TIntermNode *expression, const AccessChain &accessChain)
{
mCurrentAccessChain = accessChain;
expression->traverse(this);
}
bool visitUnary(Visit visit, TIntermUnary *node) override
{
// Unary operations cannot be applied to structures.
ASSERT(mCurrentAccessChain.getChain().empty());
// Mark arithmetic nodes as |precise|.
if (IsArithmeticOp(node->getOp()))
{
node->setIsPrecise();
}
// Mark the operand itself |precise| too.
return true;
}
bool visitBinary(Visit visit, TIntermBinary *node) override
{
if (IsIndexOp(node->getOp()))
{
// Append the remaining access chain with that of the node, and mark that as |precise|.
// For example, if we are evaluating an expression and expecting to mark the access
// chain [1, 3] as |precise|, and the node itself has access chain [0, 2] applied to
// variable V, then what ends up being |precise| is V with access chain [0, 2, 1, 3].
AccessChain nodeAccessChain;
const TVariable *baseVariable = nodeAccessChain.build(node);
if (baseVariable != nullptr)
{
nodeAccessChain.append(mCurrentAccessChain);
ObjectAndAccessChain preciseObject = {baseVariable, nodeAccessChain};
AddPreciseObject(mInfo, preciseObject);
}
// Visit index nodes, each of which should be considered |precise| in its entirety.
mCurrentAccessChain.clear();
TraverseIndexNodesOnly(node, this);
return false;
}
if (node->getOp() == EOpComma)
{
// For expr1,expr2, consider only expr2 as that's the one whose calculation is relevant.
node->getRight()->traverse(this);
return false;
}
// Mark arithmetic nodes as |precise|.
if (IsArithmeticOp(node->getOp()))
{
node->setIsPrecise();
}
if (IsAssignment(node->getOp()) || node->getOp() == EOpInitialize)
{
// If the node itself is a[...] op= expr, consider only expr as |precise|, as that's the
// one whose calculation is significant.
node->getRight()->traverse(this);
// The indices used on the left hand side are also significant in their entirety.
mCurrentAccessChain.clear();
TraverseIndexNodesOnly(node->getLeft(), this);
return false;
}
// Binary operations cannot be applied to structures.
ASSERT(mCurrentAccessChain.getChain().empty());
// Mark the operands themselves |precise| too.
return true;
}
void visitSymbol(TIntermSymbol *symbol) override
{
// Mark the symbol together with the current access chain as |precise|.
ObjectAndAccessChain preciseObject = {&symbol->variable(), mCurrentAccessChain};
AddPreciseObject(mInfo, preciseObject);
}
bool visitAggregate(Visit visit, TIntermAggregate *node) override
{
// If this is a struct constructor and the access chain is not empty, only apply |precise|
// to the field selected by the access chain.
const TType &type = node->getType();
const bool isStructConstructor =
node->getOp() == EOpConstruct && type.getStruct() != nullptr && !type.isArray();
if (!mCurrentAccessChain.getChain().empty() && isStructConstructor)
{
size_t selectedFieldIndex = mCurrentAccessChain.getChain().front();
mCurrentAccessChain.pop_front(1);
ASSERT(selectedFieldIndex < node->getChildCount());
// Visit only said field.
node->getChildNode(selectedFieldIndex)->traverse(this);
return false;
}
// If this is an array constructor, each element is equally |precise| with the same access
// chain. Otherwise there cannot be any access chain for constructors.
if (node->getOp() == EOpConstruct)
{
ASSERT(type.isArray() || mCurrentAccessChain.getChain().empty());
return true;
}
// Otherwise this is a function call. The access chain is irrelevant and every (non-out)
// parameter of the function call should be considered |precise|.
mCurrentAccessChain.clear();
const TFunction *function = node->getFunction();
ASSERT(function);
for (size_t paramIndex = 0; paramIndex < function->getParamCount(); ++paramIndex)
{
if (function->getParam(paramIndex)->getType().getQualifier() != EvqParamOut)
{
node->getChildNode(paramIndex)->traverse(this);
}
}
// Mark arithmetic nodes as |precise|.
if (IsArithmeticOp(node->getOp()))
{
node->setIsPrecise();
}
return false;
}
private:
ASTInfo *mInfo = nullptr;
AccessChain mCurrentAccessChain;
};
} // anonymous namespace
void FindPreciseNodes(TCompiler *compiler, TIntermBlock *root)
{
ASTInfo info;
InfoGatherTraverser infoGather(&info);
root->traverse(&infoGather);
PropagatePreciseTraverser propagator(&info);
// First, get return expressions out of the way by propagating |precise|.
for (TIntermBranch *returnNode : info.preciseReturnNodes)
{
ASSERT(returnNode->getChildCount() == 1);
propagator.propagatePrecise(returnNode->getChildNode(0), {});
}
// Now take |precise| access chains one by one, and propagate their |precise|-ness to the right
// hand side of all assignments in which they are on the left hand side, as well as the
// arithmetic expression that assigns to them.
while (!info.preciseObjectsToProcess.empty())
{
// Get one |precise| object to process.
auto first = info.preciseObjectsToProcess.begin();
const ObjectAndAccessChain toProcess = *first;
info.preciseObjectsToProcess.erase(first);
// Propagate |precise| to every node where it's assigned to.
const TVector<TIntermOperator *> &assignmentNodes =
info.variableAssignmentNodeMap[toProcess.variable];
for (TIntermOperator *assignmentNode : assignmentNodes)
{
AccessChain assignmentAccessChain = GetAssignmentAccessChain(assignmentNode);
// There are two possibilities:
//
// - The assignment is to a bigger access chain than that which is being processed, in
// which case the entire right hand side is marked |precise|,
// - The assignment is to a smaller access chain, in which case only the subobject of
// the right hand side that corresponds to the remaining part of the access chain must
// be marked |precise|.
//
// For example, if processing |a.b.c| as a |precise| access chain:
//
// - If the assignment is to |a.b.c.d|, then the entire right hand side must be
// |precise|.
// - If the assignment is to |a.b|, only the |.c| part of the right hand side expression
// must be |precise|.
// - If the assignment is to |a.e|, there is nothing to do.
//
AccessChain remainingAccessChain = toProcess.accessChain;
if (!remainingAccessChain.removePrefix(assignmentAccessChain))
{
continue;
}
propagator.propagatePrecise(assignmentNode, remainingAccessChain);
}
}
// The AST nodes now contain information gathered by this post-processing step, and so the tree
// must no longer be transformed.
compiler->enableValidateNoMoreTransformations();
}
} // namespace sh