Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DFCxx kernel instantiation #52

Merged
merged 7 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion examples/addconst/addconst.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class AddConst : public dfcxx::Kernel {
public:
std::string_view getName() override {
std::string_view getName() const override {
return "AddConst";
}

Expand Down
2 changes: 1 addition & 1 deletion examples/idct/idct.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static const int32_t kSIZE = kDIM * kDIM;

class IDCT : public dfcxx::Kernel {
public:
std::string_view getName() override {
std::string_view getName() const override {
return "IDCT";
}

Expand Down
2 changes: 1 addition & 1 deletion examples/matrixmul2/matrixmul2.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class MatrixMul2 : public dfcxx::Kernel {
public:
std::string_view getName() override {
std::string_view getName() const override {
return "MatrixMul2";
}

Expand Down
2 changes: 1 addition & 1 deletion examples/movingsum/movingsum.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class MovingSum : public dfcxx::Kernel {
public:
std::string_view getName() override {
std::string_view getName() const override {
return "MovingSum";
}

Expand Down
2 changes: 1 addition & 1 deletion examples/muxmul/muxmul.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class MuxMul : public dfcxx::Kernel {
public:
std::string_view getName() override {
std::string_view getName() const override {
return "MuxMul";
}

Expand Down
2 changes: 1 addition & 1 deletion examples/polynomial2/polynomial2.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class Polynomial2 : public dfcxx::Kernel {
public:
std::string_view getName() override {
std::string_view getName() const override {
return "Polynomial2";
}

Expand Down
4 changes: 4 additions & 0 deletions examples/polynomial2_inst/add_int_2_mul_int3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ADD_INT": 2,
"MUL_INT": 3
}
16 changes: 16 additions & 0 deletions examples/polynomial2_inst/polynomial2_inst.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//===----------------------------------------------------------------------===//
//
// Part of the Utopia HLS Project, under the Apache License v2.0
// SPDX-License-Identifier: Apache-2.0
// Copyright 2024 ISP RAS (http://www.ispras.ru)
//
//===----------------------------------------------------------------------===//

#include "polynomial2_inst.h"

#include <memory>

std::unique_ptr<dfcxx::Kernel> start() {
Polynomial2Inst *kernel = new Polynomial2Inst();
return std::unique_ptr<dfcxx::Kernel>(kernel);
}
55 changes: 55 additions & 0 deletions examples/polynomial2_inst/polynomial2_inst.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//===----------------------------------------------------------------------===//
//
// Part of the Utopia HLS Project, under the Apache License v2.0
// SPDX-License-Identifier: Apache-2.0
// Copyright 2024 ISP RAS (http://www.ispras.ru)
//
//===----------------------------------------------------------------------===//

#include "dfcxx/DFCXX.h"

class Polynomial2 : public dfcxx::Kernel {
public:
std::string_view getName() const override {
return "Polynomial2";
}

~Polynomial2() override = default;

Polynomial2() : dfcxx::Kernel() {
using dfcxx::DFType;
using dfcxx::DFVariable;

const DFType &type = dfUInt(32);
DFVariable x = io.input("x", type);
DFVariable squared = x * x;
DFVariable squaredPlusX = squared + x;
DFVariable result = squaredPlusX + x;
DFVariable out = io.output("out", type);
out.connect(result);
}
};

class Polynomial2Inst : public dfcxx::Kernel {
public:
std::string_view getName() const override {
return "Polynomial2Inst";
}

~Polynomial2Inst() override = default;

Polynomial2Inst() : dfcxx::Kernel() {
using dfcxx::DFType;
using dfcxx::DFVariable;

const DFType &type = dfUInt(32);
DFVariable x = io.input("x", type);
DFVariable intermediate = io.newStream(type);
instance<Polynomial2>({
{x, "x"},
{intermediate, "out"}
});
DFVariable out = io.output("out", type);
out.connect(intermediate);
}
};
5 changes: 5 additions & 0 deletions examples/polynomial2_inst/sim.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
x 0x32

x 0x45

x 0x56
2 changes: 1 addition & 1 deletion examples/scalar3/scalar3.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class Scalar3 : public dfcxx::Kernel {
public:
std::string_view getName() override {
std::string_view getName() const override {
return "Scalar3";
}

Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct SimContext {

int hlsMain(const HlsContext &context) {
auto kernel = start();
if (!kernel->check()) { return 1; }
bool useASAP = context.options.asapScheduler;
return !kernel->compile(context.options.latencyCfg,
context.options.outNames,
Expand Down
19 changes: 17 additions & 2 deletions src/model/dfcxx/include/dfcxx/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "dfcxx/node.h"
#include "dfcxx/vars/var.h"

#include <functional>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <vector>
Expand All @@ -21,6 +23,7 @@ namespace dfcxx {

class Graph {
private:
std::unordered_map<std::string_view, Node> nameMap;
std::unordered_set<Node> nodes;
std::unordered_set<Node> startNodes;
std::unordered_map<Node, std::vector<Channel>> inputs;
Expand All @@ -38,12 +41,24 @@ class Graph {

const std::unordered_map<Node, Channel> &getConnections() const;

Node findNode(DFVariableImpl *var);

void addNode(DFVariableImpl *var, OpType type, NodeData data);

void addChannel(DFVariableImpl *source, DFVariableImpl *target,
unsigned opInd, bool connect);

void transferFrom(Graph &&graph);

void resetNodeName(const std::string &name);

void deleteNode(Node node);

void rebindInput(Node source, Node input, Graph &graph);

Node rebindOutput(Node output, Node target, Graph &graph);

Node findNode(const std::string &name);

Node findNode(DFVariableImpl *var);
};

} // namespace dfcxx
Expand Down
4 changes: 4 additions & 0 deletions src/model/dfcxx/include/dfcxx/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class IO {

DFVariable inputScalar(const std::string &name, const DFType &type);

DFVariable newStream(const DFType &type);

DFVariable newScalar(const DFType &type);

DFVariable output(const std::string &name, const DFType &type);

DFVariable outputScalar(const std::string &name, const DFType &type);
Expand Down
34 changes: 33 additions & 1 deletion src/model/dfcxx/include/dfcxx/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
#include "dfcxx/types/type.h"
#include "dfcxx/vars/var.h"

#include <initializer_list>
#include <ostream>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

// This forward declaration is needed to avoid
Expand All @@ -37,6 +39,12 @@ class Kernel {

bool compileDot(llvm::raw_fd_ostream *stream);

void rebindInput(DFVariable source, Node input, Kernel &kern);

DFVariable rebindOutput(Node output, DFVariable target, Kernel &kern);

void deleteNode(Node node);

protected:
IO io;
Offset offset;
Expand All @@ -51,12 +59,31 @@ class Kernel {

DFType dfBool();

using IOBinding = std::pair<DFVariable&, std::string>;

template <typename Kern, typename... Args>
void instance(std::initializer_list<IOBinding> bindings, Args && ...args) {
Kern kern(std::forward<Args>(args)...);

for (auto &binding: bindings) {
Node node = kern.meta.graph.findNode(binding.second);
kern.meta.graph.resetNodeName(binding.second);
if (node.type == OpType::IN) {
rebindInput(binding.first, node, kern);
} else {
binding.first = rebindOutput(node, binding.first, kern);
}
}

meta.transferFrom(std::move(kern.meta));
}

Kernel();

public:
virtual ~Kernel() = default;

virtual std::string_view getName() = 0;
virtual std::string_view getName() const = 0;

const Graph &getGraph() const;

Expand All @@ -71,6 +98,11 @@ class Kernel {
bool simulate(const std::string &inDataPath,
const std::string &outFilePath);

bool check() const;

// Checker methods.
private:
bool checkValidNodes() const;
};

} // namespace dfcxx
Expand Down
2 changes: 2 additions & 0 deletions src/model/dfcxx/include/dfcxx/kernmeta.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ struct KernMeta {
KernMeta() = default;
KernMeta(const KernMeta &) = delete;
~KernMeta() = default;

void transferFrom(KernMeta &&meta);
};

} // namespace dfcxx
Expand Down
4 changes: 4 additions & 0 deletions src/model/dfcxx/include/dfcxx/kernstorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ class KernStorage {

DFVariableImpl *addVariable(DFVariableImpl *var);

void deleteVariable(DFVariableImpl *var);

~KernStorage();

void transferFrom(KernStorage &&storage);
};

} // namespace dfcxx
Expand Down
4 changes: 3 additions & 1 deletion src/model/dfcxx/include/dfcxx/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
namespace dfcxx {

enum OpType : uint8_t {
OFFSET = 0,
NONE = 0, // Is not allowed in a fully constructed kernel.
OFFSET,
IN,
OUT,
CONST,
Expand Down Expand Up @@ -53,6 +54,7 @@ struct Node {
Node(DFVariableImpl *var, OpType type, NodeData data);

bool operator==(const Node &node) const;
bool operator!=(const Node &node) const { return !(*this == node); }
};

} // namespace dfcxx
Expand Down
2 changes: 2 additions & 0 deletions src/model/dfcxx/include/dfcxx/vars/var.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class DFVariableImpl {

std::string_view getName() const;

void resetName();

IODirection getDirection() const;

const KernMeta &getMeta() const;
Expand Down
1 change: 1 addition & 0 deletions src/model/dfcxx/lib/dfcxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(SOURCES
${VAR_BUILDERS_SOURCES}
${TYPE_BUILDERS_SOURCES}
converter.cpp
kernmeta.cpp
kernstorage.cpp
io.cpp
offset.cpp
Expand Down
7 changes: 4 additions & 3 deletions src/model/dfcxx/lib/dfcxx/IRbuilders/builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,16 +275,17 @@ void DFCIRBuilder::translate(Node node, const Graph &graph,
}
default: {
// TODO: Add proper logging: https://github.com/ispras/utopia-hls/issues/13
std::cout << "[ERROR] Unknown node type id: " << node.type << std::endl;
std::cout << "[ERROR] Unknown/unsupported node type id: " << node.type << std::endl;
assert(false);
};
}

map[node] = newOp->getResult(0);

auto &connections = graph.getConnections();
if (connections.find(node) != connections.end()) {
auto conSrc = connections.at(node).source;
auto it = connections.find(node);
if (it != connections.end()) {
auto conSrc = it->second.source;
builder.create<mlir::dfcir::ConnectOp>(loc, map[node], map[conSrc]);
}
}
Expand Down
Loading