Skip to content

Commit

Permalink
Pywrap refactor (#140)
Browse files Browse the repository at this point in the history
* WIP

* WIP

* Adding full banner
Adding banner in verilog files
in  perf stats also

* Cleaning log banner
  • Loading branch information
xtofalex authored Nov 5, 2024
1 parent b1e00ef commit 8efb88a
Show file tree
Hide file tree
Showing 52 changed files with 380 additions and 93 deletions.
18 changes: 12 additions & 6 deletions src/apps/naja_edit/NajaEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "NajaVersion.h"
#include "NajaPerf.h"
#include "NajaUtils.h"

#include "SNLException.h"
#include "SNLPyEdit.h"
Expand Down Expand Up @@ -140,7 +141,17 @@ int main(int argc, char* argv[]) {

if (program.is_used("--log")) {
auto logName = program.get<std::string>("--log");
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(logName, true);
{
std::ofstream logFile(logName, std::ios::out);
if (logFile.is_open()) {
std::string bannerTitle = "naja_edit " + NAJA_EDIT_VERSION;
std::ostringstream bannerStream;
naja::NajaUtils::createBanner(logFile, bannerTitle, "#");
logFile << std::endl;
logFile.close();
}
}
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(logName);
file_sink->set_level(spdlog::level::trace);
sinks.push_back(file_sink);
}
Expand All @@ -151,11 +162,6 @@ int main(int argc, char* argv[]) {

spdlog::set_default_logger(edit_logger);
spdlog::flush_every(std::chrono::seconds(3));
SPDLOG_INFO("########################################################");
SPDLOG_INFO("naja_edit {}", NAJA_EDIT_VERSION);
SPDLOG_INFO("naja version: {}", naja::NAJA_VERSION);
SPDLOG_INFO("Git hash: {}", naja::NAJA_GIT_HASH);
SPDLOG_INFO("########################################################");

bool argError = false;

Expand Down
10 changes: 5 additions & 5 deletions src/apps/naja_edit/examples/addaccu/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
YOSYS ?= yosys
NAJA_EDIT ?= ${NAJA_INSTALL}/bin/naja_edit
PRIMITIVES ?= ${NAJA_INSTALL}/shared/primitives/xilinx.py
SNL_INTERCHANGE_DIR ?= ${NAJA_INSTALL}/shared/snlif
PRIMITIVES ?= ${NAJA_INSTALL}/share/naja/primitives/xilinx.py
SNL_INTERCHANGE_DIR ?= ${NAJA_INSTALL}/share/naja/snlif
PYTHON_ENV ?= export PYTHONPATH=${NAJA_INSTALL}/lib/python;

all: addaccu_snl.v addaccu_edited.v
Expand All @@ -16,13 +16,13 @@ yosys addaccu_netlist.v: addaccu.v
${YOSYS} synth.ys

snl_if addaccu_snl/snl.mf: addaccu_netlist.v
${PYTHON_ENV} ${NAJA_EDIT} -f verilog -t snl -i $< -o addaccu_snl -p ${PRIMITIVES}
${PYTHON_ENV} ${NAJA_EDIT} -l -f verilog -t snl -i $< -o addaccu_snl -p ${PRIMITIVES}

addaccu_snl.v: addaccu_snl/snl.mf
${PYTHON_ENV} ${NAJA_EDIT} -f snl -t verilog -i addaccu_snl -o $@
${PYTHON_ENV} ${NAJA_EDIT} -l -f snl -t verilog -i addaccu_snl -o $@

addaccu_edited.v: addaccu_snl/snl.mf
${PYTHON_ENV} ${NAJA_EDIT} -f snl -t verilog -i addaccu_snl -o $@ -e edit.py
${PYTHON_ENV} ${NAJA_EDIT} -l -f snl -t verilog -i addaccu_snl -o $@ -e edit.py

clean:
-rm -rf __pycache__ addaccu_snl
Expand Down
3 changes: 3 additions & 0 deletions src/core/NajaPerf.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <fstream>
#include <iomanip>

#include "NajaUtils.h"
#include "NajaException.h"

namespace naja {
Expand Down Expand Up @@ -71,6 +72,8 @@ class NajaPerf {
NajaPerf(const std::filesystem::path& logPath, const std::string& topName):
startClock_(std::chrono::steady_clock::now()) {
os_.open(logPath);
NajaUtils::createBanner(os_, "Naja Performance Report", "#");
os_ << std::endl;
}

static void registerDestructor() {
Expand Down
12 changes: 12 additions & 0 deletions src/core/NajaUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "NajaUtils.h"

#include <chrono>
#include <string>
#include "NajaVersion.h"

namespace {

Expand All @@ -22,8 +24,18 @@ void NajaUtils::createBanner(
std::ostream& stream,
const std::string& title,
const std::string& commentChar) {
for (size_t i=0; i<(80/commentChar.size()); ++i) {
stream << commentChar;
}
stream << std::endl;
stream << commentChar << " " << currentDate();
stream << commentChar << " " << title << std::endl;
stream << commentChar << " naja version: " << naja::NAJA_VERSION << std::endl;
stream << commentChar << " Git hash: " << naja::NAJA_GIT_HASH << std::endl;
for (size_t i=0; i<(80/commentChar.size()); ++i) {
stream << commentChar;
}
stream << std::endl;
}

} // namespace naja
20 changes: 20 additions & 0 deletions src/snl/formats/verilog/backend/SNLVRLDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <fstream>
#include <unordered_set>

#include "NajaUtils.h"

#include "SNLLibrary.h"
#include "SNLDesign.h"
#include "SNLParameter.h"
Expand Down Expand Up @@ -797,6 +799,12 @@ void SNLVRLDumper::dumpDesign(const SNLDesign* design, const std::filesystem::pa
std::filesystem::path filePath = path/getTopFileName(design);
std::ofstream outFile;
outFile.open(filePath);
NajaUtils::createBanner(
outFile,
"Verilog file for " + design->getName().getString(),
"//"
);
outFile << std::endl;
dumpDesign(design, outFile);
} else {
SNLVRLDumper streamDumper;
Expand All @@ -810,6 +818,12 @@ void SNLVRLDumper::dumpDesign(const SNLDesign* design, const std::filesystem::pa
std::filesystem::path filePath = path/getTopFileName(design);
std::ofstream outFile;
outFile.open(filePath);
NajaUtils::createBanner(
outFile,
"Verilog file for " + design->getName().getString(),
"//"
);
outFile << std::endl;
streamDumper.dumpDesign(design, outFile);
}
}
Expand All @@ -832,6 +846,12 @@ void SNLVRLDumper::dumpLibrary(const SNLLibrary* library, const std::filesystem:
std::filesystem::path filePath = path/getLibraryFileName(library);
std::ofstream outFile;
outFile.open(filePath);
NajaUtils::createBanner(
outFile,
"Verilog file for " + library->getName().getString(),
"//"
);
outFile << std::endl;
dumpLibrary(library, outFile);
} else {
for (auto design: library->getDesigns()) {
Expand Down
34 changes: 17 additions & 17 deletions src/snl/python/snl_wrapping/PyInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ PyObject* richCompare(T left, T right, int op) {
// Example : baseOject_.object_
#define ACCESS_OBJECT object_

#define SNLTRY try {
#define TRY try {

#define SNLCATCH \
} catch (const naja::SNL::SNLException& e) { \
Expand Down Expand Up @@ -231,20 +231,20 @@ PyObject* richCompare(T left, T right, int op) {
return PySNL##OBJECT_TYPE##_Link(selfObject->METHOD()); \
}

#define GetObjectByName(SELF_TYPE, OBJECT_TYPE) \
static PyObject* PySNL##SELF_TYPE##_get##OBJECT_TYPE(PySNL##SELF_TYPE* self, PyObject* args) { \
SNL##OBJECT_TYPE* obj = nullptr; \
METHOD_HEAD("SNL##SELF_TYPE.get##OBJECT_TYPE()") \
#define GetObjectByName(SELF_TYPE, OBJECT_TYPE, METHOD) \
static PyObject* Py##SELF_TYPE##_##METHOD(Py##SELF_TYPE* self, PyObject* args) { \
OBJECT_TYPE* obj = nullptr; \
METHOD_HEAD("SELF_TYPE.METHOD()") \
char* name = NULL; \
if (PyArg_ParseTuple(args, "s:SNL##SELF_TYPE.get##OBJECT_TYPE", &name)) { \
SNLTRY \
obj = selfObject->get##OBJECT_TYPE(SNLName(name)); \
if (PyArg_ParseTuple(args, "s:SELF_TYPE.METHOD", &name)) { \
TRY \
obj = selfObject->METHOD(SNLName(name)); \
SNLCATCH \
} else { \
setError("invalid number of parameters for get##OBJECT_TYPE."); \
setError("invalid number of parameters for METHOD."); \
return nullptr; \
} \
return PySNL##OBJECT_TYPE##_Link(obj); \
return Py##OBJECT_TYPE##_Link(obj); \
}

#define GetObjectByIndex(SELF_TYPE, OBJECT_TYPE, METHOD) \
Expand All @@ -253,7 +253,7 @@ PyObject* richCompare(T left, T right, int op) {
METHOD_HEAD("SNL"#SELF_TYPE".get"#OBJECT_TYPE"()") \
int index = 0; \
if (PyArg_ParseTuple(args, "i:SNL##SELF_TYPE.get##METHOD", &index)) { \
SNLTRY \
TRY \
obj = selfObject->get##METHOD(index); \
SNLCATCH \
} else { \
Expand All @@ -266,7 +266,7 @@ PyObject* richCompare(T left, T right, int op) {
#define GetNameMethod(SELF_TYPE) \
static PyObject* Py##SELF_TYPE##_getName(Py##SELF_TYPE* self) { \
METHOD_HEAD(#SELF_TYPE".getName()") \
SNLTRY \
TRY \
return PyUnicode_FromString(selfObject->getName().getString().c_str()); \
SNLCATCH \
return nullptr; \
Expand All @@ -286,7 +286,7 @@ PyObject* richCompare(T left, T right, int op) {
#define GetStringAttribute(SELF_TYPE, METHOD) \
static PyObject* PySNL##SELF_TYPE##_##METHOD(PySNL##SELF_TYPE* self) { \
METHOD_HEAD("SNL"#SELF_TYPE"."#METHOD"()") \
SNLTRY \
TRY \
return PyUnicode_FromString(selfObject->METHOD().c_str()); \
SNLCATCH \
return nullptr; \
Expand Down Expand Up @@ -467,7 +467,7 @@ PyObject* richCompare(T left, T right, int op) {
static PyObject* PySNL##TYPE##_get##GET_OBJECTS(PySNL##TYPE *self) { \
METHOD_HEAD("SNL" #TYPE ".get" #GET_OBJECTS "()") \
PySNL##CONTAINER* pyObjects = nullptr; \
SNLTRY \
TRY \
auto objects = new naja::NajaCollection<SNL##ITERATED>(selfObject->get##GET_OBJECTS()); \
pyObjects = PyObject_NEW(PySNL##CONTAINER, &PyTypeSNL##CONTAINER); \
if (not pyObjects) return nullptr; \
Expand All @@ -480,7 +480,7 @@ PyObject* richCompare(T left, T right, int op) {
static PyObject* PySNL##TYPE##_get##GET_OBJECTS(PySNL##TYPE *self) { \
METHOD_HEAD("SNL" #TYPE ".get" #GET_OBJECTS "()") \
PySNL##CONTAINER* pyObjects = nullptr; \
SNLTRY \
TRY \
auto objects = new naja::NajaCollection<SNL##ITERATED*>(selfObject->get##GET_OBJECTS()); \
pyObjects = PyObject_NEW(PySNL##CONTAINER, &PyTypeSNL##CONTAINER); \
if (not pyObjects) return nullptr; \
Expand All @@ -493,7 +493,7 @@ PyObject* richCompare(T left, T right, int op) {
static PyObject* PySNL##TYPE##_##METHOD(PySNL##TYPE *self) { \
METHOD_HEAD("SNL" #TYPE "." #METHOD "()") \
PySNL##ITERATED##s* pyObjects = nullptr; \
SNLTRY \
TRY \
auto objects = new naja::NajaCollection<SNL##ITERATED*>(selfObject->METHOD()); \
pyObjects = PyObject_NEW(PySNL##ITERATED##s, &PyTypeSNL##ITERATED##s); \
if (not pyObjects) return nullptr; \
Expand All @@ -505,7 +505,7 @@ PyObject* richCompare(T left, T right, int op) {
#define GetDesignModelingRelatedObjects(TYPE, GETTER, OWNER_TYPE) \
if (IsPy##TYPE(object)) { \
auto object_o = PY##TYPE##_O(object); \
SNLTRY \
TRY \
auto objects = new naja::NajaCollection<TYPE*>(SNLDesignModeling::GETTER(object_o)); \
auto pyObjects = PyObject_NEW(Py##TYPE##s, &PyType##TYPE##s); \
if (not pyObjects) return nullptr; \
Expand Down
2 changes: 1 addition & 1 deletion src/snl/python/snl_wrapping/PySNLBusNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static PyObject* PySNLBusNet_create(PyObject*, PyObject* args) {
}

SNLBusNet* net = nullptr;
SNLTRY
TRY
if (IsPySNLDesign(arg0)) {
net = SNLBusNet::create(PYSNLDesign_O(arg0), arg1, arg2, name);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/snl/python/snl_wrapping/PySNLBusTerm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static PyObject* PySNLBusTerm_create(PyObject*, PyObject* args) {
}

SNLBusTerm* term = nullptr;
SNLTRY
TRY
if (IsPySNLDesign(arg0)) {
term = SNLBusTerm::create(PYSNLDesign_O(arg0), direction, arg2, arg3, name);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/snl/python/snl_wrapping/PySNLDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ static PyObject* PySNLDB_create(PyObject*, PyObject* args) {
}
auto universe = PYSNLUNIVERSE_O(arg);
SNLDB* db = nullptr;
SNLTRY
TRY
db = SNLDB::create(universe);
SNLCATCH
return PySNLDB_Link(db);
}

GetObjectByName(DB, Library)
GetObjectByName(SNLDB, SNLLibrary, getLibrary)
GetContainerMethod(DB, Library, Libraries, Libraries)

DBoDestroyAttribute(PySNLDB_destroy, PySNLDB)
Expand Down
26 changes: 13 additions & 13 deletions src/snl/python/snl_wrapping/PySNLDesign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static PyObject* PySNLDesign_create(PyObject*, PyObject* args) {
}

SNLDesign* design = nullptr;
SNLTRY
TRY
if (IsPySNLLibrary(arg0)) {
design = SNLDesign::create(PYSNLLibrary_O(arg0), name);
} else {
Expand All @@ -73,7 +73,7 @@ static PyObject* PySNLDesign_createPrimitive(PyObject*, PyObject* args) {
}

SNLDesign* design = nullptr;
SNLTRY
TRY
if (IsPySNLLibrary(arg0)) {
design = SNLDesign::create(PYSNLLibrary_O(arg0), SNLDesign::Type::Primitive, name);
} else {
Expand All @@ -97,7 +97,7 @@ static PyObject* PySNLDesign_clone(PySNLDesign* self, PyObject* args) {

SNLDesign* newDesign = nullptr;
METHOD_HEAD("SNLDesign.clone()")
SNLTRY
TRY
newDesign = selfObject->clone(name);
SNLCATCH
return PySNLDesign_Link(newDesign);
Expand All @@ -111,7 +111,7 @@ static PyObject* PySNLDesign_dumpVerilog(PySNLDesign* self, PyObject* args) {
return nullptr;
}
METHOD_HEAD("SNLDesign.dumpVerilog()")
SNLTRY
TRY
SNLVRLDumper dumper;
dumper.setTopFileName(arg1);
dumper.dumpDesign(selfObject, std::filesystem::path(arg0));
Expand Down Expand Up @@ -174,7 +174,7 @@ static PyObject* PySNLDesign_addCombinatorialArcs(PySNLDesign* self, PyObject* a
}
}
}
SNLTRY
TRY
SNLDesignModeling::addCombinatorialArcs(terms0, terms1);
SNLCATCH
Py_RETURN_NONE;
Expand Down Expand Up @@ -304,14 +304,14 @@ static PyObject* PySNLDesign_getClockRelatedOutputs(PySNLDesign*, PyObject* obje

GetObjectMethod(Design, DB, getDB)
GetObjectMethod(Design, Library, getLibrary)
GetObjectByName(Design, Instance)
GetObjectByName(Design, Term)
GetObjectByName(Design, ScalarTerm)
GetObjectByName(Design, BusTerm)
GetObjectByName(Design, Net)
GetObjectByName(Design, ScalarNet)
GetObjectByName(Design, BusNet)
GetObjectByName(Design, Parameter)
GetObjectByName(SNLDesign, SNLInstance, getInstance)
GetObjectByName(SNLDesign, SNLTerm, getTerm)
GetObjectByName(SNLDesign, SNLScalarTerm, getScalarTerm)
GetObjectByName(SNLDesign, SNLBusTerm, getBusTerm)
GetObjectByName(SNLDesign, SNLNet, getNet)
GetObjectByName(SNLDesign, SNLScalarNet, getScalarNet)
GetObjectByName(SNLDesign, SNLBusNet, getBusNet)
GetObjectByName(SNLDesign, SNLParameter, getParameter)
GetNameMethod(SNLDesign)
GetBoolAttribute(Design, isAnonymous)
GetBoolAttribute(Design, isBlackBox)
Expand Down
2 changes: 1 addition & 1 deletion src/snl/python/snl_wrapping/PySNLInstParameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static PyObject* PySNLInstParameter_create(PyObject*, PyObject* args) {
return nullptr;
}
SNLInstParameter* instParameter = nullptr;
SNLTRY
TRY
if (not IsPySNLInstance(arg0)) {
setError("SNLInstParameter create needs SNLInstance as first argument");
return nullptr;
Expand Down
Loading

0 comments on commit 8efb88a

Please sign in to comment.