Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
d5c6b3f
construct dependenceDAG and update RemoveImplicitScheduling pass logic
pjots Feb 20, 2026
7782f17
Merge branch 'develop' of github.com:ROCm/rocm-libraries into users/p…
pjots Feb 20, 2026
5c3924f
Merge branch 'develop' into users/psandhu/BasicBlockDependenceDAGs
pjots Feb 23, 2026
38fe5be
apply Nathan's suggested patch -- more modular and reusable code
pjots Feb 23, 2026
4d30184
Merge branch 'develop' of github.com:ROCm/rocm-libraries into users/p…
pjots Feb 23, 2026
e21943f
order might be undefined in kernelgraph for two Multiply nodes with W…
pjots Feb 23, 2026
0dd47e2
Merge branch 'develop' of github.com:ROCm/rocm-libraries into users/p…
pjots Feb 23, 2026
b6df996
WIP
pjots Feb 26, 2026
36aeca0
Merge branch 'develop' of github.com:ROCm/rocm-libraries into users/p…
pjots Feb 26, 2026
f2b4cf1
Revert "WIP"
pjots Mar 2, 2026
9b0c590
Merge branch 'develop' of github.com:ROCm/rocm-libraries into users/p…
pjots Mar 2, 2026
13800bd
update test
pjots Mar 2, 2026
36a999e
fix formatting
pjots Mar 3, 2026
6b9dbd0
Merge branch 'develop' of github.com:ROCm/rocm-libraries into users/p…
pjots Mar 3, 2026
d741c3a
Merge branch 'develop' into users/psandhu/BasicBlockDependenceDAGs
pjots Mar 3, 2026
d40e06f
update code structure
pjots Mar 4, 2026
4f55c99
added licenses
pjots Mar 4, 2026
910bb44
Merge branch 'develop' of github.com:ROCm/rocm-libraries into users/p…
pjots Mar 4, 2026
22a735e
Merge branch 'users/psandhu/BasicBlockDependenceDAGs' of github.com:R…
pjots Mar 4, 2026
91d79fa
Merge branch 'develop' into users/psandhu/BasicBlockDependenceDAGs
pjots Mar 5, 2026
383333d
add a unit test
pjots Mar 6, 2026
78c9a9e
Merge branch 'develop' of github.com:ROCm/rocm-libraries into users/p…
pjots Mar 6, 2026
966c558
Merge branch 'users/psandhu/BasicBlockDependenceDAGs' of github.com:R…
pjots Mar 6, 2026
f872cdc
add a fun Nth fibonacci unit test
pjots Mar 7, 2026
d3f184d
Merge branch 'develop' of github.com:ROCm/rocm-libraries into users/p…
pjots Mar 7, 2026
f38c6f0
updated hoist unit test
pjots Mar 9, 2026
6e6902d
address Kerry's comments
pjots Mar 11, 2026
e789bb6
Merge branch 'develop' of github.com:ROCm/rocm-libraries into users/p…
pjots Mar 11, 2026
76bebb8
adds documentation
pjots Mar 11, 2026
76cea6b
Merge branch 'develop' of github.com:ROCm/rocm-libraries into users/p…
pjots Mar 11, 2026
ca52c33
Merge branch 'develop' into users/psandhu/BasicBlockDependenceDAGs
pjots Mar 12, 2026
35cdd49
WIP add unit tests
pjots Mar 12, 2026
7e9a01b
Merge branch 'users/psandhu/BasicBlockDependenceDAGs' of github.com:R…
pjots Mar 12, 2026
c1eaf26
added check for same basic block, unit testing WIP
pjots Mar 16, 2026
c80ba10
Merge branch 'develop' of github.com:ROCm/rocm-libraries into users/p…
pjots Mar 16, 2026
76f2905
return boolean for addDependenceEdge routine
pjots Mar 17, 2026
2542551
Merge branch 'develop' of github.com:ROCm/rocm-libraries into users/p…
pjots Mar 17, 2026
cbdb67a
remove belongToSameBasicBlock routine and add TODOs
pjots Mar 18, 2026
955a4dd
Merge branch 'develop' of github.com:ROCm/rocm-libraries into users/p…
pjots Mar 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT

