Hash :
c2d1a464
Author :
Date :
2021-04-09T16:53:30
Translator: Track nameless interface block field indices in type Fields of nameless interface blocks are stored as TVariables and referenced through a TIntermSymbols (unlike named blocks which use EOpIndexDirectInterfaceBlock with a constant field indices). With this change, the field index is stored in the variable's type which let's the TField of the TInterfaceBlock be directly accessible (and not need a search by name). This will be helpful in translation to SPIR-V as interface block members are accessed by field index. Bug: angleproject:4889 Change-Id: If3ab45b1e5f5f9576721dc52e2bdf1161882514f Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2818242 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Tim Van Patten <timvp@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
//
// Copyright 2019 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.
//
#include "compiler/translator/ValidateAST.h"
#include "compiler/translator/Diagnostics.h"
#include "compiler/translator/Symbol.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
#include "compiler/translator/tree_util/SpecializationConstant.h"
namespace sh
{
namespace
{
class ValidateAST : public TIntermTraverser
{
public:
static bool validate(TIntermNode *root,
TDiagnostics *diagnostics,
const ValidateASTOptions &options);
void visitSymbol(TIntermSymbol *node) override;
void visitConstantUnion(TIntermConstantUnion *node) override;
bool visitSwizzle(Visit visit, TIntermSwizzle *node) override;
bool visitBinary(Visit visit, TIntermBinary *node) override;
bool visitUnary(Visit visit, TIntermUnary *node) override;
bool visitTernary(Visit visit, TIntermTernary *node) override;
bool visitIfElse(Visit visit, TIntermIfElse *node) override;
bool visitSwitch(Visit visit, TIntermSwitch *node) override;
bool visitCase(Visit visit, TIntermCase *node) override;
void visitFunctionPrototype(TIntermFunctionPrototype *node) override;
bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override;
bool visitAggregate(Visit visit, TIntermAggregate *node) override;
bool visitBlock(Visit visit, TIntermBlock *node) override;
bool visitGlobalQualifierDeclaration(Visit visit,
TIntermGlobalQualifierDeclaration *node) override;
bool visitDeclaration(Visit visit, TIntermDeclaration *node) override;
bool visitLoop(Visit visit, TIntermLoop *node) override;
bool visitBranch(Visit visit, TIntermBranch *node) override;
void visitPreprocessorDirective(TIntermPreprocessorDirective *node) override;
private:
ValidateAST(TIntermNode *root, TDiagnostics *diagnostics, const ValidateASTOptions &options);
// Visit as a generic node
void visitNode(Visit visit, TIntermNode *node);
void scope(Visit visit);
bool isVariableDeclared(const TVariable *variable);
bool variableNeedsDeclaration(const TVariable *variable);
void expectNonNullChildren(Visit visit, TIntermNode *node, size_t least_count);
bool validateInternal();
ValidateASTOptions mOptions;
TDiagnostics *mDiagnostics;
// For validateSingleParent:
std::map<TIntermNode *, TIntermNode *> mParent;
bool mSingleParentFailed = false;
// For validateVariableReferences:
std::vector<std::set<const TVariable *>> mDeclaredVariables;
std::set<const TInterfaceBlock *> mNamelessInterfaceBlocks;
bool mVariableReferencesFailed = false;
// For validateNullNodes:
bool mNullNodesFailed = false;
// For validateMultiDeclarations:
bool mMultiDeclarationsFailed = false;
};
bool ValidateAST::validate(TIntermNode *root,
TDiagnostics *diagnostics,
const ValidateASTOptions &options)
{
ValidateAST validate(root, diagnostics, options);
root->traverse(&validate);
return validate.validateInternal();
}
ValidateAST::ValidateAST(TIntermNode *root,
TDiagnostics *diagnostics,
const ValidateASTOptions &options)
: TIntermTraverser(true, false, true, nullptr), mOptions(options), mDiagnostics(diagnostics)
{
bool isTreeRoot = root->getAsBlock() && root->getAsBlock()->isTreeRoot();
// Some validations are not applicable unless run on the entire tree.
if (!isTreeRoot)
{
mOptions.validateVariableReferences = false;
}
if (mOptions.validateSingleParent)
{
mParent[root] = nullptr;
}
}
void ValidateAST::visitNode(Visit visit, TIntermNode *node)
{
if (visit == PreVisit && mOptions.validateSingleParent)
{
size_t childCount = node->getChildCount();
for (size_t i = 0; i < childCount; ++i)
{
TIntermNode *child = node->getChildNode(i);
if (mParent.find(child) != mParent.end())
{
// If child is visited twice but through the same parent, the problem is in one of
// the ancestors.
if (mParent[child] != node)
{
mDiagnostics->error(node->getLine(), "Found child with two parents",
"<validateSingleParent>");
mSingleParentFailed = true;
}
}
mParent[child] = node;
}
}
}
void ValidateAST::scope(Visit visit)
{
if (mOptions.validateVariableReferences)
{
if (visit == PreVisit)
{
mDeclaredVariables.push_back({});
}
else if (visit == PostVisit)
{
mDeclaredVariables.pop_back();
}
}
}
bool ValidateAST::isVariableDeclared(const TVariable *variable)
{
ASSERT(mOptions.validateVariableReferences);
for (const std::set<const TVariable *> &scopeVariables : mDeclaredVariables)
{
if (scopeVariables.count(variable) > 0)
{
return true;
}
}
return false;
}
bool ValidateAST::variableNeedsDeclaration(const TVariable *variable)
{
// Don't expect declaration for built-in variables.
if (variable->name().beginsWith("gl_"))
{
return false;
}
// Additionally, don't expect declaration for Vulkan specialization constants. There is no
// representation for them in the AST.
if (variable->symbolType() == SymbolType::AngleInternal &&
SpecConst::IsSpecConstName(variable->name()))
{
return false;
}
return true;
}
void ValidateAST::expectNonNullChildren(Visit visit, TIntermNode *node, size_t least_count)
{
if (visit == PreVisit && mOptions.validateNullNodes)
{
size_t childCount = node->getChildCount();
if (childCount < least_count)
{
mDiagnostics->error(node->getLine(), "Too few children", "<validateNullNodes>");
mNullNodesFailed = true;
}
for (size_t i = 0; i < childCount; ++i)
{
if (node->getChildNode(i) == nullptr)
{
mDiagnostics->error(node->getLine(), "Found nullptr child", "<validateNullNodes>");
mNullNodesFailed = true;
}
}
}
}
void ValidateAST::visitSymbol(TIntermSymbol *node)
{
visitNode(PreVisit, node);
const TVariable *variable = &node->variable();
const TType &type = node->getType();
if (mOptions.validateVariableReferences && variableNeedsDeclaration(variable))
{
// If it's a reference to a field of a nameless interface block, match it by index and name.
if (type.getInterfaceBlock() && !type.isInterfaceBlock())
{
const TInterfaceBlock *interfaceBlock = type.getInterfaceBlock();
const TFieldList &fieldList = interfaceBlock->fields();
const size_t fieldIndex = type.getInterfaceBlockFieldIndex();
if (mNamelessInterfaceBlocks.count(interfaceBlock) == 0)
{
mDiagnostics->error(node->getLine(),
"Found reference to undeclared or inconsistenly redeclared "
"nameless interface block <validateVariableReferences>",
node->getName().data());
mVariableReferencesFailed = true;
}
else if (fieldIndex >= fieldList.size() ||
node->getName() != fieldList[fieldIndex]->name())
{
mDiagnostics->error(node->getLine(),
"Found reference to inconsistenly redeclared nameless "
"interface block field <validateVariableReferences>",
node->getName().data());
mVariableReferencesFailed = true;
}
}
else
{
if (!isVariableDeclared(variable))
{
mDiagnostics->error(node->getLine(),
"Found reference to undeclared or inconsistently redeclared "
"variable <validateVariableReferences>",
node->getName().data());
mVariableReferencesFailed = true;
}
}
}
}
void ValidateAST::visitConstantUnion(TIntermConstantUnion *node)
{
visitNode(PreVisit, node);
}
bool ValidateAST::visitSwizzle(Visit visit, TIntermSwizzle *node)
{
visitNode(visit, node);
return true;
}
bool ValidateAST::visitBinary(Visit visit, TIntermBinary *node)
{
visitNode(visit, node);
return true;
}
bool ValidateAST::visitUnary(Visit visit, TIntermUnary *node)
{
visitNode(visit, node);
return true;
}
bool ValidateAST::visitTernary(Visit visit, TIntermTernary *node)
{
visitNode(visit, node);
return true;
}
bool ValidateAST::visitIfElse(Visit visit, TIntermIfElse *node)
{
visitNode(visit, node);
return true;
}
bool ValidateAST::visitSwitch(Visit visit, TIntermSwitch *node)
{
visitNode(visit, node);
return true;
}
bool ValidateAST::visitCase(Visit visit, TIntermCase *node)
{
visitNode(visit, node);
return true;
}
void ValidateAST::visitFunctionPrototype(TIntermFunctionPrototype *node)
{
visitNode(PreVisit, node);
}
bool ValidateAST::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
{
visitNode(visit, node);
scope(visit);
if (mOptions.validateVariableReferences && visit == PreVisit)
{
const TFunction *function = node->getFunction();
size_t paramCount = function->getParamCount();
for (size_t paramIndex = 0; paramIndex < paramCount; ++paramIndex)
{
const TVariable *variable = function->getParam(paramIndex);
if (isVariableDeclared(variable))
{
mDiagnostics->error(node->getLine(),
"Found two declarations of the same function argument "
"<validateVariableReferences>",
variable->name().data());
mVariableReferencesFailed = true;
break;
}
mDeclaredVariables.back().insert(variable);
}
}
return true;
}
bool ValidateAST::visitAggregate(Visit visit, TIntermAggregate *node)
{
visitNode(visit, node);
expectNonNullChildren(visit, node, 0);
return true;
}
bool ValidateAST::visitBlock(Visit visit, TIntermBlock *node)
{
visitNode(visit, node);
scope(visit);
expectNonNullChildren(visit, node, 0);
return true;
}
bool ValidateAST::visitGlobalQualifierDeclaration(Visit visit,
TIntermGlobalQualifierDeclaration *node)
{
visitNode(visit, node);
return true;
}
bool ValidateAST::visitDeclaration(Visit visit, TIntermDeclaration *node)
{
visitNode(visit, node);
expectNonNullChildren(visit, node, 0);
const TIntermSequence &sequence = *(node->getSequence());
if (mOptions.validateMultiDeclarations && sequence.size() > 1)
{
mMultiDeclarationsFailed = true;
}
if (mOptions.validateVariableReferences && visit == PreVisit)
{
for (TIntermNode *instance : sequence)
{
TIntermSymbol *symbol = instance->getAsSymbolNode();
if (symbol == nullptr)
{
TIntermBinary *init = instance->getAsBinaryNode();
ASSERT(init && init->getOp() == EOpInitialize);
symbol = init->getLeft()->getAsSymbolNode();
}
ASSERT(symbol);
const TVariable *variable = &symbol->variable();
if (isVariableDeclared(variable))
{
mDiagnostics->error(
node->getLine(),
"Found two declarations of the same variable <validateVariableReferences>",
variable->name().data());
mVariableReferencesFailed = true;
break;
}
mDeclaredVariables.back().insert(variable);
const TInterfaceBlock *interfaceBlock = variable->getType().getInterfaceBlock();
if (variable->symbolType() == SymbolType::Empty && interfaceBlock != nullptr)
{
// Nameless interface blocks can only be declared at the top level. Their fields
// are matched by field index, and then verified to match by name. Conflict in
// names should have already generated a compile error.
ASSERT(mDeclaredVariables.size() == 1);
ASSERT(mNamelessInterfaceBlocks.count(interfaceBlock) == 0);
mNamelessInterfaceBlocks.insert(interfaceBlock);
}
}
}
return true;
}
bool ValidateAST::visitLoop(Visit visit, TIntermLoop *node)
{
visitNode(visit, node);
return true;
}
bool ValidateAST::visitBranch(Visit visit, TIntermBranch *node)
{
visitNode(visit, node);
return true;
}
void ValidateAST::visitPreprocessorDirective(TIntermPreprocessorDirective *node)
{
visitNode(PreVisit, node);
}
bool ValidateAST::validateInternal()
{
return !mSingleParentFailed && !mVariableReferencesFailed && !mNullNodesFailed &&
!mMultiDeclarationsFailed;
}
} // anonymous namespace
bool ValidateAST(TIntermNode *root, TDiagnostics *diagnostics, const ValidateASTOptions &options)
{
return ValidateAST::validate(root, diagnostics, options);
}
} // namespace sh