From a29637cb9bdb3aa57c69070119cb15b45b3287ce Mon Sep 17 00:00:00 2001 From: Christophe Alexandre Date: Wed, 17 Apr 2024 10:50:59 +0200 Subject: [PATCH] cleaning naja_edit (#64) --- src/apps/edit/CMakeLists.txt | 2 +- src/apps/edit/NajaEdit.cpp | 57 ++++++++++++++++++++---- src/optimization/CMakeLists.txt | 1 + src/optimization/RemoveLoadlessLogic.cpp | 11 +++-- 4 files changed, 57 insertions(+), 14 deletions(-) diff --git a/src/apps/edit/CMakeLists.txt b/src/apps/edit/CMakeLists.txt index ac60d5a7..51f1be7e 100644 --- a/src/apps/edit/CMakeLists.txt +++ b/src/apps/edit/CMakeLists.txt @@ -4,7 +4,7 @@ add_executable(naja_edit NajaEdit.cpp) target_include_directories(naja_edit SYSTEM BEFORE PUBLIC ${Boost_INCLUDE_DIR}) -target_include_directories(naja_edit PUBLIC ${ARGPARSE_DIR} ${SPDLOG_DIR}) +target_include_directories(naja_edit PUBLIC ${ARGPARSE_DIR}) target_link_libraries(naja_edit naja_snl_verilog naja_snl_pyloader naja_snl_dump naja_dnl naja_opt) install(TARGETS naja_edit DESTINATION bin) \ No newline at end of file diff --git a/src/apps/edit/NajaEdit.cpp b/src/apps/edit/NajaEdit.cpp index b8271e14..83fb2a5f 100644 --- a/src/apps/edit/NajaEdit.cpp +++ b/src/apps/edit/NajaEdit.cpp @@ -31,7 +31,6 @@ using namespace naja::SNL; namespace { enum class FormatType { NOT_PROVIDED, UNKNOWN, VERILOG, SNL }; - FormatType argToFormatType(const std::string& inputFormat) { if (inputFormat.empty()) { return FormatType::NOT_PROVIDED; @@ -44,10 +43,24 @@ FormatType argToFormatType(const std::string& inputFormat) { } } +enum class OptimizationType { NOT_PROVIDED, UNKNOWN, DLE, ALL }; +OptimizationType argToOptimizationType(const std::string& optimization) { + if (optimization.empty()) { + return OptimizationType::NOT_PROVIDED; + } else if (optimization == "dle") { + return OptimizationType::DLE; + } else if (optimization == "all") { + return OptimizationType::ALL; + } else { + return OptimizationType::UNKNOWN; + } +} + } int main(int argc, char* argv[]) { argparse::ArgumentParser program("naja_edit"); + program.add_description("Edit gate level netlists using python script and apply optimizations"); program.add_argument("-f", "--from_format") .required() .help("from/input format"); @@ -63,10 +76,12 @@ int main(int argc, char* argv[]) { .help("input primitives"); program.add_argument("-d", "--dump_primitives") .help("dump primitives library in verilog"); - program.add_argument("-e", "--edit") - .help("edit netlist using python script"); - program.add_argument("-dle", "--dead_logic_elimination") - .help("dead logic elimination"); + program.add_argument("-e", "--pre_edit") + .help("edit netlist using python script after loading netlist and before applying optimizations"); + program.add_argument("-z", "--post_edit") + .help("edit netlist using python script after optimizations and before dumping netlist"); + program.add_argument("-a", "--apply") + .help("apply optimization: dle (remove loadless logic), all (all optimizations)"); try { program.parse_args(argc, argv); @@ -76,7 +91,6 @@ int main(int argc, char* argv[]) { return 1; } - std::vector sinks; auto console_sink = std::make_shared(); console_sink->set_level(spdlog::level::info); @@ -96,7 +110,6 @@ int main(int argc, char* argv[]) { spdlog::info("naja_edit"); spdlog::info("########################################################"); - bool argError = false; auto inputFormatArg = program.present("-f"); std::string inputFormat = *inputFormatArg; @@ -135,6 +148,14 @@ int main(int argc, char* argv[]) { } } + auto optimizationArg = program.present("-a"); + std::string optimization = *optimizationArg; + OptimizationType optimizationType = argToOptimizationType(optimization); + if (optimizationType == OptimizationType::UNKNOWN) { + spdlog::critical("Unrecognized optimization type: {}", optimization); + argError = true; + } + if (argError) { std::exit(-1); } @@ -227,22 +248,40 @@ int main(int argc, char* argv[]) { if (program.is_used("-e")) { auto editPath = std::filesystem::path(program.get("-e")); + spdlog::info("Post editing netlist using python script: {}", editPath.string()); SNLPyEdit::edit(editPath); } - if (program.is_used("-dle")) { + if (optimizationType != OptimizationType::DLE + or optimizationType != OptimizationType::ALL) { + const auto start{std::chrono::steady_clock::now()}; + spdlog::info("Starting removal of loadless logic"); LoadlessLogicRemover remover; remover.process(); - printf("Removed loadless logic\n"); + const auto end{std::chrono::steady_clock::now()}; + const std::chrono::duration elapsed_seconds{end - start}; + { + std::ostringstream oss; + oss << "Removal of loadless logic done in: " << elapsed_seconds.count() << "s"; + spdlog::info(oss.str()); + } + } + + if (program.is_used("-z")) { + auto editPath = std::filesystem::path(program.get("-z")); + spdlog::info("Post editing netlist using python script: {}", editPath.string()); + SNLPyEdit::edit(editPath); } if (outputFormatType == FormatType::SNL) { + spdlog::info("Dumping netlist in SNL format to {}", outputPath.string()); SNLCapnP::dump(db, outputPath); } else if (outputFormatType == FormatType::VERILOG) { if (db->getTopDesign()) { std::ofstream output(outputPath); SNLVRLDumper dumper; dumper.setSingleFile(true); + spdlog::info("Dumping netlist in verilog format to {}", outputPath.string()); dumper.dumpDesign(db->getTopDesign(), output); } else { db->debugDump(0); diff --git a/src/optimization/CMakeLists.txt b/src/optimization/CMakeLists.txt index 98b881b5..b3d46785 100644 --- a/src/optimization/CMakeLists.txt +++ b/src/optimization/CMakeLists.txt @@ -19,6 +19,7 @@ target_compile_options(naja_opt PRIVATE ${NAJA_CXX_WARNINGS}) target_include_directories(naja_opt SYSTEM BEFORE PUBLIC ${Boost_INCLUDE_DIR}) target_include_directories(naja_opt PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_include_directories(naja_opt PUBLIC ${SPDLOG_DIR}) set_target_properties(naja_opt PROPERTIES PUBLIC_HEADER "${naja_opt_HEADERS}") diff --git a/src/optimization/RemoveLoadlessLogic.cpp b/src/optimization/RemoveLoadlessLogic.cpp index 18057d43..b8dedd9e 100644 --- a/src/optimization/RemoveLoadlessLogic.cpp +++ b/src/optimization/RemoveLoadlessLogic.cpp @@ -3,11 +3,14 @@ // // SPDX-License-Identifier: Apache-2.0 -#include "RemoveLoadlessLogic.h" #include +#include + #include "SNLBusNetBit.h" #include "SNLDB0.h" #include "SNLUniverse.h" + +#include "RemoveLoadlessLogic.h" #include "Utils.h" using namespace naja::DNL; @@ -281,8 +284,9 @@ void LoadlessLogicRemover::removeLoadlessInstances( } //#ifdef DEBUG_PRINTS // LCOV_EXCL_START - printf("Deleted %lu leaf instances out of %lu\n", loadlessInstances.size(), - dnl_->getLeaves().size()); + spdlog::info("Deleted {} leaf instances out of {}", + loadlessInstances.size(), + dnl_->getLeaves().size()); // LCOV_EXCL_STOP ///#endif } @@ -300,5 +304,4 @@ void LoadlessLogicRemover::removeLoadlessLogic() { DNL::destroy(); } - } // namespace naja::NAJA_OPT \ No newline at end of file