yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
FreduceWorker Struct Reference
+ Collaboration diagram for FreduceWorker:

Public Member Functions

 FreduceWorker (RTLIL::Design *design, RTLIL::Module *module)
 
bool find_bit_in_cone (std::set< RTLIL::Cell * > &celldone, RTLIL::SigBit needle, RTLIL::SigBit haystack)
 
bool find_bit_in_cone (RTLIL::SigBit needle, RTLIL::SigBit haystack)
 
void dump ()
 
int run ()
 

Data Fields

RTLIL::Designdesign
 
RTLIL::Modulemodule
 
SigMap sigmap
 
drivers_t drivers
 
std::set< std::pair
< RTLIL::SigBit, RTLIL::SigBit > > 
inv_pairs
 

Detailed Description

Definition at line 551 of file freduce.cc.

Constructor & Destructor Documentation

FreduceWorker::FreduceWorker ( RTLIL::Design design,
RTLIL::Module module 
)
inline

Definition at line 560 of file freduce.cc.

560  : design(design), module(module), sigmap(module)
561  {
562  }
SigMap sigmap
Definition: freduce.cc:556
RTLIL::Design * design
Definition: freduce.cc:553
RTLIL::Module * module
Definition: freduce.cc:554

Member Function Documentation

void FreduceWorker::dump ( )
inline

Definition at line 589 of file freduce.cc.

590  {
591  std::string filename = stringf("%s_%s_%05d.il", dump_prefix.c_str(), RTLIL::id2cstr(module->name), reduce_counter);
592  log("%s Writing dump file `%s'.\n", reduce_counter ? " " : "", filename.c_str());
593  Pass::call(design, stringf("dump -outfile %s %s", filename.c_str(), design->selected_active_module.empty() ? module->name.c_str() : ""));
594  }
const char * c_str() const
Definition: rtlil.h:178
std::string stringf(const char *fmt,...)
Definition: yosys.cc:58
int reduce_counter
Definition: freduce.cc:35
RTLIL::Design * design
Definition: freduce.cc:553
std::string dump_prefix
Definition: freduce.cc:37
RTLIL::Module * module
Definition: freduce.cc:554
RTLIL::IdString name
Definition: rtlil.h:599
static const char * id2cstr(const RTLIL::IdString &str)
Definition: rtlil.h:267
void log(const char *format,...)
Definition: log.cc:180
std::string selected_active_module
Definition: rtlil.h:511
static void call(RTLIL::Design *design, std::string command)
Definition: register.cc:146

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool FreduceWorker::find_bit_in_cone ( std::set< RTLIL::Cell * > &  celldone,
RTLIL::SigBit  needle,
RTLIL::SigBit  haystack 
)
inline

Definition at line 564 of file freduce.cc.

565  {
566  if (needle == haystack)
567  return true;
568  if (haystack.wire == NULL || needle.wire == NULL || drivers.count(haystack) == 0)
569  return false;
570 
571  std::pair<RTLIL::Cell*, std::set<RTLIL::SigBit>> &drv = drivers.at(haystack);
572 
573  if (celldone.count(drv.first))
574  return false;
575  celldone.insert(drv.first);
576 
577  for (auto &bit : drv.second)
578  if (find_bit_in_cone(celldone, needle, bit))
579  return true;
580  return false;
581  }
RTLIL::Wire * wire
Definition: rtlil.h:907
#define NULL
bool find_bit_in_cone(std::set< RTLIL::Cell * > &celldone, RTLIL::SigBit needle, RTLIL::SigBit haystack)
Definition: freduce.cc:564
drivers_t drivers
Definition: freduce.cc:557

+ Here is the caller graph for this function:

bool FreduceWorker::find_bit_in_cone ( RTLIL::SigBit  needle,
RTLIL::SigBit  haystack 
)
inline

Definition at line 583 of file freduce.cc.

584  {
585  std::set<RTLIL::Cell*> celldone;
586  return find_bit_in_cone(celldone, needle, haystack);
587  }
bool find_bit_in_cone(std::set< RTLIL::Cell * > &celldone, RTLIL::SigBit needle, RTLIL::SigBit haystack)
Definition: freduce.cc:564

+ Here is the call graph for this function:

int FreduceWorker::run ( )
inline

Definition at line 596 of file freduce.cc.

