summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/angle/src/compiler/translator/VectorizeVectorScalarArithmetic.cpp
blob: 1e79a609915bb222bb7341c0fef250c855b9f59a (plain)
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
// Copyright (c) 2017 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.
//
// VectorizeVectorScalarArithmetic.cpp: Turn some arithmetic operations that operate on a float
// vector-scalar pair into vector-vector operations. This is done recursively. Some scalar binary
// operations inside vector constructors are also turned into vector operations.
//
// This is targeted to work around a bug in NVIDIA OpenGL drivers that was reproducible on NVIDIA
// driver version 387.92. It works around the most common occurrences of the bug.

#include "compiler/translator/VectorizeVectorScalarArithmetic.h"

#include <set>

#include "compiler/translator/IntermNode.h"
#include "compiler/translator/IntermTraverse.h"

namespace sh
{

namespace
{

class VectorizeVectorScalarArithmeticTraverser : public TIntermTraverser
{
  public:
    VectorizeVectorScalarArithmeticTraverser(TSymbolTable *symbolTable)
        : TIntermTraverser(true, false, false, symbolTable), mReplaced(false)
    {
    }

    bool didReplaceScalarsWithVectors() { return mReplaced; }
    void nextIteration()
    {
        mReplaced = false;
        mModifiedBlocks.clear();
    }

  protected:
    bool visitBinary(Visit visit, TIntermBinary *node) override;
    bool visitAggregate(Visit visit, TIntermAggregate *node) override;

  private:
    // These helpers should only be called from visitAggregate when visiting a constructor.
    // argBinary is the only argument of the constructor.
    void replaceMathInsideConstructor(TIntermAggregate *node, TIntermBinary *argBinary);
    void replaceAssignInsideConstructor(const TIntermAggregate *node,
                                        const TIntermBinary *argBinary);

    static TIntermTyped *Vectorize(TIntermTyped *node,
                                   TType vectorType,
                                   TIntermTraverser::OriginalNode *originalNodeFate);