#pragma once

#include <rocRoller/KernelGraph/ControlGraph/ControlGraph.hpp>

namespace rocRoller::KernelGraph::DataDependenceDAG
{
/**
* Builds the data dependence graph for the given kernel graph,
* specifically based on its control graph portion. It explicitly
* represents only the data dependences(flow, anti, and output) between
* the control graph nodes at the basic-block level.
*
* The data dependence graph has no cycles in it, therefore it
* is termed as data dependence DAG (directed acyclic graph).
* Since it involves multiple basic-blocks, the data dependence graph
* may not be a connected graph at the moment. In the future,
* we can make it as a connected graph by transforming it into a
* program dependence graph (which includes both data and control
* dependence information).
*
* It describes the dependency relationship between the control graph nodes,
* so it makes sense to use the existing `rocRoller::KernelGraph::ControlGraph`
* structure for its represenatation.
*
* The dependences are indicated using `Sequence` edges at each basic-block
* level i.e. between the nodes sharing the same immediate body-parent
* in `graph.control`.
*/
ControlGraph::ControlGraph ConstructDataDependenceDAG(KernelGraph const& graph);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT

#pragma once

#include <rocRoller/KernelGraph/ControlGraph/ControlFlowRWTracer.hpp>

namespace rocRoller::KernelGraph::DataDependenceDAG::Detail
{
class DataDependenceDAGDetail
{
public:
/**
* Initializes the data dependence DAG structure (`m_dependenceDAG`)
* with only the control nodes from the given kernel graph.
*/
DataDependenceDAGDetail(KernelGraph const& graph);

/**
* Returns the body-parent for the given node in the control graph.
*/
int getBodyParent(int control);
/**
* Adds a dependence edge(represented via `Sequence`) between the given
* source and destination nodes in the dependence DAG (`m_dependenceDAG),
* if both the nodes have the same body-parent.
*
* Returns true if a dependence edge is added or exists between the
* given source and destination nodes, otherwise returns false.
*/
bool addDependenceEdge(int source, int dest);
/**
* Makes necessary updates to `m_latestWriteToCoord`, `m_latestReadsToCoord`
* and `m_dependenceDAG` structures for the given `ReadWriteRecord`.
*/
void processReadWriteRecord(ControlFlowRWTracer::ReadWriteRecord const& record);
/**
* Builds the data dependence DAG by populating the `m_dependenceDAG` structure
* with required dependence edges based on the trace generated using `ControlFlowRWTracer`
* for `m_graph`.
*/
void constructDataDependenceDAG();
/**
* Returns the data dependence DAG (`m_dependenceDAG`).
*/
ControlGraph::ControlGraph getDataDependenceDAG();

private:
KernelGraph const& m_graph;
ControlGraph::ControlGraph m_dependenceDAG;
std::unordered_map<int, int> m_bodyParentCache;
std::map<int, int> m_latestWriteToCoord;
std::map<int, std::unordered_set<int>> m_latestReadsToCoord;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace rocRoller::KernelGraph
* The sub-graph is created by adding the given nodes to a new control graph, and then
* adding edges between the nodes based on the order of the nodes in the original
* control graph.
*
*
* rv.compare(cacheMode, a, b) should always return the same result as the original
* control graph as long as a and b are both in `nodes`.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace rocRoller::KernelGraph::NodeScheduling
{
auto theNodes = graph.control.getNodes().filter(pred);

// TODO: Use key as pair<nodeID, ControlEdge> to indicate the associated edge
// type with the bodyParent.
std::unordered_map<int, std::vector<int>> rv;
for(auto node : theNodes)
{
Expand Down Expand Up @@ -125,4 +127,4 @@ namespace rocRoller::KernelGraph::NodeScheduling
Log::debug("Desired order: \n{}", fmt::join(desiredOrder, "\n"));
Log::debug("Actual order: \n{}", fmt::join(nodes, "\n"));
}
}
}
42 changes: 5 additions & 37 deletions shared/rocroller/lib/source/KernelGraph/ControlFlowRWTracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,43 +394,8 @@ namespace rocRoller::KernelGraph

void ControlFlowRWTracer::operator()(ForLoopOp const& op, int tag)
{
//
// Don't examine for loop intialize or increment operations.
//
// Assign operations within loop initialisation operations
// are scoped already.
//
// Assign operations within loop increment operations
// typically involve: incrementing loop counters and
// offsets. Loop counters are scoped already.
//
// Offsets are created by AssignIndexExpressions and are
// used in other nodes like LoadTiled. These
// references do not explicitly appear in the graph.
//
// If we examine loop increment operations and "track" an
// offset increment, but don't track it during loads, then
// a Deallocate node would be mis-placed.
//
// A few solutions:
//
// 1. Don't examine loop increment operations. They
// already appear in Scopes so are deallocated regardless.
// Fairly easy but perhaps we miss an opporunity to free
// up registers early.
//
// 2. Teach the tracker how to dig into all nodes. Very
// tedious and not future-proof.
//
// 3. Expose all references in the graph. Ideal but we
// aren't there yet.
//

// auto init = m_graph.control.getOutputNodeIndices<Initialize>(tag).to<std::set>();
// generate(init);

auto incr = m_graph.control.getOutputNodeIndices<ForLoopIncrement>(tag).to<std::set>();
generate(incr);
auto init = m_graph.control.getOutputNodeIndices<Initialize>(tag).to<std::set>();
generate(init);

CollectDataFlowExpressionVisitor visitor;
visitor.call(op.condition);
Expand All @@ -441,6 +406,9 @@ namespace rocRoller::KernelGraph

auto body = m_graph.control.getOutputNodeIndices<Body>(tag).to<std::set>();
generate(body);

auto incr = m_graph.control.getOutputNodeIndices<ForLoopIncrement>(tag).to<std::set>();
generate(incr);
}

void ControlFlowRWTracer::operator()(Kernel const& op, int tag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
target_sources(rocroller
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/ControlGraph.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/DataDependenceDAG.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Operation.cpp"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// Copyright Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT

#include <rocRoller/KernelGraph/ControlGraph/DataDependenceDAG.hpp>
#include <rocRoller/KernelGraph/ControlGraph/DataDependenceDAG_detail.hpp>
#include <rocRoller/KernelGraph/Utils.hpp>

namespace rocRoller::KernelGraph
{
namespace DataDependenceDAG
{
using ReadWrite = ControlFlowRWTracer::ReadWrite;

ControlGraph::ControlGraph ConstructDataDependenceDAG(KernelGraph const& graph)
{
using namespace Detail;

DataDependenceDAGDetail obj(graph);
obj.constructDataDependenceDAG();
return obj.getDataDependenceDAG();
}

namespace Detail
{
DataDependenceDAGDetail::DataDependenceDAGDetail(KernelGraph const& graph)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it makes sense, the constructor could call constructDataDependenceDAG()

: m_graph(graph)
{
// Insert all control graph nodes into the data dependence DAG
for(auto node : m_graph.control.getNodes())
{
m_dependenceDAG.setElement(node, m_graph.control.getElement(node));
}
}

ControlGraph::ControlGraph DataDependenceDAGDetail::getDataDependenceDAG()
{
return m_dependenceDAG;
}

void DataDependenceDAGDetail::constructDataDependenceDAG()
{
auto tracer = ControlFlowRWTracer(m_graph);
auto records = tracer.coordinatesReadWrite();

// This assumes that the trace is ordered and records for the
// same control operation are consecutive.
std::unordered_set<int> seen;
for(auto iter = records.begin(); iter != records.end();)
{
auto currentControl = iter->control;

AssertFatal(seen.find(currentControl) == seen.end(),
"The records for the same control operation are not consecutive.",
ShowValue(currentControl));

for(; iter != records.end() && iter->control == currentControl; ++iter)
{
processReadWriteRecord(*iter);
}

seen.insert(currentControl);
}
}

int DataDependenceDAGDetail::getBodyParent(int control)
{
if(auto iter = m_bodyParentCache.find(control); iter != m_bodyParentCache.end())
return iter->second;

auto topSetCoordinate = getTopSetCoordinate(m_graph, control);
auto bodyParent = bodyParents(topSetCoordinate, m_graph).take(1).only();
AssertFatal(bodyParent.has_value(),
"Control node has no body parent",
ShowValue(control),
ShowValue(topSetCoordinate));

m_bodyParentCache.emplace(control, bodyParent.value());
return bodyParent.value();
}

bool DataDependenceDAGDetail::addDependenceEdge(int source, int dest)
{
AssertFatal(source != dest, ShowValue(source), ShowValue(dest));

if(getBodyParent(source) != getBodyParent(dest))
return false;

if(!m_dependenceDAG.findEdge(source, dest).has_value())
{
m_dependenceDAG.addElement(ControlGraph::Sequence(), {source}, {dest});
}

return true;
}

void DataDependenceDAGDetail::processReadWriteRecord(
ControlFlowRWTracer::ReadWriteRecord const& record)
{
AssertFatal(record.rw != ReadWrite::Count,
ShowValue(record.control),
ShowValue(record.coordinate),
ShowValue(record.rw));

if(auto writeIter = m_latestWriteToCoord.find(record.coordinate);
writeIter != m_latestWriteToCoord.end())
{
AssertFatal(writeIter->second != record.control,
ShowValue(writeIter->second),
ShowValue(record.control),
ShowValue(record.coordinate),
ShowValue(record.rw));

// adds WW(output dep) or WR(flow dep) edge
auto depAdded = addDependenceEdge(writeIter->second, record.control);

//TODO: Check the order and ensure that writeIter->second
// happens before record.control.
//if(depAdded)
//{
// auto order = m_graph.control.compareNodes(
// UseCacheIfAvailable, writeIter->second, record.control);
// AssertFatal(order == ControlGraph::NodeOrdering::LeftFirst
// || order == ControlGraph::NodeOrdering::RightInBodyOfLeft,
// ShowValue(order),
// ShowValue(writeIter->second),
// ShowValue(record.control),
// ShowValue(record.coordinate),
// ShowValue(record.rw));
//}
}

if(record.rw == ReadWrite::WRITE || record.rw == ReadWrite::READWRITE)
{
for(auto const readControl : m_latestReadsToCoord[record.coordinate])
{
if(readControl == record.control)
continue;

// adds RW(anti dep) edges
auto depAdded = addDependenceEdge(readControl, record.control);

//TODO: Check the order and ensure that writeIter->second
// happens before record.control.
//if(depAdded)
//{
// auto order = m_graph.control.compareNodes(
// UseCacheIfAvailable, readControl, record.control);
// AssertFatal(order == ControlGraph::NodeOrdering::LeftFirst
// || order
// == ControlGraph::NodeOrdering::RightInBodyOfLeft,
// ShowValue(order),
// ShowValue(readControl),
// ShowValue(record.control),
// ShowValue(record.coordinate),
// ShowValue(record.rw));
//}
}

// Since the current control node writes into this coord,
// the latest reads info needs to be reset.
m_latestReadsToCoord[record.coordinate].clear();
// update the latest write to coord
m_latestWriteToCoord[record.coordinate] = record.control;
}

if(record.rw == ReadWrite::READ || record.rw == ReadWrite::READWRITE)
{
m_latestReadsToCoord[record.coordinate].insert(record.control);
}
}
}
}
}
Loading
Loading