597  {
598  log("Running functional reduction on module %s:\n", RTLIL::id2cstr(module->name));
599 
600  CellTypes ct;
601  ct.setup_internals();
602  ct.setup_stdcells();
603 
604  int bits_full_total = 0;
605  std::vector<std::set<RTLIL::SigBit>> batches;
606  for (auto &it : module->wires_)
607  if (it.second->port_input) {
608  batches.push_back(sigmap(it.second).to_sigbit_set());
609  bits_full_total += it.second->width;
610  }
611  for (auto &it : module->cells_) {
612  if (ct.cell_known(it.second->type)) {
613  std::set<RTLIL::SigBit> inputs, outputs;
614  for (auto &port : it.second->connections()) {
615  std::vector<RTLIL::SigBit> bits = sigmap(port.second).to_sigbit_vector();
616  if (ct.cell_output(it.second->type, port.first))
617  outputs.insert(bits.begin(), bits.end());
618  else
619  inputs.insert(bits.begin(), bits.end());
620  }
621  std::pair<RTLIL::Cell*, std::set<RTLIL::SigBit>> drv(it.second, inputs);
622  for (auto &bit : outputs)
623  drivers[bit] = drv;
624  batches.push_back(outputs);
625  bits_full_total += outputs.size();
626  }
627  if (inv_mode && it.second->type == "$_NOT_")
628  inv_pairs.insert(std::pair<RTLIL::SigBit, RTLIL::SigBit>(sigmap(it.second->getPort("\\A")), sigmap(it.second->getPort("\\Y"))));
629  }
630 
631  int bits_count = 0;
632  int bits_full_count = 0;
633  std::map<std::vector<RTLIL::SigBit>, std::vector<RTLIL::SigBit>> buckets;
634  for (auto &batch : batches)
635  {
636  for (auto &bit : batch)
637  if (bit.wire != NULL && design->selected(module, bit.wire))
638  goto found_selected_wire;
639  bits_full_count += batch.size();
640  continue;
641 
642  found_selected_wire:
643  log(" Finding reduced input cone for signal batch %s%c\n",
644  log_signal(batch), verbose_level ? ':' : '.');
645 
646  FindReducedInputs infinder(sigmap, drivers);
647  for (auto &bit : batch) {
648  std::vector<RTLIL::SigBit> inputs;
649  infinder.analyze(inputs, bit, 100 * bits_full_count / bits_full_total);
650  buckets[inputs].push_back(bit);
651  bits_full_count++;
652  bits_count++;
653  }
654  }
655  log(" Sorted %d signal bits into %d buckets.\n", bits_count, int(buckets.size()));
656 
657  int bucket_count = 0;
658  std::vector<std::vector<equiv_bit_t>> equiv;
659  for (auto &bucket : buckets)
660  {
661  bucket_count++;
662 
663  if (bucket.second.size() == 1)
664  continue;
665 
666  if (bucket.first.size() == 0) {
667  log(" Finding const values for bucket %s%c\n", log_signal(bucket.second), verbose_level ? ':' : '.');
668  PerformReduction worker(sigmap, drivers, inv_pairs, bucket.second, bucket.first.size());
669  for (size_t idx = 0; idx < bucket.second.size(); idx++)
670  worker.analyze_const(equiv, idx);
671  } else {
672  log(" Trying to shatter bucket %s%c\n", log_signal(bucket.second), verbose_level ? ':' : '.');
673  PerformReduction worker(sigmap, drivers, inv_pairs, bucket.second, bucket.first.size());
674  worker.analyze(equiv, 100 * bucket_count / (buckets.size() + 1));
675  }
676  }
677 
678  std::map<RTLIL::SigBit, int> bitusage;
680 
681  if (!dump_prefix.empty())
682  dump();
683 
684  log(" Rewiring %d equivialent groups:\n", int(equiv.size()));
685  int rewired_sigbits = 0;
686  for (auto &grp : equiv)
687  {
688  log(" [%05d] Using as master for group: %s\n", ++reduce_counter, log_signal(grp.front().bit));
689 
690  RTLIL::SigSpec inv_sig;
691  for (size_t i = 1; i < grp.size(); i++)
692  {
693  if (!design->selected(module, grp[i].bit.wire)) {
694  log(" Skipping not-selected slave: %s\n", log_signal(grp[i].bit));
695  continue;
696  }
697 
698  if (grp[i].bit.wire->port_id == 0 && bitusage[grp[i].bit] <= 1) {
699  log(" Skipping unused slave: %s\n", log_signal(grp[i].bit));
700  continue;
701  }
702 
703  if (find_bit_in_cone(grp[i].bit, grp.front().bit)) {
704  log(" Skipping dependency of master: %s\n", log_signal(grp[i].bit));
705  continue;
706  }
707 
708  log(" Connect slave%s: %s\n", grp[i].inverted ? " using inverter" : "", log_signal(grp[i].bit));
709 
710  RTLIL::Cell *drv = drivers.at(grp[i].bit).first;
711  RTLIL::Wire *dummy_wire = module->addWire(NEW_ID);
712  for (auto &port : drv->connections_)
713  if (ct.cell_output(drv->type, port.first))
714  sigmap(port.second).replace(grp[i].bit, dummy_wire, &port.second);
715 
716  if (grp[i].inverted)
717  {
718  if (inv_sig.size() == 0)
719  {
720  inv_sig = module->addWire(NEW_ID);
721 
722  RTLIL::Cell *inv_cell = module->addCell(NEW_ID, "$_NOT_");
723  inv_cell->setPort("\\A", grp[0].bit);
724  inv_cell->setPort("\\Y", inv_sig);
725  }
726 
727  module->connect(RTLIL::SigSig(grp[i].bit, inv_sig));
728  }
729  else
730  module->connect(RTLIL::SigSig(grp[i].bit, grp[0].bit));
731 
732  rewired_sigbits++;
733  }
734 
735  if (!dump_prefix.empty())
736  dump();
737 
739  log(" Reached limit passed using -stop option. Skipping all further reductions.\n");
740  break;
741  }
742  }
743 
744  log(" Rewired a total of %d signal bits in module %s.\n", rewired_sigbits, RTLIL::id2cstr(module->name));
745  return rewired_sigbits;
746  }
bool selected(T1 *module) const
Definition: rtlil.h:551
RTLIL::Wire * wire(RTLIL::IdString id)
Definition: rtlil.h:637
void setup_stdcells()
Definition: celltypes.h:132
RTLIL::Cell * addCell(RTLIL::IdString name, RTLIL::IdString type)
Definition: rtlil.cc:1353
int reduce_counter
Definition: freduce.cc:35
CellTypes ct
Definition: opt_clean.cc:33
SigMap sigmap
Definition: freduce.cc:556
std::map< RTLIL::IdString, RTLIL::Wire * > wires_
Definition: rtlil.h:595
void rewrite_sigspecs(T functor)
Definition: rtlil.h:1166
const char * log_signal(const RTLIL::SigSpec &sig, bool autoint)
Definition: log.cc:269
static std::string idx(std::string str)
Definition: test_autotb.cc:57
void setPort(RTLIL::IdString portname, RTLIL::SigSpec signal)
Definition: rtlil.cc:1789
RTLIL::IdString type
Definition: rtlil.h:854
int size() const
Definition: rtlil.h:1019
void analyze_const(std::vector< std::vector< equiv_bit_t >> &results, int idx)
Definition: freduce.cc:294
RTLIL::Design * design
Definition: freduce.cc:553
USING_YOSYS_NAMESPACE PRIVATE_NAMESPACE_BEGIN bool inv_mode
Definition: freduce.cc:34
bool cell_known(RTLIL::IdString type)
Definition: celltypes.h:188
void connect(const RTLIL::SigSig &conn)
Definition: rtlil.cc:1278
std::string dump_prefix
Definition: freduce.cc:37
bool cell_output(RTLIL::IdString type, RTLIL::IdString port)
Definition: celltypes.h:193
RTLIL::Module * module
Definition: freduce.cc:554
RTLIL::Wire * addWire(RTLIL::IdString name, int width=1)
Definition: rtlil.cc:1331
RTLIL::IdString name
Definition: rtlil.h:599
#define NEW_ID
Definition: yosys.h:166
int verbose_level
Definition: freduce.cc:35
static const char * id2cstr(const RTLIL::IdString &str)
Definition: rtlil.h:267
#define NULL
std::map< RTLIL::IdString, RTLIL::Cell * > cells_
Definition: rtlil.h:596
void log(const char *format,...)
Definition: log.cc:180
bool find_bit_in_cone(std::set< RTLIL::Cell * > &celldone, RTLIL::SigBit needle, RTLIL::SigBit haystack)
Definition: freduce.cc:564
void setup_internals()
Definition: celltypes.h:83
std::map< RTLIL::IdString, RTLIL::SigSpec > connections_
Definition: rtlil.h:855
std::pair< SigSpec, SigSpec > SigSig
Definition: rtlil.h:71
int reduce_stop_at
Definition: freduce.cc:35
void analyze(std::vector< std::set< int >> &results, std::map< int, int > &results_map, std::vector< int > &bucket, std::string indent1, std::string indent2)
Definition: freduce.cc:338
std::set< std::pair< RTLIL::SigBit, RTLIL::SigBit > > inv_pairs
Definition: freduce.cc:558
void dump()
Definition: freduce.cc:589
drivers_t drivers
Definition: freduce.cc:557

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Field Documentation

RTLIL::Design* FreduceWorker::design

Definition at line 553 of file freduce.cc.

drivers_t FreduceWorker::drivers

Definition at line 557 of file freduce.cc.

std::set<std::pair<RTLIL::SigBit, RTLIL::SigBit> > FreduceWorker::inv_pairs

Definition at line 558 of file freduce.cc.

RTLIL::Module* FreduceWorker::module

Definition at line 554 of file freduce.cc.

SigMap FreduceWorker::sigmap

Definition at line 556 of file freduce.cc.


The documentation for this struct was generated from the following file: