Skip to content

Commit

Permalink
cleaning naja_edit (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
xtofalex authored Apr 17, 2024
1 parent 0287916 commit a29637c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/apps/edit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
57 changes: 48 additions & 9 deletions src/apps/edit/NajaEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -76,7 +91,6 @@ int main(int argc, char* argv[]) {
return 1;
}


std::vector<spdlog::sink_ptr> sinks;
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
console_sink->set_level(spdlog::level::info);
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -227,22 +248,40 @@ int main(int argc, char* argv[]) {

if (program.is_used("-e")) {
auto editPath = std::filesystem::path(program.get<std::string>("-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<double> 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<std::string>("-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);
Expand Down
1 change: 1 addition & 0 deletions src/optimization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
11 changes: 7 additions & 4 deletions src/optimization/RemoveLoadlessLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
//
// SPDX-License-Identifier: Apache-2.0

#include "RemoveLoadlessLogic.h"
#include <vector>
#include <spdlog/spdlog.h>

#include "SNLBusNetBit.h"
#include "SNLDB0.h"
#include "SNLUniverse.h"

#include "RemoveLoadlessLogic.h"
#include "Utils.h"

using namespace naja::DNL;
Expand Down Expand Up @@ -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
}
Expand All @@ -300,5 +304,4 @@ void LoadlessLogicRemover::removeLoadlessLogic() {
DNL::destroy();
}


} // namespace naja::NAJA_OPT

0 comments on commit a29637c

Please sign in to comment.