Skip to content

Commit

Permalink
option for bne
Browse files Browse the repository at this point in the history
  • Loading branch information
nanocoh committed Sep 25, 2024
1 parent dceaa17 commit c3e262a
Show file tree
Hide file tree
Showing 11 changed files with 885 additions and 108 deletions.
321 changes: 241 additions & 80 deletions src/optimization/ConstantPropagation.cpp

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/optimization/ConstantPropagation.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class ConstantPropagation {
void setTruthTableEngine(bool value) {
truthTableEngine_ = value;
}

void setNormalizedUniquification(bool value) {
normalizedUniquification_ = value;
}
private:

unsigned computeOutputValueForConstantInstance(DNLID instanceID);
Expand All @@ -41,6 +43,8 @@ class ConstantPropagation {
//void computOuputValuesforHalfAdder(DNLID instanceID);
void performConstantPropagationAnalysis();
void propagateConstants();
void changeDriverToLocal0(SNLInstTerm* term, DNLID id);
void changeDriverToLocal1(SNLInstTerm* term, DNLID id);
DNLFull* dnl_ = nullptr;
std::unordered_map<SNLID::DesignID, DNLID> designObjectID2Type_;
std::set<DNLID> initialConstants0_;
Expand All @@ -57,6 +61,7 @@ class ConstantPropagation {
partialConstantReaders_;
std::vector<SNLBitTerm*> constant1TopReaders_;
bool truthTableEngine_ = false;
bool normalizedUniquification_ = false;
};

} // namespace naja::NAJA_OPT
170 changes: 150 additions & 20 deletions src/optimization/Reduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
//
// SPDX-License-Identifier: Apache-2.0

#include <spdlog/spdlog.h>
#include "Reduction.h"
#include <spdlog/spdlog.h>
#include <ranges>
#include "SNLDesignTruthTable.h"
#include "SNLTruthTable.h"
Expand All @@ -15,25 +15,34 @@ using namespace naja::SNL;
using namespace naja::NAJA_OPT;
using namespace naja::BNE;

//#define DEBUG_PRINTS
// #define DEBUG_PRINTS

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

void ReductionOptimization::run() {
for (auto& partialConstantReader : partialConstantReaders_) {
reducPartialConstantInstance(partialConstantReader);
if (normalizedUniquification_) {
reducPartialConstantInstanceWithNormalizedUniquification(
partialConstantReader);
} else {
reducPartialConstantInstance(partialConstantReader);
}
}
if (normalizedUniquification_) {
bne_.process();
}
bne_.process();
/*report_ = collectStatistics();
spdlog::info(report_);*/
destroy();
}

SNLTruthTable ReductionOptimization::reduceTruthTable(SNLInstance* uniquifiedCandidate,
SNLTruthTable ReductionOptimization::reduceTruthTable(
SNLInstance* uniquifiedCandidate,
const SNLTruthTable& truthTable,
const std::vector<std::pair<SNLID::DesignObjectID, int>>& constTerms) {
assert(constTerms.size() <= truthTable.size());
Expand All @@ -51,12 +60,62 @@ SNLTruthTable ReductionOptimization::reduceTruthTable(SNLInstance* uniquifiedCan
}
for (auto& constTerm : constTerms) {
constInputs.push_back(
ConstantInput(termID2index[constTerm.first],
constTerm.second));
ConstantInput(termID2index[constTerm.first], constTerm.second));
}
return truthTable.getReducedWithConstants(constInputs);
}

void ReductionOptimization::replaceInstance(
SNLInstance* instance,
const std::pair<SNLDesign*, SNLLibraryTruthTables::Indexes>& result) {
reductionStatistics_[std::pair<std::string, std::string>(
instance->getModel()->getName().getString(),
result.first->getName().getString())]++;
SNLDesign* design = instance->getDesign();
SNLDesign* reducedDesign = result.first;
SNLInstance* reducedInstance = SNLInstance::create(
design, reducedDesign,
SNLName(std::string(instance->getName().getString()) + "_reduced"));
std::vector<SNLInstTerm*> reducedInstTerms;
SNLInstTerm* output = nullptr;
SNLInstTerm* reducedOutput = nullptr;
for (auto term : reducedInstance->getInstTerms()) {
if (term->getDirection() != SNLInstTerm::Direction::Input) {
reducedOutput = term;
continue;
}
reducedInstTerms.push_back(term);
}
size_t index = 0;
size_t originNonConstantIndex = 0;
for (auto term : instance->getInstTerms()) {
if (term->getDirection() != SNLInstTerm::Direction::Input) {
output = term;
break;
}
}
for (auto term : instance->getInstTerms()) {
SNLBitNet* bitNet = term->getNet();
term->setNet(nullptr);
if (bitNet->isConstant() || reducedInstTerms.empty()) {
continue;
}
originNonConstantIndex++;
if (std::find(result.second.begin(), result.second.end(),
originNonConstantIndex) != result.second.end()) {
continue;
}
reducedInstTerms[index]->setNet(bitNet);
index++;
if (index == reducedInstTerms.size()) {
break;
}
}
reducedOutput->setNet(output->getNet());
output->setNet(nullptr);
instance->destroy();
}

void ReductionOptimization::reducPartialConstantInstance(
std::tuple<std::vector<SNLID::DesignObjectID>,
std::vector<std::pair<SNLID::DesignObjectID, int>>,
Expand All @@ -66,11 +125,14 @@ void ReductionOptimization::reducPartialConstantInstance(
printf("reducPartialConstantInstance Reducing partial constant instance\n");
// LCOV_EXCL_STOP
#endif
auto library = *(SNLUniverse::get()->getTopDesign()->getDB()->getPrimitiveLibraries().begin());
/*Uniquifier uniquifier(std::get<0>(candidate), std::get<2>(candidate));
auto library = *(SNLUniverse::get()
->getTopDesign()
->getDB()
->getPrimitiveLibraries()
.begin());
Uniquifier uniquifier(std::get<0>(candidate), std::get<2>(candidate));
uniquifier.process();
SNLInstance* uniquifiedCandidate = uniquifier.getPathUniq().back();*/
auto inst = getInstanceForPath(std::get<0>(candidate));
SNLInstance* uniquifiedCandidate = uniquifier.getPathUniq().back();
/*if (!uniquifiedCandidate) {uniquifier.getPathUniq().back()
std::ostringstream reason;
auto instance = std::get<0>(candidate).back();
Expand All @@ -79,6 +141,76 @@ void ReductionOptimization::reducPartialConstantInstance(
<< instance->getDesign()->getName().getString();
throw SNLException(reason.str());
}*/
SNLTruthTable invTruthTable =
SNLDesignTruthTable::getTruthTable(uniquifiedCandidate->getModel());
if (!invTruthTable.isInitialized()) {
#ifdef DEBUG_PRINTS
// LCOV_EXCL_START
printf(
"reducPartialConstantInstance Truth table is not initialized for "
"design: %s\n",
unqiuifedCandidate->getModel()->getName().getString().c_str());
// LCOV_EXCL_STOP
#endif
return;
}
SNLTruthTable reducedTruthTable = reduceTruthTable(
uniquifiedCandidate, invTruthTable, std::get<1>(candidate));
if (reducedTruthTable.size() == 0) {
#ifdef DEBUG_PRINTS
// LCOV_EXCL_START
printf("reducPartialConstantInstance Full constant %s\n",
unqiuifedCandidate->getModel()->getName().getString().c_str());
// LCOV_EXCL_STOP
#endif
}
#ifdef DEBUG_PRINTS
// LCOV_EXCL_START
printf("reducPartialConstantInstance truth table: %s\n",
invTruthTable.getString().c_str());
printf("reducPartialConstantInstance reduced truth table: %s\n",
reducedTruthTable.getString().c_str());
// LCOV_EXCL_STOP
#endif
auto result =
SNLLibraryTruthTables::getDesignForTruthTable(library, reducedTruthTable);
if (result.first) {
#ifdef DEBUG_PRINTS
// LCOV_EXCL_START
printf("reducPartialConstantInstance design %s\n",
unqiuifedCandidate->getModel()->getName().getString().c_str());
printf("reducPartialConstantInstance redcued design %s\n",
result.first->getName().getString().c_str());
// LCOV_EXCL_STOP
#endif
replaceInstance(uniquifiedCandidate, result);
} else {
#ifdef DEBUG_PRINTS
// LCOV_EXCL_START
printf(
"reducPartialConstantInstanceNo design found for the reduced truth "
"table\n");
// LCOV_EXCL_STOP
#endif
}
}

void ReductionOptimization::
reducPartialConstantInstanceWithNormalizedUniquification(
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 = *(SNLUniverse::get()
->getTopDesign()
->getDB()
->getPrimitiveLibraries()
.begin());
auto inst = getInstanceForPath(std::get<0>(candidate));
SNLTruthTable invTruthTable =
SNLDesignTruthTable::getTruthTable(inst->getModel());
if (!invTruthTable.isInitialized()) {
Expand Down Expand Up @@ -123,9 +255,8 @@ void ReductionOptimization::reducPartialConstantInstance(
#endif
auto context = std::get<0>(candidate);
auto instance = context.back();
context.pop_back();
context.pop_back();
bne_.addReductionCommand(context, instance, result);
//replaceInstance(uniquifiedCandidate, result);
} else {
#ifdef DEBUG_PRINTS
// LCOV_EXCL_START
Expand All @@ -138,12 +269,11 @@ void ReductionOptimization::reducPartialConstantInstance(
}

std::string ReductionOptimization::collectStatistics() const {
std::stringstream ss;
std::stringstream ss;
ss << "RO report:" << std::endl;
for (const auto& entry : reductionStatistics_) {
ss << entry.first.first << " -> " << entry.first.second << " : " << entry.second << std::endl;
ss << entry.first.first << " -> " << entry.first.second << " : "
<< entry.second << std::endl;
}
return ss.str();
}


8 changes: 8 additions & 0 deletions src/optimization/Reduction.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ class ReductionOptimization {
const std::vector<std::pair<SNLID::DesignObjectID, int>>& constTerms);
void run();
std::string collectStatistics() const;
void setNormalizedUniquification(bool normalizedUniquification) {
normalizedUniquification_ = normalizedUniquification;
}
private:
void replaceInstance(SNLInstance* instance, const std::pair<SNLDesign*, SNLLibraryTruthTables::Indexes>& result);
void reducPartialConstantInstanceWithNormalizedUniquification(std::tuple<std::vector<SNLID::DesignObjectID>,
std::vector<std::pair<SNLID::DesignObjectID, int>>,
DNLID>& candidate);
void reducPartialConstantInstance(std::tuple<std::vector<SNLID::DesignObjectID>,
std::vector<std::pair<SNLID::DesignObjectID, int>>,
DNLID>& candidate);
Expand All @@ -39,6 +46,7 @@ class ReductionOptimization {
std::map<std::pair<std::string, std::string>, size_t> reductionStatistics_;
std::string report_;
BNE::BNE bne_;
bool normalizedUniquification_ = false;
};

} // namespace naja::NAJA_OPT
13 changes: 9 additions & 4 deletions src/optimization/RemoveLoadlessLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ void LoadlessLogicRemover::removeLoadlessInstances(
loadlessInstances) {
BNE::BNE bne;
for (auto& path : loadlessInstances) {
/*Uniquifier uniquifier(path.first, path.second);
if (!normalizedUniquification_) {
Uniquifier uniquifier(path.first, path.second);
uniquifier.process();
for (SNLInstTerm* term : uniquifier.getPathUniq().back()->getInstTerms()) {
auto net = term->getNet();
Expand All @@ -323,10 +324,14 @@ void LoadlessLogicRemover::removeLoadlessInstances(

// LCOV_EXCL_STOP
#endif
uniquifier.getPathUniq().back()->destroy();*/
bne.addDeleteAction(path.first);
uniquifier.getPathUniq().back()->destroy();
} else {
bne.addDeleteAction(path.first);
}
}
if (normalizedUniquification_) {
bne.process();
}
bne.process();
// #ifdef DEBUG_PRINTS
// LCOV_EXCL_START
spdlog::info("Deleted {} leaf instances out of {}", loadlessInstances.size(),
Expand Down
5 changes: 4 additions & 1 deletion src/optimization/RemoveLoadlessLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ class LoadlessLogicRemover {
void removeLoadlessLogic();
std::string collectStatistics() const;
std::string getReport() const { return report_; }

void setNormalizedUniquification(bool normalizedUniquification) {
normalizedUniquification_ = normalizedUniquification;
}
private:
naja::DNL::DNL<DNLInstanceFull, DNLTerminalFull>* dnl_;
std::vector<std::pair<std::vector<SNLID::DesignObjectID>, DNLID>> loadlessInstances_;
std::string report_;
bool normalizedUniquification_ = false;
};

} // namespace naja::NAJA_OPT
1 change: 1 addition & 0 deletions test/logic_opt/ConstantPropagationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2750,6 +2750,7 @@ TEST_F(ConstantPropagationTests, TestConstantPropagationAND_Hierarchical_duplica
.c_str());
}
ConstantPropagation cp;
cp.setNormalizedUniquification(true);
// 13. collect the constants
cp.collectConstants();
// 14. run the constant propagation
Expand Down
Loading

0 comments on commit c3e262a

Please sign in to comment.