    bool mReplaced;
    std::set<const TIntermBlock *> mModifiedBlocks;
};

TIntermTyped *VectorizeVectorScalarArithmeticTraverser::Vectorize(
    TIntermTyped *node,
    TType vectorType,
    TIntermTraverser::OriginalNode *originalNodeFate)
{
    ASSERT(node->isScalar());
    vectorType.setQualifier(EvqTemporary);
    TIntermSequence vectorConstructorArgs;
    vectorConstructorArgs.push_back(node);
    TIntermAggregate *vectorized =
        TIntermAggregate::CreateConstructor(vectorType, &vectorConstructorArgs);
    TIntermTyped *vectorizedFolded = vectorized->fold(nullptr);
    if (originalNodeFate != nullptr)
    {
        if (vectorizedFolded != vectorized)
        {
            *originalNodeFate = OriginalNode::IS_DROPPED;
        }
        else
        {
            *originalNodeFate = OriginalNode::BECOMES_CHILD;
        }
    }
    return vectorizedFolded;
}

bool VectorizeVectorScalarArithmeticTraverser::visitBinary(Visit /*visit*/, TIntermBinary *node)
{
    TIntermTyped *left  = node->getLeft();
    TIntermTyped *right = node->getRight();
    ASSERT(left);
    ASSERT(right);
    switch (node->getOp())
    {
        case EOpAdd:
        case EOpAddAssign:
            // Only these specific ops are necessary to turn into vector ops.
            break;
        default:
            return true;
    }
    if (node->getBasicType() != EbtFloat)
    {
        // Only float ops have reproduced the bug.
        return true;
    }
    if (left->isScalar() && right->isVector())
    {
        ASSERT(!node->isAssignment());
        ASSERT(!right->isArray());
        OriginalNode originalNodeFate;
        TIntermTyped *leftVectorized = Vectorize(left, right->getType(), &originalNodeFate);
        queueReplacementWithParent(node, left, leftVectorized, originalNodeFate);
        mReplaced = true;
        // Don't replace more nodes in the same subtree on this traversal. However, nodes elsewhere
        // in the tree may still be replaced.
        return false;
    }
    else if (left->isVector() && right->isScalar())
    {
        OriginalNode originalNodeFate;
        TIntermTyped *rightVectorized = Vectorize(right, left->getType(), &originalNodeFate);
        queueReplacementWithParent(node, right, rightVectorized, originalNodeFate);
        mReplaced = true;
        // Don't replace more nodes in the same subtree on this traversal. However, nodes elsewhere
        // in the tree may still be replaced.
        return false;
    }
    return true;
}

void VectorizeVectorScalarArithmeticTraverser::replaceMathInsideConstructor(
    TIntermAggregate *node,
    TIntermBinary *argBinary)
{
    // Turn:
    //   a * b
    // into:
    //   gvec(a) * gvec(b)

    TIntermTyped *left  = argBinary->getLeft();
    TIntermTyped *right = argBinary->getRight();
    ASSERT(left->isScalar() && right->isScalar());

    TType leftVectorizedType = left->getType();
    leftVectorizedType.setPrimarySize(static_cast<unsigned char>(node->getType().getNominalSize()));
    TIntermTyped *leftVectorized = Vectorize(left, leftVectorizedType, nullptr);
    TType rightVectorizedType    = right->getType();
    rightVectorizedType.setPrimarySize(
        static_cast<unsigned char>(node->getType().getNominalSize()));
    TIntermTyped *rightVectorized = Vectorize(right, rightVectorizedType, nullptr);

    TIntermBinary *newArg = new TIntermBinary(argBinary->getOp(), leftVectorized, rightVectorized);
    queueReplacementWithParent(node, argBinary, newArg, OriginalNode::IS_DROPPED);
}

void VectorizeVectorScalarArithmeticTraverser::replaceAssignInsideConstructor(
    const TIntermAggregate *node,
    const TIntermBinary *argBinary)
{
    // Turn:
    //   gvec(a *= b);
    // into:
    //   // This is inserted into the parent block:
    //   gvec s0 = gvec(a);
    //
    //   // This goes where the gvec constructor used to be:
    //   ((s0 *= b, a = s0.x), s0);

    TIntermTyped *left  = argBinary->getLeft();
    TIntermTyped *right = argBinary->getRight();
    ASSERT(left->isScalar() && right->isScalar());
    ASSERT(!left->hasSideEffects());

    TType vecType = node->getType();
    vecType.setQualifier(EvqTemporary);

    nextTemporaryId();
    // gvec s0 = gvec(a);
    // s0 is called "tempAssignmentTarget" below.
    TIntermTyped *tempAssignmentTargetInitializer = Vectorize(left->deepCopy(), vecType, nullptr);
    TIntermDeclaration *tempAssignmentTargetDeclaration =
        createTempInitDeclaration(tempAssignmentTargetInitializer);

    // s0 *= b
    TOperator compoundAssignmentOp = argBinary->getOp();
    if (compoundAssignmentOp == EOpMulAssign)
    {
        compoundAssignmentOp = EOpVectorTimesScalarAssign;
    }
    TIntermBinary *replacementCompoundAssignment =
        new TIntermBinary(compoundAssignmentOp, createTempSymbol(vecType), right->deepCopy());

    // s0.x
    TVector<int> swizzleXOffset;
    swizzleXOffset.push_back(0);
    TIntermSwizzle *tempAssignmentTargetX =
        new TIntermSwizzle(createTempSymbol(vecType), swizzleXOffset);
    // a = s0.x
    TIntermBinary *replacementAssignBackToTarget =
        new TIntermBinary(EOpAssign, left->deepCopy(), tempAssignmentTargetX);

    // s0 *= b, a = s0.x
    TIntermBinary *replacementSequenceLeft =
        new TIntermBinary(EOpComma, replacementCompoundAssignment, replacementAssignBackToTarget);
    // (s0 *= b, a = s0.x), s0
    TIntermBinary *replacementSequence =
        new TIntermBinary(EOpComma, replacementSequenceLeft, createTempSymbol(vecType));

    insertStatementInParentBlock(tempAssignmentTargetDeclaration);
    queueReplacement(replacementSequence, OriginalNode::IS_DROPPED);
}

bool VectorizeVectorScalarArithmeticTraverser::visitAggregate(Visit /*visit*/,
                                                              TIntermAggregate *node)
{
    // Transform scalar binary expressions inside vector constructors.
    if (!node->isConstructor() || !node->isVector() || node->getSequence()->size() != 1)
    {
        return true;
    }
    TIntermTyped *argument = node->getSequence()->back()->getAsTyped();
    ASSERT(argument);
    if (!argument->isScalar() || argument->getBasicType() != EbtFloat)
    {
        return true;
    }
    TIntermBinary *argBinary = argument->getAsBinaryNode();
    if (!argBinary)
    {
        return true;
    }

    // Only specific ops are necessary to change.
    switch (argBinary->getOp())
    {
        case EOpMul:
        case EOpDiv:
        {
            replaceMathInsideConstructor(node, argBinary);
            mReplaced = true;
            // Don't replace more nodes in the same subtree on this traversal. However, nodes
            // elsewhere in the tree may still be replaced.
            return false;
        }
        case EOpMulAssign:
        case EOpDivAssign:
        {
            // The case where the left side has side effects is too complicated to deal with, so we
            // leave that be.
            if (!argBinary->getLeft()->hasSideEffects())
            {
                const TIntermBlock *parentBlock = getParentBlock();
                // We can't do more than one insertion to the same block on the same traversal.
                if (mModifiedBlocks.find(parentBlock) == mModifiedBlocks.end())
                {
                    replaceAssignInsideConstructor(node, argBinary);
                    mModifiedBlocks.insert(parentBlock);
                    mReplaced = true;
                    // Don't replace more nodes in the same subtree on this traversal.
                    // However, nodes elsewhere in the tree may still be replaced.
                    return false;
                }
            }
            break;
        }
        default:
            return true;
    }
    return true;
}

}  // anonymous namespace

void VectorizeVectorScalarArithmetic(TIntermBlock *root, TSymbolTable *symbolTable)
{
    VectorizeVectorScalarArithmeticTraverser traverser(symbolTable);
    do
    {
        traverser.nextIteration();
        root->traverse(&traverser);
        traverser.updateTree();
    } while (traverser.didReplaceScalarsWithVectors());
}

}  // namespace sh