Skip to content

Commit

Permalink
Replace optimization interface with Transformation interface (#760)
Browse files Browse the repository at this point in the history
This PR replaces the optimization interface with the Transformation
interface. Specifically, this PR does the following:

1. Removes the optimization interface in the llvm sublibrary
2. Adds the Transformation interface to the rvsdg sublibrary.
3. Replaces all usages of the optimization interface with the
Transformation interface

Ultimately, this lifts the concept of an RVSDG transformation to the
rvsdg library, enabling other sublibraries, such as the HLS library, to
tap into it in the future and set up a coherent transformation pipeline.

Moreover, this will enable to lift common transformations, such as dead
node elimination, to the RVSDG library level. Ultimately, this will help
to remove code duplication that is currently present in the code base,
i.e., DNE clones in the LLVM and HLS sublibraries.
  • Loading branch information
phate authored Jan 18, 2025
1 parent 86f1970 commit 7c3c119
Show file tree
Hide file tree
Showing 63 changed files with 394 additions and 406 deletions.
30 changes: 15 additions & 15 deletions jlm/hls/backend/rvsdg2rhls/rvsdg2rhls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ split_opt(llvm::RvsdgModule & rm)
jlm::llvm::tginversion tgi;
jlm::llvm::NodeReduction red;
jlm::util::StatisticsCollector statisticsCollector;
tgi.run(rm, statisticsCollector);
dne.run(rm, statisticsCollector);
cne.run(rm, statisticsCollector);
ivr.run(rm, statisticsCollector);
red.run(rm, statisticsCollector);
dne.run(rm, statisticsCollector);
tgi.Run(rm, statisticsCollector);
dne.Run(rm, statisticsCollector);
cne.Run(rm, statisticsCollector);
ivr.Run(rm, statisticsCollector);
red.Run(rm, statisticsCollector);
dne.Run(rm, statisticsCollector);
}

void
Expand All @@ -79,13 +79,13 @@ pre_opt(jlm::llvm::RvsdgModule & rm)
jlm::llvm::InvariantValueRedirection ivr;
jlm::llvm::tginversion tgi;
jlm::util::StatisticsCollector statisticsCollector;
tgi.run(rm, statisticsCollector);
dne.run(rm, statisticsCollector);
cne.run(rm, statisticsCollector);
ivr.run(rm, statisticsCollector);
dne.run(rm, statisticsCollector);
cne.run(rm, statisticsCollector);
dne.run(rm, statisticsCollector);
tgi.Run(rm, statisticsCollector);
dne.Run(rm, statisticsCollector);
cne.Run(rm, statisticsCollector);
ivr.Run(rm, statisticsCollector);
dne.Run(rm, statisticsCollector);
cne.Run(rm, statisticsCollector);
dne.Run(rm, statisticsCollector);
}

void
Expand Down Expand Up @@ -421,7 +421,7 @@ rvsdg2rhls(llvm::RvsdgModule & rhls, util::StatisticsCollector & collector)
merge_gamma(rhls);

llvm::DeadNodeElimination llvmDne;
llvmDne.run(rhls, collector);
llvmDne.Run(rhls, collector);

mem_sep_argument(rhls);
remove_unused_state(rhls);
Expand All @@ -430,7 +430,7 @@ rvsdg2rhls(llvm::RvsdgModule & rhls, util::StatisticsCollector & collector)
ConvertGammaNodes(rhls);
ConvertThetaNodes(rhls);
hls::cne hlsCne;
hlsCne.run(rhls, collector);
hlsCne.Run(rhls, collector);
// rhls optimization
dne(rhls);
alloca_conv(rhls);
Expand Down
8 changes: 4 additions & 4 deletions jlm/hls/opt/cne.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,12 +615,12 @@ divert(rvsdg::Region * region, cnectx & ctx)
}

