/******************************************************************************* * * MIT License * * Copyright 2024-2025 AMD ROCm(TM) Software * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * *******************************************************************************/ #pragma once #include #include #include "TopoVisitor.hpp" #include namespace rocRoller::KernelGraph { template TopoControlGraphVisitor::TopoControlGraphVisitor(KernelGraph const& kgraph) : m_graph(kgraph) { } template void TopoControlGraphVisitor::reset() { m_visitedNodes.clear(); } template std::unordered_set& TopoControlGraphVisitor::nodeDependencies(int node) { namespace CG = ControlGraph; auto iter = m_nodeDependencies.find(node); if(iter == m_nodeDependencies.end()) { auto [newIter, wasInserted] = m_nodeDependencies.emplace( node, m_graph.control.getInputNodeIndices(node).to()); AssertFatal(wasInserted); iter = newIter; } return iter->second; } template bool TopoControlGraphVisitor::hasWalkedInputs(int node) { auto& deps = nodeDependencies(node); for(auto iter = deps.begin(); iter != deps.end();) { if(m_visitedNodes.contains(*iter)) { iter = deps.erase(iter); } else { return false; } } return true; } template void TopoControlGraphVisitor::walk() { auto nodes = m_graph.control.roots().to(); walk(nodes); } template void TopoControlGraphVisitor::walk(std::set nodes) { namespace CG = ControlGraph; nodes = m_graph.control.followEdges(nodes); while(!nodes.empty()) { bool walkedAny = false; for(auto iter = nodes.begin(); iter != nodes.end();) { auto node = *iter; if(hasWalkedInputs(node)) { walk(node); iter = nodes.erase(iter); walkedAny = true; } else { ++iter; } } if(!walkedAny) { std::ostringstream msg; msg << "Graph cannot be completely walked! Node"; if(nodes.size() != 1) msg << "s"; msg << " ("; streamJoin(msg, nodes, ", "); msg << ") remain"; if(nodes.size() == 1) msg << "s"; msg << "."; errorCondition(msg.str()); break; } } } template void TopoControlGraphVisitor::errorCondition(std::string const& message) { AssertFatal(false, message); } template void TopoControlGraphVisitor::walk(int node) { namespace CG = ControlGraph; call(node); walk(m_graph.control.getOutputNodeIndices(node).to()); walk(m_graph.control.getOutputNodeIndices(node).to()); walk(m_graph.control.getOutputNodeIndices(node).to()); walk(m_graph.control.getOutputNodeIndices(node).to()); m_visitedNodes.insert(node); } template constexpr Derived* TopoControlGraphVisitor::derived() { return static_cast(this); } template auto TopoControlGraphVisitor::call(int node) { auto op = m_graph.control.getNode(node); return std::visit(*derived(), singleVariant(node), op); } }