From c89f38c813544c3890fa4b3be23458f88b87e33e Mon Sep 17 00:00:00 2001 From: Roland Coeurjoly Date: Tue, 21 May 2024 12:46:44 +0200 Subject: [PATCH] WIP --- kernel/drivertools.h | 2 + lol.json | 1 + passes/cmds/Makefile.inc | 1 + passes/cmds/example_dt.cc | 22 ++++-- passes/cmds/netlist_from_compute_graph.cc | 86 +++++++++++++++++++++++ result | 1 + 6 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 lol.json create mode 100644 passes/cmds/netlist_from_compute_graph.cc create mode 120000 result diff --git a/kernel/drivertools.h b/kernel/drivertools.h index 1cb835df2cc..065431fa491 100644 --- a/kernel/drivertools.h +++ b/kernel/drivertools.h @@ -1025,6 +1025,8 @@ struct DriveChunk return marker_.size(); case DriveType::MULTIPLE: return multiple_.size(); + default: + log_assert(false && "unsupported type"); } } }; diff --git a/lol.json b/lol.json new file mode 100644 index 00000000000..4e3ea0b0eb3 --- /dev/null +++ b/lol.json @@ -0,0 +1 @@ +{"nodes": [{"connections": [], "id": "0", "parameters": {}, "type": "$$input"}, {"connections": [], "id": "1", "parameters": {}, "type": "$$input"}, {"connections": ["0", "1"], "id": "2", "parameters": {}, "type": "$add"}, {"connections": ["2"], "id": "3", "parameters": {}, "type": "$$cell_output"}, {"connections": ["3"], "id": "4", "parameters": {}, "type": "$$buf"}], "outputs": []} diff --git a/passes/cmds/Makefile.inc b/passes/cmds/Makefile.inc index caea266c397..3d4b4798367 100644 --- a/passes/cmds/Makefile.inc +++ b/passes/cmds/Makefile.inc @@ -49,3 +49,4 @@ OBJS += passes/cmds/xprop.o OBJS += passes/cmds/dft_tag.o OBJS += passes/cmds/future.o OBJS += passes/cmds/example_dt.o +OBJS += passes/cmds/netlist_from_compute_graph.o diff --git a/passes/cmds/example_dt.cc b/passes/cmds/example_dt.cc index fe3a703f722..64d6a68425f 100644 --- a/passes/cmds/example_dt.cc +++ b/passes/cmds/example_dt.cc @@ -46,7 +46,7 @@ struct ExampleDtPass : public Pass log("\n"); } - std::string generate_json(const ComputeGraph& compute_graph) { + void generate_json(const ComputeGraph& compute_graph, const std::string& filename) { Json::array json_nodes; for (int i = 0; i < compute_graph.size(); ++i) { @@ -83,14 +83,23 @@ struct ExampleDtPass : public Pass {"nodes", json_nodes}, {"outputs", json_outputs} }; + std::ofstream outfile(filename); + outfile << json.dump() << std::endl; - return json.dump(); + // return json.dump(); } - - + void execute(std::vector args, RTLIL::Design *design) override - { - size_t argidx = 1; + { + size_t argidx; + std::string filename; + for (argidx = 1; argidx < args.size(); argidx++) + { + if (args[argidx] == "-o" && argidx+1 < args.size()) { + filename = args[++argidx]; + continue; + } + } extra_args(args, argidx, design); for (auto module : design->selected_modules()) { @@ -288,6 +297,7 @@ struct ExampleDtPass : public Pass log("return %d as %s \n", key.second, log_id(key.first)); } + generate_json(compute_graph, filename); } log("Plugin test passed!\n"); } diff --git a/passes/cmds/netlist_from_compute_graph.cc b/passes/cmds/netlist_from_compute_graph.cc new file mode 100644 index 00000000000..b63ae30cbb6 --- /dev/null +++ b/passes/cmds/netlist_from_compute_graph.cc @@ -0,0 +1,86 @@ +#include "kernel/yosys.h" +#include "kernel/drivertools.h" +#include "kernel/topo_scc.h" +#include "kernel/functional.h" +#include "libs/json11/json11.hpp" + +USING_YOSYS_NAMESPACE +PRIVATE_NAMESPACE_BEGIN + +using namespace json11; + +struct ExampleFn { + IdString name; + dict parameters; + + ExampleFn(IdString name) : name(name) {} + ExampleFn(IdString name, dict parameters) : name(name), parameters(parameters) {} + + bool operator==(ExampleFn const &other) const { + return name == other.name && parameters == other.parameters; + } + + unsigned int hash() const { + return mkhash(name.hash(), parameters.hash()); + } +}; + +typedef ComputeGraph ExampleGraph; +struct ExampleWorker +{ + DriverMap dm; + Module *module; + + ExampleWorker(Module *module) : module(module) { + dm.celltypes.setup(); + } +}; + +struct NetlistFromComputeGraphPass : public Pass +{ + NetlistFromComputeGraphPass() : Pass("netlist_from_compute_graph", "drivertools example") {} + + void help() override + { + // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---| + log("\n"); + } + + void execute(std::vector args, RTLIL::Design *design) override + { + // std::ifstream file(filename); + // std::string file_contents((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + // std::string err; + // json11::Json json = json11::Json::parse(file_contents, err); + // if (!err.empty()) { + // log_error("JSON parsing error: %s\n", err.c_str()); + // return; + // } + + // // Example assumes JSON has a nodes array and outputs array + // json11::Json::array nodes = json["nodes"].array_items(); + // json11::Json::array outputs = json["outputs"].array_items(); + + // // Process nodes + // for (auto &node : nodes) { + // std::string node_type = node["type"].string_value(); + // std::string node_id = node["id"].string_value(); + // json11::Json::object parameters = node["parameters"].object_items(); + + // if (node_type == "$$input") { + // // Create an input port in RTLIL + // RTLIL::Wire *wire = new RTLIL::Wire(); + // wire->name = RTLIL::IdString(node_id); + // wire->port_input = true; + // design->add(wire); + // } + // // Handle other node types similarly + // } + + // // Outputs handling could involve setting certain wires as outputs based on the JSON + + // log("Imported design from %s\n", filename.c_str()); + } +} ExampleDtPass; + +PRIVATE_NAMESPACE_END diff --git a/result b/result new file mode 120000 index 00000000000..85cae19e594 --- /dev/null +++ b/result @@ -0,0 +1 @@ +/nix/store/kxi3i88z8rq3dgywk1p6l5slnp84dyjf-yosys-sanitized \ No newline at end of file