Edit

kc3-lang/angle/src/compiler/translator/tree_ops/SplitSequenceOperator.cpp

Branch :

  • Show log

    Commit

  • Author : Shahbaz Youssefi
    Date : 2019-08-19 16:32:13
    Hash : 472c74c6
    Message : Translator: Allow tree validation in children of TCompiler This is to be able to perform validation inside TranslatorVulkan, even if it's through ASSERTs. Additionally, every transformation is changed such that they do their validation themselves. TIntermTraverser::updateTree() performs the validation, which indirectly validates many of three tree transformations. Some of the more ancient transformations that don't use this function directly call TCompiler::validateAST. Bug: angleproject:2733 Change-Id: Ie4af029d34e053c5ad1dc8c2c2568eecd625d344 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1761149 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>

  • src/compiler/translator/tree_ops/SplitSequenceOperator.cpp
  • //
    // Copyright 2016 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.
    //
    // SplitSequenceOperator is an AST traverser that detects sequence operator expressions that
    // go through further AST transformations that generate statements, and splits them so that
    // possible side effects of earlier parts of the sequence operator expression are guaranteed to be
    // evaluated before the latter parts of the sequence operator expression are evaluated.
    //
    
    #include "compiler/translator/tree_ops/SplitSequenceOperator.h"
    
    #include "compiler/translator/tree_util/IntermNodePatternMatcher.h"
    #include "compiler/translator/tree_util/IntermTraverse.h"
    
    namespace sh
    {
    
    namespace
    {
    
    class SplitSequenceOperatorTraverser : public TLValueTrackingTraverser
    {
      public:
        SplitSequenceOperatorTraverser(unsigned int patternsToSplitMask, TSymbolTable *symbolTable);
    
        bool visitUnary(Visit visit, TIntermUnary *node) override;
        bool visitBinary(Visit visit, TIntermBinary *node) override;
        bool visitAggregate(Visit visit, TIntermAggregate *node) override;
        bool visitTernary(Visit visit, TIntermTernary *node) override;
    
        void nextIteration();
        bool foundExpressionToSplit() const { return mFoundExpressionToSplit; }
    
      protected:
        // Marked to true once an operation that needs to be hoisted out of the expression has been
        // found. After that, no more AST updates are performed on that traversal.
        bool mFoundExpressionToSplit;
        int mInsideSequenceOperator;
    
        IntermNodePatternMatcher mPatternToSplitMatcher;
    };
    
    SplitSequenceOperatorTraverser::SplitSequenceOperatorTraverser(unsigned int patternsToSplitMask,
                                                                   TSymbolTable *symbolTable)
        : TLValueTrackingTraverser(true, false, true, symbolTable),
          mFoundExpressionToSplit(false),
          mInsideSequenceOperator(0),
          mPatternToSplitMatcher(patternsToSplitMask)
    {}
    
    void SplitSequenceOperatorTraverser::nextIteration()
    {
        mFoundExpressionToSplit = false;
        mInsideSequenceOperator = 0;
    }
    
    bool SplitSequenceOperatorTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
    {
        if (mFoundExpressionToSplit)
            return false;
    
        if (mInsideSequenceOperator > 0 && visit == PreVisit)
        {
            // Detect expressions that need to be simplified
            mFoundExpressionToSplit = mPatternToSplitMatcher.match(node, getParentNode());
            return !mFoundExpressionToSplit;
        }
    
        return true;
    }
    
    bool SplitSequenceOperatorTraverser::visitUnary(Visit visit, TIntermUnary *node)
    {
        if (mFoundExpressionToSplit)
            return false;
    
        if (mInsideSequenceOperator > 0 && visit == PreVisit)
        {
            // Detect expressions that need to be simplified
            mFoundExpressionToSplit = mPatternToSplitMatcher.match(node);
            return !mFoundExpressionToSplit;
        }
    
        return true;
    }
    
    bool SplitSequenceOperatorTraverser::visitBinary(Visit visit, TIntermBinary *node)
    {
        if (node->getOp() == EOpComma)
        {
            if (visit == PreVisit)
            {
                if (mFoundExpressionToSplit)
                {
                    return false;
                }
                mInsideSequenceOperator++;
            }
            else if (visit == PostVisit)
            {
                // Split sequence operators starting from the outermost one to preserve correct
                // execution order.
                if (mFoundExpressionToSplit && mInsideSequenceOperator == 1)
                {
                    // Move the left side operand into a separate statement in the parent block.
                    TIntermSequence insertions;
                    insertions.push_back(node->getLeft());
                    insertStatementsInParentBlock(insertions);
                    // Replace the comma node with its right side operand.
                    queueReplacement(node->getRight(), OriginalNode::IS_DROPPED);
                }
                mInsideSequenceOperator--;
            }
            return true;
        }
    
        if (mFoundExpressionToSplit)
            return false;
    
        if (mInsideSequenceOperator > 0 && visit == PreVisit)
        {
            // Detect expressions that need to be simplified
            mFoundExpressionToSplit =
                mPatternToSplitMatcher.match(node, getParentNode(), isLValueRequiredHere());
            return !mFoundExpressionToSplit;
        }
    
        return true;
    }
    
    bool SplitSequenceOperatorTraverser::visitTernary(Visit visit, TIntermTernary *node)
    {
        if (mFoundExpressionToSplit)
            return false;
    
        if (mInsideSequenceOperator > 0 && visit == PreVisit)
        {
            // Detect expressions that need to be simplified
            mFoundExpressionToSplit = mPatternToSplitMatcher.match(node);
            return !mFoundExpressionToSplit;
        }
    
        return true;
    }
    
    }  // namespace
    
    bool SplitSequenceOperator(TCompiler *compiler,
                               TIntermNode *root,
                               int patternsToSplitMask,
                               TSymbolTable *symbolTable)
    {
        SplitSequenceOperatorTraverser traverser(patternsToSplitMask, symbolTable);
        // Separate one expression at a time, and reset the traverser between iterations.
        do
        {
            traverser.nextIteration();
            root->traverse(&traverser);
            if (traverser.foundExpressionToSplit())
            {
                if (!traverser.updateTree(compiler, root))
                {
                    return false;
                }
            }
        } while (traverser.foundExpressionToSplit());
    
        return true;
    }
    
    }  // namespace sh