Path: blob/master/thirdparty/glslang/SPIRV/InReadableOrder.cpp
9903 views
//1// Copyright (C) 2016 Google, Inc.2//3// All rights reserved.4//5// Redistribution and use in source and binary forms, with or without6// modification, are permitted provided that the following conditions7// are met:8//9// Redistributions of source code must retain the above copyright10// notice, this list of conditions and the following disclaimer.11//12// Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following14// disclaimer in the documentation and/or other materials provided15// with the distribution.16//17// Neither the name of 3Dlabs Inc. Ltd. nor the names of its18// contributors may be used to endorse or promote products derived19// from this software without specific prior written permission.20//21// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS22// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT23// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS24// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE25// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,26// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,27// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;28// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER29// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN31// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE32// POSSIBILITY OF SUCH DAMAGE.3334// The SPIR-V spec requires code blocks to appear in an order satisfying the35// dominator-tree direction (ie, dominator before the dominated). This is,36// actually, easy to achieve: any pre-order CFG traversal algorithm will do it.37// Because such algorithms visit a block only after traversing some path to it38// from the root, they necessarily visit the block's idom first.39//40// But not every graph-traversal algorithm outputs blocks in an order that41// appears logical to human readers. The problem is that unrelated branches may42// be interspersed with each other, and merge blocks may come before some of the43// branches being merged.44//45// A good, human-readable order of blocks may be achieved by performing46// depth-first search but delaying merge nodes until after all their branches47// have been visited. This is implemented below by the inReadableOrder()48// function.4950#include "spvIR.h"5152#include <cassert>53#include <unordered_set>5455using spv::Block;56using spv::Id;5758namespace {59// Traverses CFG in a readable order, invoking a pre-set callback on each block.60// Use by calling visit() on the root block.61class ReadableOrderTraverser {62public:63ReadableOrderTraverser(std::function<void(Block*, spv::ReachReason, Block*)> callback)64: callback_(callback) {}65// Visits the block if it hasn't been visited already and isn't currently66// being delayed. Invokes callback(block, why, header), then descends into its67// successors. Delays merge-block and continue-block processing until all68// the branches have been completed. If |block| is an unreachable merge block or69// an unreachable continue target, then |header| is the corresponding header block.70void visit(Block* block, spv::ReachReason why, Block* header)71{72assert(block);73if (why == spv::ReachViaControlFlow) {74reachableViaControlFlow_.insert(block);75}76if (visited_.count(block) || delayed_.count(block))77return;78callback_(block, why, header);79visited_.insert(block);80Block* mergeBlock = nullptr;81Block* continueBlock = nullptr;82auto mergeInst = block->getMergeInstruction();83if (mergeInst) {84Id mergeId = mergeInst->getIdOperand(0);85mergeBlock = block->getParent().getParent().getInstruction(mergeId)->getBlock();86delayed_.insert(mergeBlock);87if (mergeInst->getOpCode() == spv::OpLoopMerge) {88Id continueId = mergeInst->getIdOperand(1);89continueBlock =90block->getParent().getParent().getInstruction(continueId)->getBlock();91delayed_.insert(continueBlock);92}93}94if (why == spv::ReachViaControlFlow) {95const auto& successors = block->getSuccessors();96for (auto it = successors.cbegin(); it != successors.cend(); ++it)97visit(*it, why, nullptr);98}99if (continueBlock) {100const spv::ReachReason continueWhy =101(reachableViaControlFlow_.count(continueBlock) > 0)102? spv::ReachViaControlFlow103: spv::ReachDeadContinue;104delayed_.erase(continueBlock);105visit(continueBlock, continueWhy, block);106}107if (mergeBlock) {108const spv::ReachReason mergeWhy =109(reachableViaControlFlow_.count(mergeBlock) > 0)110? spv::ReachViaControlFlow111: spv::ReachDeadMerge;112delayed_.erase(mergeBlock);113visit(mergeBlock, mergeWhy, block);114}115}116117private:118std::function<void(Block*, spv::ReachReason, Block*)> callback_;119// Whether a block has already been visited or is being delayed.120std::unordered_set<Block *> visited_, delayed_;121122// The set of blocks that actually are reached via control flow.123std::unordered_set<Block *> reachableViaControlFlow_;124};125}126127void spv::inReadableOrder(Block* root, std::function<void(Block*, spv::ReachReason, Block*)> callback)128{129ReadableOrderTraverser(callback).visit(root, spv::ReachViaControlFlow, nullptr);130}131132133