Hash :
57dd0e5f
Author :
Date :
2023-03-27T13:05:36
Translator: Initialize out arguments too for WebGL Local variables were initialized for WebGL, but not `out` arguments. However, `out` arguments completely overwrite the passed-in variable at call site, so they should also be zeroed out. Bug: chromium:1425685 Change-Id: Ib2f61a962eea74c4933aafa4d5ad377189cec578 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4375137 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Kenneth Russell <kbr@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
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
//
// Copyright 2002 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/tree_ops/InitializeVariables.h"
#include "angle_gl.h"
#include "common/debug.h"
#include "compiler/translator/Compiler.h"
#include "compiler/translator/StaticType.h"
#include "compiler/translator/SymbolTable.h"
#include "compiler/translator/tree_util/FindMain.h"
#include "compiler/translator/tree_util/FindSymbolNode.h"
#include "compiler/translator/tree_util/IntermNode_util.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
#include "compiler/translator/util.h"
namespace sh
{
namespace
{
void AddArrayZeroInitSequence(const TIntermTyped *initializedNode,
bool canUseLoopsToInitialize,
bool highPrecisionSupported,
TIntermSequence *initSequenceOut,
TSymbolTable *symbolTable);
void AddStructZeroInitSequence(const TIntermTyped *initializedNode,
bool canUseLoopsToInitialize,
bool highPrecisionSupported,
TIntermSequence *initSequenceOut,
TSymbolTable *symbolTable);
TIntermBinary *CreateZeroInitAssignment(const TIntermTyped *initializedNode)
{
TIntermTyped *zero = CreateZeroNode(initializedNode->getType());
return new TIntermBinary(EOpAssign, initializedNode->deepCopy(), zero);
}
void AddZeroInitSequence(const TIntermTyped *initializedNode,
bool canUseLoopsToInitialize,
bool highPrecisionSupported,
TIntermSequence *initSequenceOut,
TSymbolTable *symbolTable)
{
if (initializedNode->isArray())
{
AddArrayZeroInitSequence(initializedNode, canUseLoopsToInitialize, highPrecisionSupported,
initSequenceOut, symbolTable);
}
else if (initializedNode->getType().isStructureContainingArrays() ||
initializedNode->getType().isNamelessStruct())
{
AddStructZeroInitSequence(initializedNode, canUseLoopsToInitialize, highPrecisionSupported,
initSequenceOut, symbolTable);
}
else if (initializedNode->getType().isInterfaceBlock())
{
const TType &type = initializedNode->getType();
const TInterfaceBlock &interfaceBlock = *type.getInterfaceBlock();
const TFieldList &fieldList = interfaceBlock.fields();
for (size_t fieldIndex = 0; fieldIndex < fieldList.size(); ++fieldIndex)
{
const TField &field = *fieldList[fieldIndex];
TIntermTyped *fieldIndexRef = CreateIndexNode(static_cast<int>(fieldIndex));
TIntermTyped *fieldReference =
new TIntermBinary(TOperator::EOpIndexDirectInterfaceBlock,
initializedNode->deepCopy(), fieldIndexRef);
TIntermTyped *fieldZero = CreateZeroNode(*field.type());
TIntermTyped *assignment =
new TIntermBinary(TOperator::EOpAssign, fieldReference, fieldZero);
initSequenceOut->push_back(assignment);
}
}
else
{
initSequenceOut->push_back(CreateZeroInitAssignment(initializedNode));
}
}
void AddStructZeroInitSequence(const TIntermTyped *initializedNode,
bool canUseLoopsToInitialize,
bool highPrecisionSupported,
TIntermSequence *initSequenceOut,
TSymbolTable *symbolTable)
{
ASSERT(initializedNode->getBasicType() == EbtStruct);
const TStructure *structType = initializedNode->getType().getStruct();
for (int i = 0; i < static_cast<int>(structType->fields().size()); ++i)
{
TIntermBinary *element = new TIntermBinary(EOpIndexDirectStruct,
initializedNode->deepCopy(), CreateIndexNode(i));
// Structs can't be defined inside structs, so the type of a struct field can't be a
// nameless struct.
ASSERT(!element->getType().isNamelessStruct());
AddZeroInitSequence(element, canUseLoopsToInitialize, highPrecisionSupported,
initSequenceOut, symbolTable);
}
}
void AddArrayZeroInitStatementList(const TIntermTyped *initializedNode,
bool canUseLoopsToInitialize,
bool highPrecisionSupported,
TIntermSequence *initSequenceOut,
TSymbolTable *symbolTable)
{
for (unsigned int i = 0; i < initializedNode->getOutermostArraySize(); ++i)
{
TIntermBinary *element =
new TIntermBinary(EOpIndexDirect, initializedNode->deepCopy(), CreateIndexNode(i));
AddZeroInitSequence(element, canUseLoopsToInitialize, highPrecisionSupported,
initSequenceOut, symbolTable);
}
}
void AddArrayZeroInitForLoop(const TIntermTyped *initializedNode,
bool highPrecisionSupported,
TIntermSequence *initSequenceOut,
TSymbolTable *symbolTable)
{
ASSERT(initializedNode->isArray());
const TType *mediumpIndexType = StaticType::Get<EbtInt, EbpMedium, EvqTemporary, 1, 1>();
const TType *highpIndexType = StaticType::Get<EbtInt, EbpHigh, EvqTemporary, 1, 1>();
TVariable *indexVariable =
CreateTempVariable(symbolTable, highPrecisionSupported ? highpIndexType : mediumpIndexType);
TIntermSymbol *indexSymbolNode = CreateTempSymbolNode(indexVariable);
TIntermDeclaration *indexInit =
CreateTempInitDeclarationNode(indexVariable, CreateZeroNode(indexVariable->getType()));
TIntermConstantUnion *arraySizeNode = CreateIndexNode(initializedNode->getOutermostArraySize());
TIntermBinary *indexSmallerThanSize =
new TIntermBinary(EOpLessThan, indexSymbolNode->deepCopy(), arraySizeNode);
TIntermUnary *indexIncrement =
new TIntermUnary(EOpPreIncrement, indexSymbolNode->deepCopy(), nullptr);
TIntermBlock *forLoopBody = new TIntermBlock();
TIntermSequence *forLoopBodySeq = forLoopBody->getSequence();
TIntermBinary *element = new TIntermBinary(EOpIndexIndirect, initializedNode->deepCopy(),
indexSymbolNode->deepCopy());
AddZeroInitSequence(element, true, highPrecisionSupported, forLoopBodySeq, symbolTable);
TIntermLoop *forLoop =
new TIntermLoop(ELoopFor, indexInit, indexSmallerThanSize, indexIncrement, forLoopBody);
initSequenceOut->push_back(forLoop);
}
void AddArrayZeroInitSequence(const TIntermTyped *initializedNode,
bool canUseLoopsToInitialize,
bool highPrecisionSupported,
TIntermSequence *initSequenceOut,
TSymbolTable *symbolTable)
{
// The array elements are assigned one by one to keep the AST compatible with ESSL 1.00 which
// doesn't have array assignment. We'll do this either with a for loop or just a list of
// statements assigning to each array index. Note that it is important to have the array init in
// the right order to workaround http://crbug.com/709317
bool isSmallArray = initializedNode->getOutermostArraySize() <= 1u ||
(initializedNode->getBasicType() != EbtStruct &&
!initializedNode->getType().isArrayOfArrays() &&
initializedNode->getOutermostArraySize() <= 3u);
if (initializedNode->getQualifier() == EvqFragData ||
initializedNode->getQualifier() == EvqFragmentOut || isSmallArray ||
!canUseLoopsToInitialize)
{
// Fragment outputs should not be indexed by non-constant indices.
// Also it doesn't make sense to use loops to initialize very small arrays.
AddArrayZeroInitStatementList(initializedNode, canUseLoopsToInitialize,
highPrecisionSupported, initSequenceOut, symbolTable);
}
else
{
AddArrayZeroInitForLoop(initializedNode, highPrecisionSupported, initSequenceOut,
symbolTable);
}
}
void InsertInitCode(TCompiler *compiler,
TIntermBlock *root,
const InitVariableList &variables,
TSymbolTable *symbolTable,
int shaderVersion,
const TExtensionBehavior &extensionBehavior,
bool canUseLoopsToInitialize,
bool highPrecisionSupported)
{
TIntermSequence *mainBody = FindMainBody(root)->getSequence();
for (const ShaderVariable &var : variables)
{
// Note that tempVariableName will reference a short-lived char array here - that's fine
// since we're only using it to find symbols.
ImmutableString tempVariableName(var.name.c_str(), var.name.length());
TIntermTyped *initializedSymbol = nullptr;
if (var.isBuiltIn() && !symbolTable->findUserDefined(tempVariableName))
{
initializedSymbol =
ReferenceBuiltInVariable(tempVariableName, *symbolTable, shaderVersion);
if (initializedSymbol->getQualifier() == EvqFragData &&
!IsExtensionEnabled(extensionBehavior, TExtension::EXT_draw_buffers))
{
// If GL_EXT_draw_buffers is disabled, only the 0th index of gl_FragData can be
// written to.
// TODO(oetuaho): This is a bit hacky and would be better to remove, if we came up
// with a good way to do it. Right now "gl_FragData" in symbol table is initialized
// to have the array size of MaxDrawBuffers, and the initialization happens before
// the shader sets the extensions it is using.
initializedSymbol =
new TIntermBinary(EOpIndexDirect, initializedSymbol, CreateIndexNode(0));
}
else if (initializedSymbol->getQualifier() == EvqClipDistance ||
initializedSymbol->getQualifier() == EvqCullDistance)
{
// The built-in may have been implicitly resized.
initializedSymbol =
new TIntermSymbol(&FindSymbolNode(root, tempVariableName)->variable());
}
}
else
{
if (tempVariableName != "")
{
initializedSymbol = ReferenceGlobalVariable(tempVariableName, *symbolTable);
}
else
{
// Must be a nameless interface block.
ASSERT(var.structOrBlockName != "");
const TSymbol *symbol = symbolTable->findGlobal(var.structOrBlockName);
ASSERT(symbol && symbol->isInterfaceBlock());
const TInterfaceBlock *block = static_cast<const TInterfaceBlock *>(symbol);
for (const TField *field : block->fields())
{
initializedSymbol = ReferenceGlobalVariable(field->name(), *symbolTable);
TIntermSequence initCode;
CreateInitCode(initializedSymbol, canUseLoopsToInitialize,
highPrecisionSupported, &initCode, symbolTable);
mainBody->insert(mainBody->begin(), initCode.begin(), initCode.end());
}
// Already inserted init code in this case
continue;
}
}
ASSERT(initializedSymbol != nullptr);
TIntermSequence initCode;
CreateInitCode(initializedSymbol, canUseLoopsToInitialize, highPrecisionSupported,
&initCode, symbolTable);
mainBody->insert(mainBody->begin(), initCode.begin(), initCode.end());
}
}
class InitializeLocalsTraverser : public TIntermTraverser
{
public:
InitializeLocalsTraverser(int shaderVersion,
TSymbolTable *symbolTable,
bool canUseLoopsToInitialize,
bool highPrecisionSupported)
: TIntermTraverser(true, false, false, symbolTable),
mShaderVersion(shaderVersion),
mCanUseLoopsToInitialize(canUseLoopsToInitialize),
mHighPrecisionSupported(highPrecisionSupported)
{}
protected:
bool visitDeclaration(Visit visit, TIntermDeclaration *node) override
{
for (TIntermNode *declarator : *node->getSequence())
{
if (!mInGlobalScope && !declarator->getAsBinaryNode())
{
TIntermSymbol *symbol = declarator->getAsSymbolNode();
ASSERT(symbol);
if (symbol->variable().symbolType() == SymbolType::Empty)
{
continue;
}
// Arrays may need to be initialized one element at a time, since ESSL 1.00 does not
// support array constructors or assigning arrays.
bool arrayConstructorUnavailable =
(symbol->isArray() || symbol->getType().isStructureContainingArrays()) &&
mShaderVersion == 100;
// Nameless struct constructors can't be referred to, so they also need to be
// initialized one element at a time.
// TODO(oetuaho): Check if it makes sense to initialize using a loop, even if we
// could use an initializer. It could at least reduce code size for very large
// arrays, but could hurt runtime performance.
if (arrayConstructorUnavailable || symbol->getType().isNamelessStruct())
{
// SimplifyLoopConditions should have been run so the parent node of this node
// should not be a loop.
ASSERT(getParentNode()->getAsLoopNode() == nullptr);
// SeparateDeclarations should have already been run, so we don't need to worry
// about further declarators in this declaration depending on the effects of
// this declarator.
ASSERT(node->getSequence()->size() == 1);
TIntermSequence initCode;
CreateInitCode(symbol, mCanUseLoopsToInitialize, mHighPrecisionSupported,
&initCode, mSymbolTable);
insertStatementsInParentBlock(TIntermSequence(), initCode);
}
else
{
TIntermBinary *init =
new TIntermBinary(EOpInitialize, symbol, CreateZeroNode(symbol->getType()));
queueReplacementWithParent(node, symbol, init, OriginalNode::BECOMES_CHILD);
}
}
}
return false;
}
bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override
{
// Initialize output function arguments as well, the parameter passed in at call time may be
// clobbered if the function doesn't fully write to the argument.
TIntermSequence initCode;
const TFunction *function = node->getFunction();
for (size_t paramIndex = 0; paramIndex < function->getParamCount(); ++paramIndex)
{
const TVariable *paramVariable = function->getParam(paramIndex);
const TType ¶mType = paramVariable->getType();
if (paramType.getQualifier() != EvqParamOut)
{
continue;
}
CreateInitCode(new TIntermSymbol(paramVariable), mCanUseLoopsToInitialize,
mHighPrecisionSupported, &initCode, mSymbolTable);
}
if (!initCode.empty())
{
TIntermSequence *body = node->getBody()->getSequence();
body->insert(body->begin(), initCode.begin(), initCode.end());
}
return true;
}
private:
int mShaderVersion;
bool mCanUseLoopsToInitialize;
bool mHighPrecisionSupported;
};
} // namespace
void CreateInitCode(const TIntermTyped *initializedSymbol,
bool canUseLoopsToInitialize,
bool highPrecisionSupported,
TIntermSequence *initCode,
TSymbolTable *symbolTable)
{
AddZeroInitSequence(initializedSymbol, canUseLoopsToInitialize, highPrecisionSupported,
initCode, symbolTable);
}
bool InitializeUninitializedLocals(TCompiler *compiler,
TIntermBlock *root,
int shaderVersion,
bool canUseLoopsToInitialize,
bool highPrecisionSupported,
TSymbolTable *symbolTable)
{
InitializeLocalsTraverser traverser(shaderVersion, symbolTable, canUseLoopsToInitialize,
highPrecisionSupported);
root->traverse(&traverser);
return traverser.updateTree(compiler, root);
}
bool InitializeVariables(TCompiler *compiler,
TIntermBlock *root,
const InitVariableList &vars,
TSymbolTable *symbolTable,
int shaderVersion,
const TExtensionBehavior &extensionBehavior,
bool canUseLoopsToInitialize,
bool highPrecisionSupported)
{
InsertInitCode(compiler, root, vars, symbolTable, shaderVersion, extensionBehavior,
canUseLoopsToInitialize, highPrecisionSupported);
return compiler->validateAST(root);
}
} // namespace sh