Skip to content

Commit

Permalink
Merge pull request #84 from nanocoh/main
Browse files Browse the repository at this point in the history
Opt + Uniq fixes
  • Loading branch information
nanocoh authored Jun 8, 2024
2 parents 92179f1 + 19fc691 commit b4d112c
Show file tree
Hide file tree
Showing 19 changed files with 186 additions and 90 deletions.
5 changes: 4 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
url = https://github.com/google/googletest
[submodule "thirdparty/naja-verilog"]
path = thirdparty/naja-verilog
url = https://github.com/najaeda/naja-verilog
url = https://github.com/najaeda/naja-verilog
[submodule "thirdparty/cpptrace"]
path = thirdparty/cpptrace
url = https://github.com/jeremy-rifkin/cpptrace
3 changes: 2 additions & 1 deletion src/apps/naja_edit/NajaEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ int main(int argc, char* argv[]) {
dumper.dumpLibrary(primitivesLibrary, output);
}
} catch (const SNLException& e) {
SPDLOG_CRITICAL("Caught SNL error: {}", e.getReason());
SPDLOG_CRITICAL("Caught SNL error: {}\n{}",
e.what(), e.trace().to_string());
std::exit(EXIT_FAILURE);
}
std::exit(EXIT_SUCCESS);
Expand Down
2 changes: 1 addition & 1 deletion src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ set(NAJA_CORE_HEADERS
list(APPEND NAJA_CORE_HEADERS ${CMAKE_CURRENT_BINARY_DIR}/NajaVersion.h)

add_library(naja_core SHARED ${SOURCES})
target_link_libraries(naja_core PUBLIC coverage_config sanitizers_config)
target_link_libraries(naja_core PUBLIC cpptrace::cpptrace coverage_config sanitizers_config)

target_compile_options(naja_core PRIVATE ${NAJA_CXX_WARNINGS})

Expand Down
6 changes: 4 additions & 2 deletions src/core/NajaException.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
#ifndef __NAJA_EXCEPTION_H_
#define __NAJA_EXCEPTION_H_

#include <cpptrace/cpptrace.hpp>

namespace naja {

struct NajaException: public std::exception {
struct NajaException: public cpptrace::lazy_exception {
public:
NajaException() = delete;
NajaException(const NajaException&) = default;

NajaException(const std::string& reason):
std::exception(),
cpptrace::lazy_exception(),
reason_(reason)
{}

Expand Down
106 changes: 72 additions & 34 deletions src/optimization/ConstantPropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,19 @@ void ConstantPropagation::initializeTypesID() {
}

void ConstantPropagation::collectConstants() {
SNLTruthTable tt0(0, 0);
auto logic0 = SNLLibraryTruthTables::getDesignForTruthTable(
*(dnl_->getTop().getSNLModel()->getDB()->getPrimitiveLibraries().begin()),
tt0)
.first;
SNLTruthTable tt1(0, 1);
auto logic1 = SNLLibraryTruthTables::getDesignForTruthTable(
*(dnl_->getTop().getSNLModel()->getDB()->getPrimitiveLibraries().begin()),
tt1)
.first;
for (DNLID leaf : dnl_->getLeaves()) {
DNLInstanceFull instance = dnl_->getDNLInstanceFromID(leaf);
std::string name = instance.getSNLModel()->getName().getString();
// printf("name: %s\n", name.c_str());
if (name.find("LOGIC0") != std::string::npos) {
if (instance.getSNLModel() == logic0) {
for (DNLID termId = instance.getTermIndexes().first;
termId <= instance.getTermIndexes().second; termId++) {
const DNLTerminalFull& term = dnl_->getDNLTerminalFromID(termId);
Expand All @@ -141,7 +149,7 @@ void ConstantPropagation::collectConstants() {
constants0_.insert(term.getIsoID());
}
}
} else if (name.find("LOGIC1") != std::string::npos) {
} else if (instance.getSNLModel() == logic1) {
for (DNLID termId = instance.getTermIndexes().first;
termId <= instance.getTermIndexes().second; termId++) {
const DNLTerminalFull& term = dnl_->getDNLTerminalFromID(termId);
Expand All @@ -167,23 +175,28 @@ unsigned ConstantPropagation::computeOutputValue(DNLID instanceID) {
DNLInstanceFull instance = dnl_->getDNLInstanceFromID(instanceID);
const SNLTruthTable& truthTable =
SNLDesignTruthTable::getTruthTable(instance.getSNLInstance()->getModel());
std::vector<std::pair<SNLInstTerm*, int>> constTerms;
if (not truthTable.isInitialized()) {
// LCOV_EXCL_START
return (unsigned)-1;
// LCOV_EXCL_STOP
}
std::vector<std::pair<SNLID::DesignObjectID, int>> constTerms;
for (DNLID termId = instance.getTermIndexes().first;
termId <= instance.getTermIndexes().second; termId++) {
const DNLTerminalFull& term = dnl_->getDNLTerminalFromID(termId);
if (term.getSnlBitTerm()->getDirection() != SNLBitTerm::Direction::Input) {
continue;
}
if (constants0_.find(term.getIsoID()) != constants0_.end()) {
constTerms.push_back({term.getSnlTerm(), 0});
constTerms.push_back({term.getSnlTerm()->getBitTerm()->getID(), 0});
} else if (constants1_.find(term.getIsoID()) != constants1_.end()) {
constTerms.push_back({term.getSnlTerm(), 1});
constTerms.push_back({term.getSnlTerm()->getBitTerm()->getID(), 1});
}
}
SNLTruthTable redcuedTruthTable = ReductionOptimization::reduceTruthTable(truthTable, constTerms);
if (redcuedTruthTable.is0()) {
SNLTruthTable reducedTruthTable = ReductionOptimization::reduceTruthTable(instance.getSNLInstance(), truthTable, constTerms);
if (reducedTruthTable.is0()) {
return 0;
} else if (redcuedTruthTable.is1()) {
} else if (reducedTruthTable.is1()) {
return 1;
}
return (unsigned)-1;
Expand Down Expand Up @@ -312,7 +325,9 @@ void ConstantPropagation::performConstantPropagationAnalysis() {
partialConstantInstances_.erase(reader.getDNLInstance().getID());

} else {
partialConstantInstances_.insert(reader.getDNLInstance().getID());
if (!reader.getDNLInstance().isTop()) {
partialConstantInstances_.insert(reader.getDNLInstance().getID());
}
}
}
}
Expand Down Expand Up @@ -881,12 +896,23 @@ void ConstantPropagation::changeDriverToLocal0(SNLInstTerm* term, DNLID id) {
assign0->setType(naja::SNL::SNLNet::Type::Supply0);
term->setNet(assign0);
SNLTruthTable tt(0, 0);
auto logic0 = SNLLibraryTruthTables::getDesignForTruthTable(
term->getInstance()->getModel()->getLibrary(), tt)
.first;
//find primitives library
if (term->getDB()->getPrimitiveLibraries().size() != 1) {
// LCOV_EXCL_START
throw SNLException("There should be only one primitive library");
// LCOV_EXCL_STOP
}
auto primitives = *term->getDB()->getPrimitiveLibraries().begin();
auto logic0 =
SNLLibraryTruthTables::getDesignForTruthTable(primitives, tt).first;

SNLInstance* logic0Inst = term->getDesign()->getInstance(SNLName(name));
if (nullptr == logic0Inst) {
if (logic0 == nullptr) {
// LCOV_EXCL_START
throw SNLException("No logic0 design found");
// LCOV_EXCL_STOP
}
logic0Inst = SNLInstance::create(term->getDesign(), logic0, SNLName(name));
}
(*logic0Inst->getInstTerms().begin())->setNet(assign0);
Expand All @@ -906,11 +932,23 @@ void ConstantPropagation::changeDriverToLocal1(SNLInstTerm* term, DNLID id) {
assign1->setType(naja::SNL::SNLNet::Type::Supply1);
term->setNet(assign1);
SNLTruthTable tt(0, 1);
auto logic1 = SNLLibraryTruthTables::getDesignForTruthTable(
term->getInstance()->getModel()->getLibrary(), tt)
.first;

//find primitives library
if (term->getDB()->getPrimitiveLibraries().size() != 1) {
// LCOV_EXCL_START
throw SNLException("There should be only one primitive library");
// LCOV_EXCL_STOP
}
auto primitives = *term->getDB()->getPrimitiveLibraries().begin();
auto logic1 =
SNLLibraryTruthTables::getDesignForTruthTable(primitives, tt).first;
SNLInstance* logic1Inst = term->getDesign()->getInstance(SNLName(name));
if (nullptr == logic1Inst) {
if (logic1 == nullptr) {
// LCOV_EXCL_START
throw SNLException("No logic1 design found");
// LCOV_EXCL_STOP
}
logic1Inst = SNLInstance::create(term->getDesign(), logic1, SNLName(name));
}
(*logic1Inst->getInstTerms().begin())->setNet(assign1);
Expand All @@ -932,16 +970,16 @@ void ConstantPropagation::propagateConstants() {
constant0TopReaders_.push_back(readerTerm.getSnlBitTerm());
continue;
}
std::vector<SNLInstance*> path;
std::vector<SNLID::DesignObjectID> path;
DNLInstanceFull currentInstance = readerInst;
while (currentInstance.isTop() == false) {
path.push_back(currentInstance.getSNLInstance());
path.push_back(currentInstance.getSNLInstance()->getID());
currentInstance = currentInstance.getParentInstance();
}
std::reverse(path.begin(), path.end());
constant0Readers_.push_back(
std::tuple<std::vector<SNLInstance*>, SNLInstTerm*, DNLID>(
path, readerTerm.getSnlTerm(), readerInst.getID()));
std::tuple<std::vector<SNLID::DesignObjectID>, SNLID::DesignObjectID, DNLID>(
path, readerTerm.getSnlTerm()->getBitTerm()->getID(), readerInst.getID()));
}
}
for (DNLID iso : constants1_) {
Expand All @@ -959,28 +997,28 @@ void ConstantPropagation::propagateConstants() {
constant1TopReaders_.push_back(readerTerm.getSnlBitTerm());
continue;
}
std::vector<SNLInstance*> path;
std::vector<SNLID::DesignObjectID> path;
DNLInstanceFull currentInstance = readerInst;
while (currentInstance.isTop() == false) {
path.push_back(currentInstance.getSNLInstance());
path.push_back(currentInstance.getSNLInstance()->getID());
currentInstance = currentInstance.getParentInstance();
}
std::reverse(path.begin(), path.end());
constant1Readers_.push_back(
std::tuple<std::vector<SNLInstance*>, SNLInstTerm*, DNLID>(
path, readerTerm.getSnlTerm(), readerInst.getID()));
std::tuple<std::vector<SNLID::DesignObjectID>, SNLID::DesignObjectID, DNLID>(
path, readerTerm.getSnlTerm()->getBitTerm()->getID(), readerInst.getID()));
}
}
for (DNLID instId : partialConstantInstances_) {
DNLInstanceFull inst = dnl_->getDNLInstanceFromID(instId);
std::vector<SNLInstance*> path;
std::vector<SNLID::DesignObjectID> path;
DNLInstanceFull currentInstance = inst;
while (currentInstance.isTop() == false) {
path.push_back(currentInstance.getSNLInstance());
path.push_back(currentInstance.getSNLInstance()->getID());
currentInstance = currentInstance.getParentInstance();
}
std::reverse(path.begin(), path.end());
std::vector<std::pair<SNLInstTerm*, int>> instTerms;
std::vector<std::pair<SNLID::DesignObjectID, int>> instTerms;
//size_t numInputs = 0;
for (DNLID termId = inst.getTermIndexes().first;
termId <= inst.getTermIndexes().second; termId++) {
Expand All @@ -990,24 +1028,24 @@ void ConstantPropagation::propagateConstants() {
//numInputs++;
if (constants0_.find(term.getIsoID()) != constants0_.end()) {
instTerms.push_back(
std::pair<SNLInstTerm*, int>(term.getSnlTerm(), 0));
std::pair<SNLID::DesignObjectID, int>(term.getSnlTerm()->getBitTerm()->getID(), 0));
} else if (constants1_.find(term.getIsoID()) != constants1_.end()) {
instTerms.push_back(
std::pair<SNLInstTerm*, int>(term.getSnlTerm(), 1));
std::pair<SNLID::DesignObjectID, int>(term.getSnlTerm()->getBitTerm()->getID(), 1));
}
}
}
//assert(numInputs > instTerms.size());
partialConstantReaders_.push_back(
std::tuple<std::vector<SNLInstance*>,
std::vector<std::pair<SNLInstTerm*, int>>, DNLID>(
std::tuple<std::vector<SNLID::DesignObjectID>,
std::vector<std::pair<SNLID::DesignObjectID, int>>, DNLID>(
path, instTerms, inst.getID()));
}
for (auto& path : constant0Readers_) {
Uniquifier uniquifier(std::get<0>(path), std::get<2>(path));
uniquifier.process();
SNLInstTerm* constTerm = uniquifier.getPathUniq().back()->getInstTerm(
std::get<1>(path)->getBitTerm());
std::get<1>(path));
changeDriverToLocal0(constTerm, std::get<2>(path));
}
for (SNLBitTerm* term : constant0TopReaders_) {
Expand Down Expand Up @@ -1039,7 +1077,7 @@ void ConstantPropagation::propagateConstants() {
Uniquifier uniquifier(std::get<0>(path), std::get<2>(path));
uniquifier.process();
SNLInstTerm* constTerm = uniquifier.getPathUniq().back()->getInstTerm(
std::get<1>(path)->getBitTerm());
std::get<1>(path));
changeDriverToLocal1(constTerm, std::get<2>(path));
}
for (SNLBitTerm* term : constant1TopReaders_) {
Expand Down
9 changes: 5 additions & 4 deletions src/optimization/ConstantPropagation.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class ConstantPropagation {
const std::set<DNLID>& getConstants0() const { return constants0_; }
const std::set<DNLID>& getConstants1() const { return constants1_; }
void initializeTypesID();
const std::vector<std::tuple<std::vector<SNLInstance*>, std::vector<std::pair<SNLInstTerm*, int>>, DNLID>>&
const std::vector<std::tuple<std::vector<SNLID::DesignObjectID>,
std::vector<std::pair<SNLID::DesignObjectID, int>>, DNLID>>&
getPartialConstantReaders() const {
return partialConstantReaders_;
}
Expand All @@ -47,12 +48,12 @@ class ConstantPropagation {
std::set<DNLID> constants0_;
std::set<DNLID> constants1_;
std::set<DNLID> partialConstantInstances_;
std::vector<std::tuple<std::vector<SNLInstance*>, SNLInstTerm*, DNLID>>
std::vector<std::tuple<std::vector<SNLID::DesignObjectID>, SNLID::DesignObjectID, DNLID>>
constant0Readers_;
std::vector<SNLBitTerm*> constant0TopReaders_;
std::vector<std::tuple<std::vector<SNLInstance*>, SNLInstTerm*, DNLID>>
std::vector<std::tuple<std::vector<SNLID::DesignObjectID>, SNLID::DesignObjectID, DNLID>>
constant1Readers_;
std::vector<std::tuple<std::vector<SNLInstance*>, std::vector<std::pair<SNLInstTerm*, int>>, DNLID>>
std::vector<std::tuple<std::vector<SNLID::DesignObjectID>, std::vector<std::pair<SNLID::DesignObjectID, int>>, DNLID>>
partialConstantReaders_;
std::vector<SNLBitTerm*> constant1TopReaders_;
bool truthTableEngine_ = false;
Expand Down
34 changes: 21 additions & 13 deletions src/optimization/Reduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ using namespace naja::NAJA_OPT;
//#define DEBUG_PRINTS

ReductionOptimization::ReductionOptimization(
const std::vector<std::tuple<std::vector<SNLInstance*>,
std::vector<std::pair<SNLInstTerm*, int>>,
const std::vector<std::tuple<std::vector<SNLID::DesignObjectID>,
std::vector<std::pair<SNLID::DesignObjectID, int>>,
DNLID>>& partialConstantReaders)
: partialConstantReaders_(partialConstantReaders) {}

Expand All @@ -30,16 +30,16 @@ void ReductionOptimization::run() {
spdlog::info(report_);
}

SNLTruthTable ReductionOptimization::reduceTruthTable(
SNLTruthTable ReductionOptimization::reduceTruthTable(SNLInstance* uniquifiedCandidate,
const SNLTruthTable& truthTable,
const std::vector<std::pair<SNLInstTerm*, int>>& constTerms) {
const std::vector<std::pair<SNLID::DesignObjectID, int>>& constTerms) {
assert(constTerms.size() <= truthTable.size());
std::map<size_t, size_t> termID2index;
size_t index = 0;
using ConstantInput = std::pair<uint32_t, bool>;
using ConstantInputs = std::vector<ConstantInput>;
ConstantInputs constInputs;
for (auto term : constTerms[0].first->getInstance()->getInstTerms()) {
for (auto term : uniquifiedCandidate->getInstTerms()) {
if (term->getDirection() != SNLInstTerm::Direction::Input) {
continue;
}
Expand All @@ -48,7 +48,7 @@ SNLTruthTable ReductionOptimization::reduceTruthTable(
}
for (auto& constTerm : constTerms) {
constInputs.push_back(
ConstantInput(termID2index[constTerm.first->getBitTerm()->getID()],
ConstantInput(termID2index[constTerm.first],
constTerm.second));
}
return truthTable.getReducedWithConstants(constInputs);
Expand Down Expand Up @@ -105,20 +105,28 @@ void ReductionOptimization::replaceInstance(
}

void ReductionOptimization::reducPartialConstantInstance(
std::tuple<std::vector<SNLInstance*>,
std::vector<std::pair<SNLInstTerm*, int>>,
std::tuple<std::vector<SNLID::DesignObjectID>,
std::vector<std::pair<SNLID::DesignObjectID, int>>,
DNLID>& candidate) {
#ifdef DEBUG_PRINTS
// LCOV_EXCL_START
printf("reducPartialConstantInstance Reducing partial constant instance\n");
// LCOV_EXCL_STOP
#endif
auto library = std::get<0>(candidate).back()->getModel()->getLibrary();
auto library = *(SNLUniverse::get()->getTopDesign()->getDB()->getPrimitiveLibraries().begin());
Uniquifier uniquifier(std::get<0>(candidate), std::get<2>(candidate));
uniquifier.process();
SNLInstance* unqiuifedCandidate = uniquifier.getPathUniq().back();
SNLInstance* uniquifiedCandidate = uniquifier.getPathUniq().back();
/*if (!uniquifiedCandidate) {uniquifier.getPathUniq().back()
std::ostringstream reason;
auto instance = std::get<0>(candidate).back();
reason << "Uniquified candidate is null for instance: "
<< instance->getName().getString() << " in design: "
<< instance->getDesign()->getName().getString();
throw SNLException(reason.str());
}*/
SNLTruthTable invTruthTable =
SNLDesignTruthTable::getTruthTable(unqiuifedCandidate->getModel());
SNLDesignTruthTable::getTruthTable(uniquifiedCandidate->getModel());
if (!invTruthTable.isInitialized()) {
#ifdef DEBUG_PRINTS
// LCOV_EXCL_START
Expand All @@ -131,7 +139,7 @@ void ReductionOptimization::reducPartialConstantInstance(
return;
}
SNLTruthTable reducedTruthTable =
reduceTruthTable(invTruthTable, std::get<1>(candidate));
reduceTruthTable(uniquifiedCandidate, invTruthTable, std::get<1>(candidate));
if (reducedTruthTable.size() == 0) {
#ifdef DEBUG_PRINTS
// LCOV_EXCL_START
Expand Down Expand Up @@ -159,7 +167,7 @@ void ReductionOptimization::reducPartialConstantInstance(
result.first->getName().getString().c_str());
// LCOV_EXCL_STOP
#endif
replaceInstance(unqiuifedCandidate, result);
replaceInstance(uniquifiedCandidate, result);
} else {
#ifdef DEBUG_PRINTS
// LCOV_EXCL_START
Expand Down
Loading

0 comments on commit b4d112c

Please sign in to comment.