Files
TheRock/rocm-libraries/shared/rocroller/lib/source/KernelGraph/LastRWTracer.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

146 lines
4.7 KiB
C++

// Copyright Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT
/*
* When analysing when registers are modified, we construct a
* "read/write" tree, where leaves are operations that can modify
* registers and interior nodes are body-parents. The root of the
* tree is the kernel. For example, the tree corresponding to the
* pseudo-code:
*
* v[0:3] = 0 # Assign(2)
* for i in range(4): # ForLoopOp(3)
* v[i] = i # Assign(4)
*
* is:
*
* Kernel(1)
* / \
* Assign(2) ForLoopOp(3)
* \
* Assign(4)
*
* When we refer to a "control stack" in the LastRWTracer
* implementation below, we mean a simple vector representing the path
* from root of the tree to the leaf that modifies the register in
* question.
*
* In the example above, there are two stacks for the `v` register:
*
* 1. The stack for Assign(2) is: [1, 2]
* 2. The stack for Assign(4) is: [1, 3, 4]
*
* The common piece between those two stacks is simply [1], and hence
* the return value of `common` is 0.
*
* The return value of `lastRWLocations` is then: { `v`: [2, 3] }.
*
* At the time of writing, we want to also track register usage inside
* the condition expressions of ForLoopOps. These are bit tricky,
* because they aren't really leaves.
*/
#include <limits>
#include <variant>
#include <vector>
#include <rocRoller/KernelGraph/ControlGraph/LastRWTracer.hpp>
#include <rocRoller/KernelGraph/ControlGraph/ControlFlowArgumentTracer.hpp>
#include <rocRoller/KernelGraph/KernelGraph.hpp>
#include <rocRoller/KernelGraph/Utils.hpp>
#include <rocRoller/Utilities/Error.hpp>
namespace rocRoller::KernelGraph
{
int common(std::deque<int> const& a, std::deque<int> const& b)
{
for(int i = 0; (i < a.size()) && (i < b.size()); ++i)
if(a.at(i) != b.at(i))
return i - 1;
return std::min(a.size(), b.size()) - 1;
}
std::unordered_map<std::string, std::set<int>>
LastRWTracer::lastArgLocations(ControlFlowArgumentTracer const& argTracer) const
{
std::unordered_map<std::string, std::vector<std::deque<int>>> controlStacks;
for(auto const& [controlNode, args] : argTracer.referencedArguments())
{
auto stack = rocRoller::KernelGraph::controlStack(controlNode, m_graph);
for(auto const& arg : args)
{
controlStacks[arg].push_back(stack);
}
}
return getLastLocationsFromControlStacks(controlStacks);
}
std::unordered_map<int, std::set<int>> LastRWTracer::lastRWLocations() const
{
TIMER(t, "lastRWLocations");
// Precompute all stacks
std::unordered_map<int, std::vector<std::deque<int>>> controlStacksByCoord;
std::unordered_map<int, std::deque<int>> controlStacksByControl;
for(auto const& x : m_trace)
{
auto iter = controlStacksByControl.find(x.control);
if(iter == controlStacksByControl.end())
{
controlStacksByControl[x.control] = controlStack(x.control, m_graph);
iter = controlStacksByControl.find(x.control);
}
controlStacksByCoord[x.coordinate].push_back(iter->second);
}
return getLastLocationsFromControlStacks(controlStacksByCoord);
}
template <typename Key>
std::unordered_map<Key, std::set<int>> LastRWTracer::getLastLocationsFromControlStacks(
std::unordered_map<Key, std::vector<ControlStack>> const& controlStacks) const
{
std::unordered_map<Key, std::set<int>> rv;
for(auto const& [key, stacks] : controlStacks)
{
if(stacks.size() == 1)
{
rv[key].insert(stacks.back().back());
continue;
}
// Find common body-parent of all operations.
int c = std::numeric_limits<int>().max();
for(int i = 1; i < stacks.size(); ++i)
{
c = std::min(c, common(stacks[i - 1], stacks[i]));
}
// Extra pass to handle conditions in ForLoopOps; bump c
// so that ForLoopOps become leaf-like.
for(auto const& stack : stacks)
{
if(c + 1 >= stack.size())
c = std::max(0, c - 1);
}
for(auto const& stack : stacks)
{
AssertFatal(c + 1 < stack.size(),
"LastRWTracer::lastRWLocations: Stacks are identical");
rv[key].insert(stack.at(c + 1));
}
}
return rv;
}
}