Hash :
711db275
Author :
Date :
2023-08-23T11:06:42
Translator: remove usage of contains() It's not available pre-C++20 Bug: angleproject:8311 Change-Id: I41940b5f8e6a90bc0224852aefe54643f2be9cb5 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4806924 Reviewed-by: Yuly Novikov <ynovikov@chromium.org> Commit-Queue: Yuly Novikov <ynovikov@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
//
// 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();
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();
if (mCurrentFunction && 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;
}
}
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;
};
} // 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