static void
cne(jlm::llvm::RvsdgModule & rm, util::StatisticsCollector & statisticsCollector)
cne(rvsdg::RvsdgModule & rvsdgModule, util::StatisticsCollector & statisticsCollector)
{
auto & graph = rm.Rvsdg();
auto & graph = rvsdgModule.Rvsdg();

cnectx ctx;
auto statistics = cnestat::Create(rm.SourceFileName());
auto statistics = cnestat::Create(rvsdgModule.SourceFilePath().value());

statistics->start_mark_stat(graph);
mark(&graph.GetRootRegion(), ctx);
Expand All @@ -639,7 +639,7 @@ cne::~cne()
{}

void
cne::run(llvm::RvsdgModule & module, util::StatisticsCollector & statisticsCollector)
cne::Run(rvsdg::RvsdgModule & module, util::StatisticsCollector & statisticsCollector)
{
hls::cne(module, statisticsCollector);
}
Expand Down
13 changes: 4 additions & 9 deletions jlm/hls/opt/cne.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
#ifndef JLM_HLS_OPT_CNE_HPP
#define JLM_HLS_OPT_CNE_HPP

#include <jlm/llvm/opt/optimization.hpp>

namespace jlm::llvm
{
class RvsdgModule;
}
#include <jlm/rvsdg/Transformation.hpp>

namespace jlm::hls
{
Expand All @@ -25,13 +20,13 @@ namespace jlm::hls
* This is mainly a copy of the CNE optimization in the LLVM backend with the addition of support
* for the hls::loop_op.
*/
class cne final : public llvm::optimization
class cne final : public rvsdg::Transformation
{
public:
virtual ~cne();

virtual void
run(llvm::RvsdgModule & module, jlm::util::StatisticsCollector & statisticsCollector) override;
void
Run(rvsdg::RvsdgModule & module, util::StatisticsCollector & statisticsCollector) override;
};

}
Expand Down
4 changes: 0 additions & 4 deletions jlm/llvm/Makefile.sub
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ libllvm_SOURCES = \
jlm/llvm/opt/inlining.cpp \
jlm/llvm/opt/InvariantValueRedirection.cpp \
jlm/llvm/opt/inversion.cpp \
jlm/llvm/opt/optimization.cpp \
jlm/llvm/opt/OptimizationSequence.cpp \
jlm/llvm/opt/pull.cpp \
jlm/llvm/opt/push.cpp \
jlm/llvm/opt/reduction.cpp \
Expand Down Expand Up @@ -91,11 +89,9 @@ libllvm_HEADERS = \
jlm/llvm/opt/alias-analyses/PointsToGraph.hpp \
jlm/llvm/opt/alias-analyses/AliasAnalysis.hpp \
jlm/llvm/opt/pull.hpp \
jlm/llvm/opt/optimization.hpp \
jlm/llvm/opt/reduction.hpp \
jlm/llvm/opt/InvariantValueRedirection.hpp \
jlm/llvm/opt/inversion.hpp \
jlm/llvm/opt/OptimizationSequence.hpp \
jlm/llvm/opt/RvsdgTreePrinter.hpp \
jlm/llvm/frontend/LlvmModuleConversion.hpp \
jlm/llvm/frontend/LlvmTypeConversion.hpp \
Expand Down
6 changes: 4 additions & 2 deletions jlm/llvm/opt/DeadNodeElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,14 @@ DeadNodeElimination::run(rvsdg::Region & region)
}

void
DeadNodeElimination::run(RvsdgModule & module, jlm::util::StatisticsCollector & statisticsCollector)
DeadNodeElimination::Run(
rvsdg::RvsdgModule & module,
util::StatisticsCollector & statisticsCollector)
{
Context_ = Context::Create();

auto & rvsdg = module.Rvsdg();
auto statistics = Statistics::Create(module.SourceFileName());
auto statistics = Statistics::Create(module.SourceFilePath().value());
statistics->StartMarkStatistics(rvsdg);
MarkRegion(rvsdg.GetRootRegion());
statistics->StopMarkStatistics();
Expand Down
14 changes: 7 additions & 7 deletions jlm/llvm/opt/DeadNodeElimination.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
#ifndef JLM_LLVM_OPT_DEADNODEELIMINATION_HPP
#define JLM_LLVM_OPT_DEADNODEELIMINATION_HPP

#include <jlm/llvm/opt/optimization.hpp>
#include <jlm/rvsdg/simple-node.hpp>
#include <jlm/rvsdg/structural-node.hpp>
#include <jlm/rvsdg/Transformation.hpp>

namespace jlm::rvsdg
{
class GammaNode;
class Graph;
class output;
class StructuralNode;
class ThetaNode;
class Region;
}

namespace jlm::llvm
Expand All @@ -34,8 +36,6 @@ namespace phi
class node;
}

