// Copyright Advanced Micro Devices, Inc., or its affiliates. // SPDX-License-Identifier: MIT #include #include #include #include #include namespace rocRoller { namespace KernelGraph { using namespace ControlGraph; using namespace CoordinateGraph; using GD = rocRoller::Graph::Direction; KernelGraph NopExtraScopes::apply(KernelGraph const& original) { auto scopes = original.control.getNodes().to(); std::unordered_set extraScopes; for(int scopeIdx : scopes) { if(!original.control.getOutputNodeIndices(scopeIdx).empty()) { Log::debug("Scope {} due to outgoing sequence", scopeIdx); continue; } auto containingScope = [&]() -> std::optional { auto stack = controlStack(scopeIdx, original); for(auto iter = stack.rbegin(); iter != stack.rend(); ++iter) { if(*iter != scopeIdx && scopes.contains(*iter)) return *iter; } return std::nullopt; }(); if(!containingScope) { Log::debug("Scope {} due to no containing scope", scopeIdx); continue; } // siblings: all nodes within the containing scope other than scopeIdx; auto siblings = original.control.getOutputNodeIndices(*containingScope).to(); siblings = original.control.followEdges(siblings); siblings.erase(scopeIdx); auto allSiblingsAreBeforeScope = [&]() { for(auto const& sibling : siblings) { auto order = original.control.compareNodes(UpdateCache, sibling, scopeIdx); if(order != NodeOrdering::LeftFirst) { Log::debug("Scope {} due to sibling {} ordered {}", scopeIdx, sibling, toString(order)); return false; } } return true; }(); if(!allSiblingsAreBeforeScope) { Log::debug("Scope {} due to siblings", scopeIdx); continue; } // if we get this far, this scope is extra. extraScopes.insert(scopeIdx); } auto graph = original; for(auto scopeIdx : extraScopes) { Log::debug("Replacing {} with NOP", scopeIdx); graph.control.setElement(scopeIdx, NOP()); auto nodesByBody = graph.control.getOutputNodeIndices(scopeIdx).to(); for(auto nodeIdx : nodesByBody) { auto edgeIdx = graph.control.findEdge(scopeIdx, nodeIdx); AssertFatal(edgeIdx); graph.control.setElement(*edgeIdx, Sequence()); } } return graph; } } }