Skip to content

Commit b7c1cdf

Browse files
committed
add new generic compute graph and rewrite c++ functional backend to use it
1 parent ea69326 commit b7c1cdf

File tree

7 files changed

+628
-302
lines changed

7 files changed

+628
-302
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ $(eval $(call add_include_file,backends/rtlil/rtlil_backend.h))
648648
OBJS += kernel/driver.o kernel/register.o kernel/rtlil.o kernel/log.o kernel/calc.o kernel/yosys.o
649649
OBJS += kernel/binding.o
650650
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
651-
OBJS += kernel/drivertools.o
651+
OBJS += kernel/drivertools.o kernel/functionalir.o
652652
ifeq ($(ENABLE_ZLIB),1)
653653
OBJS += kernel/fstdata.o
654654
endif

backends/functional/Makefile.inc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
OBJS += backends/functional/cxx.o
2-
OBJS += backends/functional/smtlib.o
2+
#OBJS += backends/functional/smtlib.o
3+
OBJS += backends/functional/test_generic.o

backends/functional/cxx.cc

+107-285
Large diffs are not rendered by default.

backends/functional/test_generic.cc

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* yosys -- Yosys Open SYnthesis Suite
3+
*
4+
* Copyright (C) 2024 Emily Schmidt <[email protected]>
5+
*
6+
* Permission to use, copy, modify, and/or distribute this software for any
7+
* purpose with or without fee is hereby granted, provided that the above
8+
* copyright notice and this permission notice appear in all copies.
9+
*
10+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17+
*
18+
*/
19+
20+
#include "kernel/yosys.h"
21+
#include "kernel/functionalir.h"
22+
23+
USING_YOSYS_NAMESPACE
24+
PRIVATE_NAMESPACE_BEGIN
25+
26+
struct FunctionalTestGeneric : public Pass
27+
{
28+
FunctionalTestGeneric() : Pass("test_generic", "test the generic compute graph") {}
29+
30+
void help() override
31+
{
32+
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
33+
log("\n");
34+
}
35+
36+
void execute(std::vector<std::string> args, RTLIL::Design *design) override
37+
{
38+
log_header(design, "Executing Test Generic.\n");
39+
40+
size_t argidx = 1;
41+
extra_args(args, argidx, design);
42+
43+
for (auto module : design->selected_modules()) {
44+
log("Dumping module `%s'.\n", module->name.c_str());
45+
auto fir = FunctionalIR::from_module(module);
46+
for(auto node : fir)
47+
std::cout << RTLIL::unescape_id(node.name()) << " = " << node.to_string([](auto n) { return RTLIL::unescape_id(n.name()); }) << "\n";
48+
for(auto [name, sort] : fir.outputs())
49+
std::cout << RTLIL::unescape_id(name) << " = " << RTLIL::unescape_id(fir.get_output_node(name).name()) << "\n";
50+
for(auto [name, sort] : fir.state())
51+
std::cout << RTLIL::unescape_id(name) << " = " << RTLIL::unescape_id(fir.get_state_next_node(name).name()) << "\n";
52+
}
53+
}
54+
} FunctionalCxxBackend;
55+
56+
PRIVATE_NAMESPACE_END

kernel/functional.h

+20-2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ struct ComputeGraph
123123
Node &deref() const { this->check(); return this->graph_->nodes[this->index_]; }
124124

125125
public:
126+
Ref(BaseRef<ComputeGraph> ref) : Ref(ref.graph_, ref.index_) {}
127+
126128
void set_function(Fn const &function) const
127129
{
128130
deref().fn_index = this->graph_->functions(function);
@@ -224,7 +226,7 @@ struct ComputeGraph
224226
}
225227

226228
template<typename T>
227-
Ref add(Fn const &function, Attr const &attr, T const &args)
229+
Ref add(Fn const &function, Attr const &attr, T &&args)
228230
{
229231
Ref added = add(function, attr);
230232
for (auto arg : args)
@@ -233,7 +235,23 @@ struct ComputeGraph
233235
}
234236

