Hash :
0624b4fb
Author :
Date :
2024-10-31T20:59:20
Metal: Make ToposortStructs compile on c++17 Use .find() != .end() instead of .contains() for std::unordered_map. Bug: angleproject:375352601 Change-Id: I2e550354e1df3b390b74fdea29427fd3a0326fe8 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5979775 Commit-Queue: Kimmo Kinnunen <kkinnunen@apple.com> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: 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
//
// Copyright 2020 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 <algorithm>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "compiler/translator/ImmutableStringBuilder.h"
#include "compiler/translator/msl/AstHelpers.h"
#include "compiler/translator/msl/ToposortStructs.h"
#include "compiler/translator/tree_util/IntermNode_util.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
using namespace sh;
////////////////////////////////////////////////////////////////////////////////
namespace
{
template <typename T>
using Edges = std::unordered_set<T>;
template <typename T>
using Graph = std::unordered_map<T, Edges<T>>;
struct EdgeComparator
{
bool operator()(const TStructure *s1, const TStructure *s2) { return s2->name() < s1->name(); }
};
void BuildGraphImpl(SymbolEnv &symbolEnv, Graph<const TStructure *> &g, const TStructure *s)
{
if (g.find(s) != g.end())
{
return;
}
Edges<const TStructure *> &es = g[s];
const TFieldList &fs = s->fields();
for (const TField *f : fs)
{
if (const TStructure *z = symbolEnv.remap(f->type()->getStruct()))
{
es.insert(z);
BuildGraphImpl(symbolEnv, g, z);
Edges<const TStructure *> &ez = g[z];
es.insert(ez.begin(), ez.end());
}
}
}
Graph<const TStructure *> BuildGraph(SymbolEnv &symbolEnv,
const std::vector<const TStructure *> &structs)
{
Graph<const TStructure *> g;
for (const TStructure *s : structs)
{
BuildGraphImpl(symbolEnv, g, s);
}
return g;
}
std::vector<const TStructure *> SortEdges(const std::unordered_set<const TStructure *> &structs)
{
std::vector<const TStructure *> sorted;
sorted.reserve(structs.size());
sorted.insert(sorted.begin(), structs.begin(), structs.end());
std::sort(sorted.begin(), sorted.end(), EdgeComparator());
return sorted;
}
// Algorthm: https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
// Note that the algorithm is modified to visit nodes in sorted order. This
// ensures consistent results. Without this, the returned order (in so far as
// leaf nodes) is undefined, because iterating over an unordered_set of pointers
// depends upon the actual pointer values. Consistent results is important for
// code that keys off the string of shaders for caching.
template <typename T>
std::vector<T> Toposort(const Graph<T> &g)
{
// nodes with temporary mark
std::unordered_set<T> temps;
// nodes without permanent mark
std::unordered_set<T> invPerms;
for (const auto &entry : g)
{
invPerms.insert(entry.first);
}
// L <- Empty list that will contain the sorted elements
std::vector<T> L;
// function visit(node n)
std::function<void(T)> visit = [&](T n) -> void {
// if n has a permanent mark then
if (invPerms.find(n) == invPerms.end())
{
// return
return;
}
// if n has a temporary mark then
if (temps.find(n) != temps.end())
{
// stop (not a DAG)
UNREACHABLE();
}
// mark n with a temporary mark
temps.insert(n);
// for each node m with an edge from n to m do
auto enIter = g.find(n);
ASSERT(enIter != g.end());
std::vector<T> sorted = SortEdges(enIter->second);
for (T m : sorted)
{
// visit(m)
visit(m);
}
// remove temporary mark from n
temps.erase(n);
// mark n with a permanent mark
invPerms.erase(n);
// add n to head of L
L.push_back(n);
};
// while exists nodes without a permanent mark do
while (!invPerms.empty())
{
// select an unmarked node n
std::vector<T> sorted = SortEdges(invPerms);
T n = *sorted.begin();
// visit(n)
visit(n);
}
return L;
}
TIntermFunctionDefinition *CreateStructEqualityFunction(
TSymbolTable &symbolTable,
const TStructure &aStructType,
const std::unordered_map<const TStructure *, const TFunction *> &equalityFunctions)
{
auto &funcEquality =
*new TFunction(&symbolTable, ImmutableString("equal"), SymbolType::AngleInternal,
new TType(TBasicType::EbtBool), true);
auto &aStruct = CreateInstanceVariable(symbolTable, aStructType, Name("a"));
auto &bStruct = CreateInstanceVariable(symbolTable, aStructType, Name("b"));
funcEquality.addParameter(&aStruct);
funcEquality.addParameter(&bStruct);
auto &bodyEquality = *new TIntermBlock();
std::vector<TIntermTyped *> andNodes;
const TFieldList &aFields = aStructType.fields();
const size_t size = aFields.size();
auto testEquality = [&](TIntermTyped &a, TIntermTyped &b) -> TIntermTyped * {
ASSERT(a.getType() == b.getType());
const TType &type = a.getType();
if (const TStructure *structure = type.getStruct(); structure != nullptr)
{
auto func = equalityFunctions.find(structure);
if (func != equalityFunctions.end())
{
return TIntermAggregate::CreateFunctionCall(*func->second,
new TIntermSequence{&a, &b});
}
UNREACHABLE();
}
return new TIntermBinary(TOperator::EOpEqual, &a, &b);
};
for (size_t idx = 0; idx < size; ++idx)
{
const TField &aField = *aFields[idx];
const TType &aFieldType = *aField.type();
const Name aFieldName(aField);
if (aFieldType.isArray())
{
ASSERT(!aFieldType.isArrayOfArrays()); // TODO
int dim = aFieldType.getOutermostArraySize();
for (int d = 0; d < dim; ++d)
{
auto &aAccess = AccessIndex(AccessField(aStruct, aFieldName), d);
auto &bAccess = AccessIndex(AccessField(bStruct, aFieldName), d);
auto *eqNode = testEquality(bAccess, aAccess);
andNodes.push_back(eqNode);
}
}
else
{
auto &aAccess = AccessField(aStruct, aFieldName);
auto &bAccess = AccessField(bStruct, aFieldName);
auto *eqNode = testEquality(bAccess, aAccess);
andNodes.push_back(eqNode);
}
}
ASSERT(andNodes.size() > 0); // Empty structs are not allowed in GLSL
TIntermTyped *outNode = andNodes.back();
andNodes.pop_back();
for (TIntermTyped *andNode : andNodes)
{
outNode = new TIntermBinary(TOperator::EOpLogicalAnd, andNode, outNode);
}
bodyEquality.appendStatement(new TIntermBranch(TOperator::EOpReturn, outNode));
auto *funcProtoEquality = new TIntermFunctionPrototype(&funcEquality);
return new TIntermFunctionDefinition(funcProtoEquality, &bodyEquality);
}
struct DeclaredStructure
{
TIntermDeclaration *declNode;
const TStructure *structure;
};
bool GetAsDeclaredStructure(SymbolEnv &symbolEnv, TIntermNode &node, DeclaredStructure &out)
{
if (TIntermDeclaration *declNode = node.getAsDeclarationNode())
{
ASSERT(declNode->getChildCount() == 1);
TIntermNode &childNode = *declNode->getChildNode(0);
if (TIntermSymbol *symbolNode = childNode.getAsSymbolNode())
{
const TVariable &var = symbolNode->variable();
const TType &type = var.getType();
if (const TStructure *structure = symbolEnv.remap(type.getStruct()))
{
if (type.isStructSpecifier())
{
out.declNode = declNode;
out.structure = structure;
return true;
}
}
}
}
return false;
}
class FindStructEqualityUse : public TIntermTraverser
{
public:
SymbolEnv &mSymbolEnv;
std::unordered_set<const TStructure *> mUsedStructs;
FindStructEqualityUse(SymbolEnv &symbolEnv)
: TIntermTraverser(false, false, true), mSymbolEnv(symbolEnv)
{}
bool visitBinary(Visit, TIntermBinary *binary) override
{
const TOperator op = binary->getOp();
switch (op)
{
case TOperator::EOpEqual:
case TOperator::EOpNotEqual:
{
const TType &leftType = binary->getLeft()->getType();
const TType &rightType = binary->getRight()->getType();
ASSERT(leftType.getStruct() == rightType.getStruct());
if (const TStructure *structure = mSymbolEnv.remap(leftType.getStruct()))
{
useStruct(*structure);
}
}
break;
default:
break;
}
return true;
}
private:
void useStruct(const TStructure &structure)
{
if (mUsedStructs.insert(&structure).second)
{
for (const TField *field : structure.fields())
{
if (const TStructure *subStruct = mSymbolEnv.remap(field->type()->getStruct()))
{
useStruct(*subStruct);
}
}
}
}
};
} // anonymous namespace
////////////////////////////////////////////////////////////////////////////////
bool sh::ToposortStructs(TCompiler &compiler,
SymbolEnv &symbolEnv,
TIntermBlock &root,
ProgramPreludeConfig &ppc)
{
FindStructEqualityUse finder(symbolEnv);
root.traverse(&finder);
auto &usedStructs = finder.mUsedStructs;
std::vector<DeclaredStructure> declaredStructs;
std::vector<TIntermNode *> nonStructStmtNodes;
{
DeclaredStructure declaredStruct;
const size_t stmtCount = root.getChildCount();
for (size_t i = 0; i < stmtCount; ++i)
{
TIntermNode &stmtNode = *root.getChildNode(i);
if (GetAsDeclaredStructure(symbolEnv, stmtNode, declaredStruct))
{
declaredStructs.push_back(declaredStruct);
}
else
{
nonStructStmtNodes.push_back(&stmtNode);
}
}
}
{
std::vector<const TStructure *> structs;
std::unordered_map<const TStructure *, DeclaredStructure> rawToDeclared;
for (const DeclaredStructure &d : declaredStructs)
{
structs.push_back(d.structure);
ASSERT(rawToDeclared.find(d.structure) == rawToDeclared.end());
rawToDeclared[d.structure] = d;
}
// Note: Graph may contain more than only explicitly declared structures.
Graph<const TStructure *> g = BuildGraph(symbolEnv, structs);
std::vector<const TStructure *> sortedStructs = Toposort(g);
ASSERT(declaredStructs.size() <= sortedStructs.size());
declaredStructs.clear();
for (const TStructure *s : sortedStructs)
{
auto it = rawToDeclared.find(s);
if (it != rawToDeclared.end())
{
auto &d = it->second;
ASSERT(d.declNode);
declaredStructs.push_back(d);
}
}
}
{
TIntermSequence newStmtNodes;
std::unordered_map<const TStructure *, const TFunction *> equalityFunctions;
for (auto &[declNode, structure] : declaredStructs)
{
newStmtNodes.push_back(declNode);
if (usedStructs.find(structure) != usedStructs.end())
{
TIntermFunctionDefinition *eq = CreateStructEqualityFunction(
compiler.getSymbolTable(), *structure, equalityFunctions);
newStmtNodes.push_back(eq);
equalityFunctions[structure] = eq->getFunction();
}
}
for (TIntermNode *stmtNode : nonStructStmtNodes)
{
ASSERT(stmtNode);
newStmtNodes.push_back(stmtNode);
}
*root.getSequence() = newStmtNodes;
}
return compiler.validateAST(&root);
}