Hash :
d193d51b
Author :
Date :
2024-06-17T22:46:08
Replace issue ids post migration to new issue tracker This change replaces anglebug.com/NNNN links. Bug: None Change-Id: I8ac3aec8d2a8a844b3d7b99fc0a6b2be8da31761 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5637912 Reviewed-by: Geoff Lang <geofflang@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
//
// Copyright 2023 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 <set>
#include "compiler/translator/tree_ops/RescopeGlobalVariables.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
#include "compiler/translator/tree_util/ReplaceVariable.h"
#include "compiler/translator/util.h"
namespace sh
{
////////////////////////////////////////////////////////////////////////////////
namespace
{
class Rescoper : public TIntermTraverser
{
public:
struct VariableInfo
{
std::set<TIntermFunctionDefinition *> functions;
TIntermDeclaration *declaration;
};
Rescoper(TSymbolTable *symbolTable) : TIntermTraverser(true, false, true, symbolTable) {}
bool rescope(TCompiler *compiler, TIntermBlock &root)
{
if (mGlobalVarsNeedRescope.empty())
{
return true;
}
// Insert the declarations into the first block in the function body and keep track of which
// ones have been moved, as well as the variables to replace.
VariableReplacementMap replacementMap;
std::set<TIntermDeclaration *> movedDeclarations;
for (auto &pair : mGlobalVarsNeedRescope)
{
if (pair.second.functions.size() == 1)
{
TIntermFunctionDefinition *func = *pair.second.functions.begin();
// Function* may be a nullptr if the variable was used in a
// global initializer. Don't rescope, http://anglebug.com/42266827
if (func != nullptr)
{
TIntermSequence *funcSequence = func->getBody()->getSequence();
funcSequence->insert(funcSequence->begin(), pair.second.declaration);
TType *newType = new TType(pair.first->getType());
newType->setQualifier(TQualifier::EvqTemporary);
const TVariable *newVar =
new TVariable(&compiler->getSymbolTable(), pair.first->name(), newType,
pair.first->symbolType(), pair.first->extensions());
replacementMap[pair.first] = new TIntermSymbol(newVar);
movedDeclarations.insert(pair.second.declaration);
}
}
}
// Remove the global declarations that have been moved from the root block.
TIntermSequence *rootOriginal = root.getSequence();
TIntermSequence rootReplacement;
for (TIntermNode *node : *rootOriginal)
{
if (movedDeclarations.find(node->getAsDeclarationNode()) == movedDeclarations.end())
{
rootReplacement.push_back(node);
}
}
*rootOriginal = std::move(rootReplacement);
return ReplaceVariables(compiler, &root, replacementMap);
}
protected:
void visitSymbol(TIntermSymbol *node) override
{
const TVariable &var = node->variable();
// Check that the symbol is in the globals list, but is not LHS of
// the current global initialiser
if (&var != mCurrentGlobal &&
mGlobalVarsNeedRescope.find(&var) != mGlobalVarsNeedRescope.end())
{
std::set<TIntermFunctionDefinition *> &set = mGlobalVarsNeedRescope.at(&var).functions;
if (set.find(mCurrentFunction) == set.end())
{
set.emplace(mCurrentFunction);
}
}
}
bool visitDeclaration(Visit visit, TIntermDeclaration *node) override
{
if (visit == Visit::PreVisit)
{
Declaration decl = ViewDeclaration(*node);
const TVariable &var = decl.symbol.variable();
const TType &nodeType = var.getType();
const TQualifier qualifier = nodeType.getQualifier();
if (qualifier == TQualifier::EvqGlobal && !nodeType.isStructSpecifier())
{
mGlobalVarsNeedRescope.emplace(&var, VariableInfo());
mGlobalVarsNeedRescope.at(&var).declaration = node;
}
// A declaration outside function definition context would be a
// global variable, set the flag to avoid rescoping any variables
// used in initializers.
if (!mCurrentFunction)
{
mCurrentGlobal = &var;
}
}
else if (visit == Visit::PostVisit)
{
if (!mCurrentFunction)
{
mCurrentGlobal = nullptr;
}
}
return true;
}
bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override
{
if (visit == Visit::PreVisit)
{
TIntermFunctionDefinition *func = node->getAsFunctionDefinition();
if (func)
{
mCurrentFunction = func;
}
}
else if (visit == Visit::PostVisit)
{
if (mCurrentFunction && mCurrentFunction == node->getAsFunctionDefinition())
{
mCurrentFunction = nullptr;
}
}
return true;
}
private:
TUnorderedMap<const TVariable *, VariableInfo> mGlobalVarsNeedRescope;
TIntermFunctionDefinition *mCurrentFunction = nullptr;
const TVariable *mCurrentGlobal = nullptr;
};
} // anonymous namespace
////////////////////////////////////////////////////////////////////////////////
bool RescopeGlobalVariables(TCompiler &compiler, TIntermBlock &root)
{
TSymbolTable &symbolTable = compiler.getSymbolTable();
Rescoper rescoper(&symbolTable);
root.traverse(&rescoper);
return rescoper.rescope(&compiler, root);
}
} // namespace sh