235237
template<typename T>
236-
Ref add(Fn const &function, Attr &&attr, T const &args)
238+
Ref add(Fn const &function, Attr &&attr, T &&args)
239+
{
240+
Ref added = add(function, std::move(attr));
241+
for (auto arg : args)
242+
added.append_arg(arg);
243+
return added;
244+
}
245+
246+
Ref add(Fn const &function, Attr const &attr, std::initializer_list<Ref> args)
247+
{
248+
Ref added = add(function, attr);
249+
for (auto arg : args)
250+
added.append_arg(arg);
251+
return added;
252+
}
253+
254+
Ref add(Fn const &function, Attr &&attr, std::initializer_list<Ref> args)
237255
{
238256
Ref added = add(function, std::move(attr));
239257
for (auto arg : args)

kernel/graphtools.h kernel/functionalir.cc

+61-13
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,8 @@
1717
*
1818
*/
1919

20-
#ifndef GRAPHTOOLS_H
21-
#define GRAPHTOOLS_H
20+
#include "kernel/functionalir.h"
2221

23-
#include "kernel/yosys.h"
24-
#include "kernel/drivertools.h"
25-
#include "kernel/functional.h"
26-
#include "kernel/mem.h"
27-
28-
USING_YOSYS_NAMESPACE
2922
YOSYS_NAMESPACE_BEGIN
3023

3124
template <class T, class Factory>
@@ -196,7 +189,7 @@ class CellSimplifier {
196189
};
197190

198191
template <class T, class Factory>
199-
class ComputeGraphConstruction {
192+
class FunctionalIRConstruction {
200193
std::deque<DriveSpec> queue;
201194
dict<DriveSpec, T> graph_nodes;
202195
idict<Cell *> cells;
@@ -218,7 +211,7 @@ class ComputeGraphConstruction {
218211
return it->second;
219212
}
220213
public:
221-
ComputeGraphConstruction(Factory &f) : factory(f), simplifier(f) {}
214+
FunctionalIRConstruction(Factory &f) : factory(f), simplifier(f) {}
222215
void add_module(Module *module)
223216
{
224217
driver_map.add(module);
@@ -238,8 +231,9 @@ class ComputeGraphConstruction {
238231
memories[mem.cell] = &mem;
239232
}
240233
}
241-
T concatenate_read_results(Mem *mem, vector<T> results)
234+
T concatenate_read_results(Mem *, vector<T> results)
242235
{
236+
/* TODO: write code to check that this is ok to do */
243237
if(results.size() == 0)
244238
return factory.undriven(0);
245239
T node = results[0];
@@ -381,6 +375,60 @@ class ComputeGraphConstruction {
381375
}
382376
};
383377

384-
YOSYS_NAMESPACE_END
378+
FunctionalIR FunctionalIR::from_module(Module *module) {
379+
FunctionalIR ir;
380+
auto factory = ir.factory();
381+
FunctionalIRConstruction<FunctionalIR::Node, FunctionalIR::Factory> ctor(factory);
382+
ctor.add_module(module);
383+
ctor.process_queue();
384+
ir.topological_sort();
385+
ir.forward_buf();
386+
return ir;
387+
}
388+
389+
void FunctionalIR::topological_sort() {
390+
Graph::SccAdaptor compute_graph_scc(_graph);
391+
bool scc = false;
392+
std::vector<int> perm;
393+
topo_sorted_sccs(compute_graph_scc, [&](int *begin, int *end) {
394+
perm.insert(perm.end(), begin, end);
395+
if (end > begin + 1)
396+
{
397+
log_warning("SCC:");
398+
for (int *i = begin; i != end; ++i)
399+
log(" %d", *i);
400+
log("\n");
401+
scc = true;
402+
}
403+
}, /* sources_first */ true);
404+
_graph.permute(perm);
405+
if(scc) log_error("combinational loops, aborting\n");
406+
}
385407

386-
#endif
408+
void FunctionalIR::forward_buf() {
409+
std::vector<int> perm, alias;
410+
perm.clear();
411+
412+
for (int i = 0; i < _graph.size(); ++i)
413+
{
414+
auto node = _graph[i];
415+
if (node.function().fn() == Fn::buf && node.arg(0).index() < i)
416+
{
417+
int target_index = alias[node.arg(0).index()];
418+
auto target_node = _graph[perm[target_index]];
419+
if(!target_node.has_sparse_attr() && node.has_sparse_attr()){
420+
IdString id = node.sparse_attr();
421+
target_node.sparse_attr() = id;
422+
}
423+
alias.push_back(target_index);
424+
}
425+
else
426+
{
427+
alias.push_back(GetSize(perm));
428+
perm.push_back(i);
429+
}
430+
}
431+
_graph.permute(perm, alias);
432+
}
433+
434+
YOSYS_NAMESPACE_END

0 commit comments

Comments
 (0)