Hash :
0e1c90cf
Author :
Date :
2021-07-21T10:24:26
Translator: Validate function parameter qualifiers
This change cleans up a confusion in EvqConst and EvqConstReadOnly,
where the former was frequently used instead of the latter for function
parameters.
The change makes the following renames to make the intent of the
relevant qualifiers clearer:
EvqIn -> EvqParamIn
EvqOut -> EvqParamOut
EvqInOut -> EvqParamInOut
EvqConstReadOnly -> EvqParamConst
Bug: angleproject:4889
Change-Id: Idedd32c08a91de069b91b1657d6b783dddece04a
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3041622
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Tim Van Patten <timvp@google.com>
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
//
// 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 "compiler/translator/TranslatorMetalDirect/RewriteOutArgs.h"
#include "compiler/translator/TranslatorMetalDirect/IntermRebuild.h"
using namespace sh;
namespace
{
template <typename T>
class SmallMultiSet
{
public:
struct Entry
{
T elem;
size_t count;
};
const Entry *find(const T &x) const
{
for (auto &entry : mEntries)
{
if (x == entry.elem)
{
return &entry;
}
}
return nullptr;
}
size_t multiplicity(const T &x) const
{
const Entry *entry = find(x);
return entry ? entry->count : 0;
}
const Entry &insert(const T &x)
{
Entry *entry = findMutable(x);
if (entry)
{
++entry->count;
return *entry;
}
else
{
mEntries.push_back({x, 1});
return mEntries.back();
}
}
void clear() { mEntries.clear(); }
bool empty() const { return mEntries.empty(); }
size_t uniqueSize() const { return mEntries.size(); }
private:
ANGLE_INLINE Entry *findMutable(const T &x) { return const_cast<Entry *>(find(x)); }
private:
std::vector<Entry> mEntries;
};
const TVariable *GetVariable(TIntermNode &node)
{
TIntermTyped *tyNode = node.getAsTyped();
ASSERT(tyNode);
if (TIntermSymbol *symbol = tyNode->getAsSymbolNode())
{
return &symbol->variable();
}
return nullptr;
}
class Rewriter : public TIntermRebuild
{
SmallMultiSet<const TVariable *> mVarBuffer; // reusable buffer
SymbolEnv &mSymbolEnv;
public:
~Rewriter() override { ASSERT(mVarBuffer.empty()); }
Rewriter(TCompiler &compiler, SymbolEnv &symbolEnv)
: TIntermRebuild(compiler, false, true), mSymbolEnv(symbolEnv)
{}
static bool argAlreadyProcessed(TIntermTyped *arg)
{
if (arg->getAsAggregate())
{
const TFunction *func = arg->getAsAggregate()->getFunction();
if (func && func->symbolType() == SymbolType::AngleInternal &&
func->name() == "swizzle_ref")
{
return true;
}
}
return false;
}
PostResult visitAggregatePost(TIntermAggregate &aggregateNode) override
{
ASSERT(mVarBuffer.empty());
const TFunction *func = aggregateNode.getFunction();
if (!func)
{
return aggregateNode;
}
TIntermSequence &args = *aggregateNode.getSequence();
size_t argCount = args.size();
auto getParamQualifier = [&](size_t i) {
const TVariable ¶m = *func->getParam(i);
const TType ¶mType = param.getType();
const TQualifier paramQual = paramType.getQualifier();
switch (paramQual)
{
case TQualifier::EvqParamOut:
case TQualifier::EvqParamInOut:
if (!mSymbolEnv.isReference(param))
{
mSymbolEnv.markAsReference(param, AddressSpace::Thread);
}
break;
default:
break;
}
return paramQual;
};
bool mightAlias = false;
for (size_t i = 0; i < argCount; ++i)
{
const TQualifier paramQual = getParamQualifier(i);
switch (paramQual)
{
case TQualifier::EvqParamOut:
case TQualifier::EvqParamInOut:
{
const TVariable *var = GetVariable(*args[i]);
if (mVarBuffer.insert(var).count > 1)
{
mightAlias = true;
i = argCount;
}
}
break;
default:
break;
}
}
const bool hasIndeterminateVar = mVarBuffer.find(nullptr);
if (!mightAlias)
{
mightAlias = hasIndeterminateVar && mVarBuffer.uniqueSize() > 1;
}
if (mightAlias)
{
for (size_t i = 0; i < argCount; ++i)
{
TIntermTyped *arg = args[i]->getAsTyped();
ASSERT(arg);
if (!argAlreadyProcessed(arg))
{
const TVariable *var = GetVariable(*arg);
const TQualifier paramQual = getParamQualifier(i);
if (hasIndeterminateVar || mVarBuffer.multiplicity(var) > 1)
{
switch (paramQual)
{
case TQualifier::EvqParamOut:
args[i] = &mSymbolEnv.callFunctionOverload(
Name("out"), arg->getType(), *new TIntermSequence{arg});
break;
case TQualifier::EvqParamInOut:
args[i] = &mSymbolEnv.callFunctionOverload(
Name("inout"), arg->getType(), *new TIntermSequence{arg});
break;
default:
break;
}
}
}
}
}
mVarBuffer.clear();
return aggregateNode;
}
};
} // anonymous namespace
bool sh::RewriteOutArgs(TCompiler &compiler, TIntermBlock &root, SymbolEnv &symbolEnv)
{
Rewriter rewriter(compiler, symbolEnv);
if (!rewriter.rebuildRoot(root))
{
return false;
}
return true;
}