Skip to content
Open
Show file tree
Hide file tree
Changes from 19 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,11 @@
// Copyright Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT

#pragma once

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

namespace rocRoller::KernelGraph::DataDependenceDAG
{
ControlGraph::ControlGraph ConstructDataDependenceDAG(KernelGraph const& graph);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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:
DataDependenceDAGDetail(KernelGraph const& graph);

int getBodyParent(int control);
void addDependenceEdge(int sourceControl, int destControl);
void processReadWriteRecord(ControlFlowRWTracer::ReadWriteRecord const& record);
void constructDataDependenceDAG();
ControlGraph::ControlGraph getDataDependenceDAG();
Copy link
Contributor

Choose a reason for hiding this comment

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

Brief documentation for these functions would also be good


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 All @@ -46,6 +46,15 @@ namespace rocRoller::KernelGraph
* `graph.control`.
*/
void orderNodes(KernelGraph const& graph, std::vector<int>& nodes, auto const& comp);

/**
* Builds the data dependence graph for the given kernel graph.
*
* The dependences are represented at each basic block level i.e. between the
Copy link
Contributor

Choose a reason for hiding this comment

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

Formatting

* nodes sharing the same immediate body-parent in `graph.control`.
* The flow, anti, and output data dependences between such nodes are included.
*/
ControlGraph::ControlGraph ConstructDataDependenceDAG(KernelGraph const& graph);
}
}

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,146 @@
// 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)
{
}

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

void DataDependenceDAGDetail::constructDataDependenceDAG()
{
// 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));
}

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();
}

void DataDependenceDAGDetail::addDependenceEdge(int sourceControl, int destControl)
{
AssertFatal(
sourceControl != destControl, ShowValue(sourceControl), ShowValue(destControl));

auto sourceBodyParent = getBodyParent(sourceControl);
auto destBodyParent = getBodyParent(destControl);

if(sourceBodyParent != destBodyParent)
return;

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

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) and WR(flow dep) edges
addDependenceEdge(writeIter->second, record.control);
}

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
addDependenceEdge(readControl, record.control);
}

// 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);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include <rocRoller/KernelGraph/Transforms/Simplify.hpp>
#include <rocRoller/KernelGraph/Utils.hpp>

#include <map>
#include <unordered_map>
#include <unordered_set>

namespace rocRoller::KernelGraph::NodeScheduling
{
ControlGraph::ControlGraph createSubGraph(KernelGraph const& graph,
Expand Down Expand Up @@ -51,5 +55,4 @@ namespace rocRoller::KernelGraph::NodeScheduling

return subGraph;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <rocRoller/KernelGraph/Transforms/RemoveImplicitScheduling.hpp>

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

#include <rocRoller/KernelGraph/Utils.hpp>

namespace rocRoller::KernelGraph
Expand All @@ -25,54 +25,6 @@ namespace rocRoller::KernelGraph
return std::nullopt;
};

auto loop = getLoopOp(nodes.front()).value_or(-1);

auto colouring = colourByUnrollValue(graph, -1);

Log::debug(toString(colouring));

using Colour = std::set<std::pair<int, int>>;

std::map<Colour, std::vector<int>> reverse;

for(auto node : nodes)
{
auto const& opColour = colouring.operationColour.at(node);

Colour key(opColour.begin(), opColour.end());

reverse[key].push_back(node);
}

for(auto& [key, keyOps] : reverse)
{
std::ranges::sort(keyOps, TopologicalCompare(graph));
}

std::set<int> edgesToKeep;

for(auto& [key, keyOps] : reverse)
{
for(int idx = 0; idx + 1 < keyOps.size(); idx++)
{
auto thisEdge = graph.control.findEdge(keyOps[idx], keyOps[idx + 1]);

if(!thisEdge)
{
AssertFatal(graph.control.compareNodes(
UseCacheIfAvailable, keyOps[idx], keyOps[idx + 1])
== ControlGraph::NodeOrdering::LeftFirst);

thisEdge = graph.control.addElement(
ControlGraph::Sequence(), {keyOps[idx]}, {keyOps[idx + 1]});
}

edgesToKeep.insert(*thisEdge);
}
}

Log::debug("Keeping edges ({})", fmt::join(edgesToKeep, ", "));

auto notMultiply = [&graph](int idx) {
if(graph.control.getElementType(idx) != Graph::ElementType::Node)
return false;
Expand All @@ -99,16 +51,26 @@ namespace rocRoller::KernelGraph

Log::debug("Got connections.");

auto dependenceDAG = DataDependenceDAG::ConstructDataDependenceDAG(graph);

for(auto nodeA : nodes)
{
for(auto nodeB : nodes)
{
if(nodeA == nodeB)
continue;

auto thisEdge = graph.control.findEdge(nodeA, nodeB);
auto depEdge = dependenceDAG.findEdge(nodeA, nodeB);
auto seqEdge = graph.control.findEdge(nodeA, nodeB);

if(thisEdge.has_value() && !edgesToKeep.contains(*thisEdge))
if(depEdge.has_value())
{
if(!seqEdge.has_value())
{
graph.control.addElement(ControlGraph::Sequence(), {nodeA}, {nodeB});
}
}
else if(seqEdge.has_value())
{
auto upstream = connectionsToKeep.at(nodeB);
auto order
Expand All @@ -118,8 +80,8 @@ namespace rocRoller::KernelGraph
ShowValue(order),
ShowValue(upstream),
ShowValue(nodeB),
ShowValue(*thisEdge));
graph.control.deleteElement(*thisEdge);
ShowValue(seqEdge.value()));
graph.control.deleteElement(seqEdge.value());
if(order == ControlGraph::NodeOrdering::LeftFirst)
graph.control.chain<ControlGraph::Sequence>(upstream, nodeB);
else
Expand All @@ -134,6 +96,7 @@ namespace rocRoller::KernelGraph
{
auto rv = original;

// grouped by immediate body-parent
auto groupedMultiplyNodes = NodeScheduling::getGroupedNodes<ControlGraph::Multiply>(rv);

for(auto& [parent, nodes] : groupedMultiplyNodes)
Expand Down
3 changes: 2 additions & 1 deletion shared/rocroller/lib/source/KernelGraph/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,8 @@ namespace rocRoller
else
break;

AssertFatal(graph.mapper.get<CT::Unroll>(tag) > 0,
AssertFatal(graph.mapper.get<CT::Unroll>(tag) > 0
|| graph.mapper.get<CT::ForLoop>(tag) > 0,
"SetCoordinate needs Unroll dimension");
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps update message to "SetCoordinate needs Unroll/ForLoop dimension"?

}
return tag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ namespace RemoveImplicitSchedulingTest
CHECK(order == graph.control.compareNodes(UpdateCache, node1, node2));
}
}
CHECK(newlyUnorderedPairs > 0);

if(dataTypeAB == DataType::FP4)
CHECK(newlyUnorderedPairs > 0);
}
}

Expand Down
Loading