Skip to content

Commit

Permalink
add new generic compute graph and rewrite c++ functional backend to u…
Browse files Browse the repository at this point in the history
…se it
  • Loading branch information
aiju committed Jun 20, 2024
1 parent ea69326 commit b7c1cdf
Show file tree
Hide file tree
Showing 7 changed files with 628 additions and 302 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ $(eval $(call add_include_file,backends/rtlil/rtlil_backend.h))
OBJS += kernel/driver.o kernel/register.o kernel/rtlil.o kernel/log.o kernel/calc.o kernel/yosys.o
OBJS += kernel/binding.o
OBJS += kernel/cellaigs.o kernel/celledges.o kernel/satgen.o kernel/scopeinfo.o kernel/qcsat.o kernel/mem.o kernel/ffmerge.o kernel/ff.o kernel/yw.o kernel/json.o kernel/fmt.o
OBJS += kernel/drivertools.o
OBJS += kernel/drivertools.o kernel/functionalir.o
ifeq ($(ENABLE_ZLIB),1)
OBJS += kernel/fstdata.o
endif
Expand Down
3 changes: 2 additions & 1 deletion backends/functional/Makefile.inc
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
OBJS += backends/functional/cxx.o
OBJS += backends/functional/smtlib.o
#OBJS += backends/functional/smtlib.o
OBJS += backends/functional/test_generic.o
392 changes: 107 additions & 285 deletions backends/functional/cxx.cc

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions backends/functional/test_generic.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* yosys -- Yosys Open SYnthesis Suite
*
* Copyright (C) 2024 Emily Schmidt <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/

#include "kernel/yosys.h"
#include "kernel/functionalir.h"

USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN

struct FunctionalTestGeneric : public Pass
{
FunctionalTestGeneric() : Pass("test_generic", "test the generic compute graph") {}

void help() override
{
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
log("\n");
}

void execute(std::vector<std::string> args, RTLIL::Design *design) override
{
log_header(design, "Executing Test Generic.\n");

size_t argidx = 1;
extra_args(args, argidx, design);

for (auto module : design->selected_modules()) {
log("Dumping module `%s'.\n", module->name.c_str());
auto fir = FunctionalIR::from_module(module);
for(auto node : fir)
std::cout << RTLIL::unescape_id(node.name()) << " = " << node.to_string([](auto n) { return RTLIL::unescape_id(n.name()); }) << "\n";
for(auto [name, sort] : fir.outputs())
std::cout << RTLIL::unescape_id(name) << " = " << RTLIL::unescape_id(fir.get_output_node(name).name()) << "\n";
for(auto [name, sort] : fir.state())
std::cout << RTLIL::unescape_id(name) << " = " << RTLIL::unescape_id(fir.get_state_next_node(name).name()) << "\n";
}
}
} FunctionalCxxBackend;

PRIVATE_NAMESPACE_END
22 changes: 20 additions & 2 deletions kernel/functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ struct ComputeGraph
Node &deref() const { this->check(); return this->graph_->nodes[this->index_]; }

public:
Ref(BaseRef<ComputeGraph> ref) : Ref(ref.graph_, ref.index_) {}

void set_function(Fn const &function) const
{
deref().fn_index = this->graph_->functions(function);
Expand Down Expand Up @@ -224,7 +226,7 @@ struct ComputeGraph
}

template<typename T>
Ref add(Fn const &function, Attr const &attr, T const &args)
Ref add(Fn const &function, Attr const &attr, T &&args)
{
Ref added = add(function, attr);
for (auto arg : args)
Expand All @@ -233,7 +235,23 @@ struct ComputeGraph
}

template<typename T>
Ref add(Fn const &function, Attr &&attr, T const &args)
Ref add(Fn const &function, Attr &&attr, T &&args)
{
Ref added = add(function, std::move(attr));
for (auto arg : args)
added.append_arg(arg);
return added;
}

Ref add(Fn const &function, Attr const &attr, std::initializer_list<Ref> args)
{
Ref added = add(function, attr);
for (auto arg : args)
added.append_arg(arg);
return added;
}

Ref add(Fn const &function, Attr &&attr, std::initializer_list<Ref> args)
{
Ref added = add(function, std::move(attr));
for (auto arg : args)
Expand Down
74 changes: 61 additions & 13 deletions kernel/graphtools.h → kernel/functionalir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,8 @@
*
*/

#ifndef GRAPHTOOLS_H
#define GRAPHTOOLS_H
#include "kernel/functionalir.h"

#include "kernel/yosys.h"
#include "kernel/drivertools.h"
#include "kernel/functional.h"
#include "kernel/mem.h"

USING_YOSYS_NAMESPACE
YOSYS_NAMESPACE_BEGIN

template <class T, class Factory>
Expand Down Expand Up @@ -196,7 +189,7 @@ class CellSimplifier {
};

template <class T, class Factory>
class ComputeGraphConstruction {
class FunctionalIRConstruction {
std::deque<DriveSpec> queue;
dict<DriveSpec, T> graph_nodes;
idict<Cell *> cells;
Expand All @@ -218,7 +211,7 @@ class ComputeGraphConstruction {
return it->second;
}
public:
ComputeGraphConstruction(Factory &f) : factory(f), simplifier(f) {}
FunctionalIRConstruction(Factory &f) : factory(f), simplifier(f) {}
void add_module(Module *module)
{
driver_map.add(module);
Expand All @@ -238,8 +231,9 @@ class ComputeGraphConstruction {
memories[mem.cell] = &mem;
}
}
T concatenate_read_results(Mem *mem, vector<T> results)
T concatenate_read_results(Mem *, vector<T> results)
{
/* TODO: write code to check that this is ok to do */
if(results.size() == 0)
return factory.undriven(0);
T node = results[0];
Expand Down Expand Up @@ -381,6 +375,60 @@ class ComputeGraphConstruction {
}
};

YOSYS_NAMESPACE_END
FunctionalIR FunctionalIR::from_module(Module *module) {
FunctionalIR ir;
auto factory = ir.factory();
FunctionalIRConstruction<FunctionalIR::Node, FunctionalIR::Factory> ctor(factory);
ctor.add_module(module);
ctor.process_queue();
ir.topological_sort();
ir.forward_buf();
return ir;
}

void FunctionalIR::topological_sort() {
Graph::SccAdaptor compute_graph_scc(_graph);
bool scc = false;
std::vector<int> perm;
topo_sorted_sccs(compute_graph_scc, [&](int *begin, int *end) {
perm.insert(perm.end(), begin, end);
if (end > begin + 1)
{
log_warning("SCC:");
for (int *i = begin; i != end; ++i)
log(" %d", *i);
log("\n");
scc = true;
}
}, /* sources_first */ true);
_graph.permute(perm);
if(scc) log_error("combinational loops, aborting\n");
}

#endif
void FunctionalIR::forward_buf() {
std::vector<int> perm, alias;
perm.clear();

for (int i = 0; i < _graph.size(); ++i)
{
auto node = _graph[i];
if (node.function().fn() == Fn::buf && node.arg(0).index() < i)
{
int target_index = alias[node.arg(0).index()];
auto target_node = _graph[perm[target_index]];
if(!target_node.has_sparse_attr() && node.has_sparse_attr()){
IdString id = node.sparse_attr();
target_node.sparse_attr() = id;
}
alias.push_back(target_index);
}
else
{
alias.push_back(GetSize(perm));
perm.push_back(i);
}
}
_graph.permute(perm, alias);
}

YOSYS_NAMESPACE_END
Loading

0 comments on commit b7c1cdf

Please sign in to comment.