class RvsdgModule;

/** \brief Dead Node Elimination Optimization
*
* Dead Node Elimination removes all nodes that do not contribute to the result of a computation. A
Expand All @@ -51,7 +51,7 @@ class RvsdgModule;
*
* Please see TestDeadNodeElimination.cpp for Dead Node Elimination examples.
*/
class DeadNodeElimination final : public optimization
class DeadNodeElimination final : public rvsdg::Transformation
{
class Context;
class Statistics;
Expand All @@ -75,7 +75,7 @@ class DeadNodeElimination final : public optimization
run(rvsdg::Region & region);

void
run(RvsdgModule & module, jlm::util::StatisticsCollector & statisticsCollector) override;
Run(rvsdg::RvsdgModule & module, util::StatisticsCollector & statisticsCollector) override;

private:
void
Expand Down
8 changes: 4 additions & 4 deletions jlm/llvm/opt/InvariantValueRedirection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ class InvariantValueRedirection::Statistics final : public util::Statistics
InvariantValueRedirection::~InvariantValueRedirection() = default;

void
InvariantValueRedirection::run(
RvsdgModule & rvsdgModule,
InvariantValueRedirection::Run(
rvsdg::RvsdgModule & module,
util::StatisticsCollector & statisticsCollector)
{
auto statistics = Statistics::Create(rvsdgModule.SourceFileName());
auto statistics = Statistics::Create(module.SourceFilePath().value());

statistics->Start();
RedirectInRootRegion(rvsdgModule.Rvsdg());
RedirectInRootRegion(module.Rvsdg());
statistics->Stop();

statisticsCollector.CollectDemandedStatistics(std::move(statistics));
Expand Down
7 changes: 3 additions & 4 deletions jlm/llvm/opt/InvariantValueRedirection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef JLM_LLVM_OPT_INVARIANTVALUEREDIRECTION_HPP
#define JLM_LLVM_OPT_INVARIANTVALUEREDIRECTION_HPP

#include <jlm/llvm/opt/optimization.hpp>
#include <jlm/rvsdg/Transformation.hpp>

namespace jlm::rvsdg
{
Expand All @@ -21,7 +21,6 @@ namespace jlm::llvm
{

class CallNode;
class RvsdgModule;

/** \brief Invariant Value Redirection Optimization
*
Expand Down Expand Up @@ -50,15 +49,15 @@ class RvsdgModule;
* for call nodes works only on non-recursive direct calls as IVR needs to inspect the lambda body
* in order to determine whether a value is simply routed through the lambda.
*/
class InvariantValueRedirection final : public optimization
class InvariantValueRedirection final : public rvsdg::Transformation
{
class Statistics;

public:
~InvariantValueRedirection() override;

void
run(RvsdgModule & rvsdgModule, util::StatisticsCollector & statisticsCollector) override;
Run(rvsdg::RvsdgModule & module, util::StatisticsCollector & statisticsCollector) override;

private:
static void
Expand Down
63 changes: 0 additions & 63 deletions jlm/llvm/opt/OptimizationSequence.cpp

This file was deleted.

47 changes: 0 additions & 47 deletions jlm/llvm/opt/OptimizationSequence.hpp

This file was deleted.

6 changes: 4 additions & 2 deletions jlm/llvm/opt/RvsdgTreePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ class RvsdgTreePrinter::Statistics final : public util::Statistics
RvsdgTreePrinter::~RvsdgTreePrinter() noexcept = default;

void
RvsdgTreePrinter::run(RvsdgModule & rvsdgModule, util::StatisticsCollector & statisticsCollector)
RvsdgTreePrinter::Run(
rvsdg::RvsdgModule & rvsdgModule,
util::StatisticsCollector & statisticsCollector)
{
auto statistics = Statistics::Create(rvsdgModule.SourceFileName());
auto statistics = Statistics::Create(rvsdgModule.SourceFilePath().value());
statistics->Start();

auto annotationMap = ComputeAnnotationMap(rvsdgModule.Rvsdg());
Expand Down
Loading

0 comments on commit 7c3c119

Please sign in to comment.