Files
TheRock/rocm-libraries/shared/rocroller/lib/source/KernelGraph/KernelGraph.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

199 lines
7.4 KiB
C++

// Copyright Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT
#include <rocRoller/KernelGraph/KernelGraph.hpp>
#include <rocRoller/KernelGraph/Transforms/GraphTransform.hpp>
#include <rocRoller/KernelGraph/Utils.hpp>
#include <rocRoller/Utilities/Settings.hpp>
namespace rocRoller
{
namespace KernelGraph
{
void KernelGraph::setTransformerByForLoopOp(CoordinateGraph::Transformer& transformer,
int forLoopOp)
{
auto loopIncrTag = mapper.get(forLoopOp, NaryArgument::DEST);
auto expr = std::make_shared<Expression::Expression>(rocRoller::Expression::DataFlowTag{
loopIncrTag, Register::Type::Scalar, rocRoller::DataType::Int32});
auto loopDims
= coordinates.getOutputNodeIndices<CoordinateGraph::DataFlowEdge>(loopIncrTag);
for(auto const& dim : loopDims | std::views::filter([&](int dim) {
return !transformer.hasCoordinate(dim);
}))
{
transformer.setCoordinate(dim, expr);
}
}
void KernelGraph::setTransformerBySetCoordinate(CoordinateGraph::Transformer& transformer,
int setCoordinateOp)
{
auto connections = mapper.getConnections(setCoordinateOp);
if(not transformer.hasCoordinate(connections[0].coordinate))
{
auto setCoordinate
= control.get<ControlGraph::SetCoordinate>(setCoordinateOp).value();
transformer.setCoordinate(connections[0].coordinate, setCoordinate.value);
}
}
std::string KernelGraph::toDOT(bool drawMappings, std::string title) const
{
std::stringstream ss;
ss << "digraph {\n";
if(!title.empty())
{
ss << "labelloc=\"t\";" << std::endl;
ss << "label=\"" << title << "\";" << std::endl;
}
ss << coordinates.toDOT("coord", false);
ss << "subgraph clusterCF {";
ss << "label = \"Control Graph\";" << std::endl;
ss << control.toDOT("cntrl", false);
ss << "}" << std::endl;
if(drawMappings)
{
ss << mapper.toDOT("coord", "cntrl");
}
ss << "}" << std::endl;
return ss.str();
}
ConstraintStatus
KernelGraph::checkConstraints(const std::vector<GraphConstraint>& constraints) const
{
ConstraintStatus retval;
for(int i = 0; i < constraints.size(); i++)
{
auto check = constraints[i](*this);
if(!check.satisfied)
{
Log::warn("Constraint failed: {}", check.explanation);
}
retval.combine(check);
}
return retval;
}
void KernelGraph::initializeTransformersForCodeGen(
Expression::ExpressionTransducer transducer)
{
for(auto& p : m_transformers)
{
p.second.setCoordinateGraph(&coordinates);
p.second.setTransducer(transducer);
}
}
void KernelGraph::updateTransformer(int op, int coord, Expression::ExpressionPtr expr)
{
AssertFatal(m_transformers.contains(op), "Transformer does not exist");
m_transformers.at(op).setCoordinate(coord, expr);
}
void KernelGraph::buildAllTransformers()
{
for(auto const& node : control.getNodes())
buildTransformer(node, IgnoreCache);
}
rocRoller::KernelGraph::CoordinateGraph::Transformer KernelGraph::buildTransformer(int op)
{
if(not m_transformers.contains(op))
return buildTransformer(op, IgnoreCache);
return m_transformers.at(op);
}
rocRoller::KernelGraph::CoordinateGraph::Transformer
KernelGraph::buildTransformer(int op, IgnoreCachePolicy const)
{
auto [iter, _] = m_transformers.insert_or_assign(op, &coordinates);
auto const stk = controlStack(op, control);
for(int index : stk | std::views::reverse)
{
std::visit(
[&](auto&& node) {
using OpType = std::decay_t<decltype(node)>;
if constexpr(std::is_same_v<OpType, ControlGraph::SetCoordinate>)
{
setTransformerBySetCoordinate(iter->second, index);
}
else if constexpr(std::is_same_v<OpType, ControlGraph::ForLoopOp>)
{
setTransformerByForLoopOp(iter->second, index);
}
},
control.getNode(index));
}
return iter->second;
}
ConstraintStatus KernelGraph::checkConstraints() const
{
return checkConstraints(m_constraints);
}
void KernelGraph::addConstraints(const std::vector<GraphConstraint>& constraints)
{
m_constraints.insert(m_constraints.end(), constraints.begin(), constraints.end());
}
std::vector<GraphConstraint> KernelGraph::getConstraints() const
{
return m_constraints;
}
KernelGraph KernelGraph::transform(std::shared_ptr<GraphTransform> const& transformation)
{
auto transformString = concatenate("KernelGraph::transform ", transformation->name());
auto checkConstraints = Settings::getInstance()->get(Settings::EnforceGraphConstraints);
if(checkConstraints)
{
auto check = (*this).checkConstraints(transformation->preConstraints());
AssertFatal(check.satisfied,
concatenate(transformString, " PreCheck: \n", check.explanation));
}
KernelGraph newGraph = [&]() {
TIMER(t, "KernelGraph::Transformation::" + transformation->name());
return transformation->apply(*this);
}();
bool drawMappings = Settings::getInstance()->get(Settings::LogGraphMapperConnections);
if(Settings::getInstance()->get(Settings::LogGraphs))
Log::debug("KernelGraph::transform: {}, post: {}",
transformation->name(),
newGraph.toDOT(drawMappings, transformString));
if(checkConstraints)
{
newGraph.addConstraints(transformation->postConstraints());
auto check = newGraph.checkConstraints();
AssertFatal(check.satisfied,
concatenate(transformString, " PostCheck: \n", check.explanation));
}
newGraph.m_transforms.push_back(transformation->name());
return newGraph;
}
std::vector<std::string> const& KernelGraph::appliedTransforms() const
{
return m_transforms;
}
void KernelGraph::addAppliedTransforms(std::vector<std::string> const& transforms)
{
m_transforms.insert(m_transforms.end(), transforms.begin(), transforms.end());
}
}
}