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

Public Member Functions

void find_terminal_bits ()
 
int share_macc_ports (Macc::port_t &p1, Macc::port_t &p2, int w1, int w2, RTLIL::SigSpec act=RTLIL::SigSpec(), Macc *supermacc=nullptr, std::set< RTLIL::Cell * > *supercell_aux=nullptr)
 
int share_macc (RTLIL::Cell *c1, RTLIL::Cell *c2, RTLIL::SigSpec act=RTLIL::SigSpec(), RTLIL::Cell *supercell=nullptr, std::set< RTLIL::Cell * > *supercell_aux=nullptr)
 
void find_shareable_cells ()
 
bool is_shareable_pair (RTLIL::Cell *c1, RTLIL::Cell *c2)
 
void find_shareable_partners (std::vector< RTLIL::Cell * > &results, RTLIL::Cell *cell)
 
RTLIL::Cellmake_supercell (RTLIL::Cell *c1, RTLIL::Cell *c2, RTLIL::SigSpec act, std::set< RTLIL::Cell * > &supercell_aux)
 
const std::set< RTLIL::SigBit > & find_forbidden_controls (RTLIL::Cell *cell)
 
bool sort_check_activation_pattern (std::pair< RTLIL::SigSpec, RTLIL::Const > &p)
 
void optimize_activation_patterns (std::set< std::pair< RTLIL::SigSpec, RTLIL::Const >> &)
 
const std::set< std::pair
< RTLIL::SigSpec, RTLIL::Const > > & 
find_cell_activation_patterns (RTLIL::Cell *cell, const char *indent)
 
RTLIL::SigSpec bits_from_activation_patterns (const std::set< std::pair< RTLIL::SigSpec, RTLIL::Const >> &activation_patterns)
 
void filter_activation_patterns (std::set< std::pair< RTLIL::SigSpec, RTLIL::Const >> &out, const std::set< std::pair< RTLIL::SigSpec, RTLIL::Const >> &in, const std::set< RTLIL::SigBit > &filter_bits)
 
RTLIL::SigSpec make_cell_activation_logic (const std::set< std::pair< RTLIL::SigSpec, RTLIL::Const >> &activation_patterns, std::set< RTLIL::Cell * > &supercell_aux)
 
bool module_has_scc ()
 
bool find_in_input_cone_worker (RTLIL::Cell *root, RTLIL::Cell *needle, std::set< RTLIL::Cell * > &stop)
 
bool find_in_input_cone (RTLIL::Cell *root, RTLIL::Cell *needle)
 
bool is_part_of_scc (RTLIL::Cell *cell)
 
 ShareWorker (ShareWorkerConfig config, RTLIL::Design *design, RTLIL::Module *module)
 

Static Public Member Functions

static int bits_macc_port (const Macc::port_t &p, int width)
 
static int bits_macc (const Macc &m, int width)
 
static int bits_macc (RTLIL::Cell *c)
 
static bool cmp_macc_ports (const Macc::port_t &p1, const Macc::port_t &p2)
 

Data Fields

ShareWorkerConfig config
 
std::set< RTLIL::IdStringgeneric_ops
 
RTLIL::Designdesign
 
RTLIL::Modulemodule
 
CellTypes fwd_ct
 
CellTypes cone_ct
 
ModWalker modwalker
 
ModIndex mi
 
std::set< RTLIL::Cell * > cells_to_remove
 
std::set< RTLIL::Cell * > recursion_state
 
SigMap topo_sigmap
 
std::map< RTLIL::Cell
*, std::set< RTLIL::Cell
*, cell_ptr_cmp >
, cell_ptr_cmp
topo_cell_drivers
 
std::map< RTLIL::SigBit,
std::set< RTLIL::Cell
*, cell_ptr_cmp > > 
topo_bit_drivers
 
std::vector< std::pair
< RTLIL::SigBit, RTLIL::SigBit > > 
exclusive_ctrls
 
std::set< RTLIL::SigBitterminal_bits
 
std::set< RTLIL::Cell
*, RTLIL::sort_by_name_str
< RTLIL::Cell > > 
shareable_cells
 
std::map< RTLIL::Cell
*, std::set< RTLIL::SigBit > > 
forbidden_controls_cache
 
std::map< RTLIL::Cell
*, std::set< std::pair
< RTLIL::SigSpec, RTLIL::Const > > > 
activation_patterns_cache
 

Detailed Description

Definition at line 41 of file share.cc.

Constructor & Destructor Documentation

ShareWorker::ShareWorker ( ShareWorkerConfig  config,
RTLIL::Design design,
RTLIL::Module module 
)
inline

Definition at line 1052 of file share.cc.

1052  :
1053  config(config), design(design), module(module), mi(module)
1054  {
1055  bool before_scc = module_has_scc();
1056 
1057  generic_ops.insert(config.generic_uni_ops.begin(), config.generic_uni_ops.end());
1058  generic_ops.insert(config.generic_bin_ops.begin(), config.generic_bin_ops.end());
1059  generic_ops.insert(config.generic_cbin_ops.begin(), config.generic_cbin_ops.end());
1060  generic_ops.insert(config.generic_other_ops.begin(), config.generic_other_ops.end());
1061 
1063 
1065  cone_ct.cell_types.erase("$mul");
1066  cone_ct.cell_types.erase("$mod");
1067  cone_ct.cell_types.erase("$div");
1068  cone_ct.cell_types.erase("$pow");
1069  cone_ct.cell_types.erase("$shl");
1070  cone_ct.cell_types.erase("$shr");
1071  cone_ct.cell_types.erase("$sshl");
1072  cone_ct.cell_types.erase("$sshr");
1073 
1074  modwalker.setup(design, module);
1075 
1078 
1079  if (shareable_cells.size() < 2)
1080  return;
1081 
1082  log("Found %d cells in module %s that may be considered for resource sharing.\n",
1083  GetSize(shareable_cells), log_id(module));
1084 
1085  for (auto cell : module->cells())
1086  if (cell->type == "$pmux")
1087  for (auto bit : cell->getPort("\\S"))
1088  for (auto other_bit : cell->getPort("\\S"))
1089  if (bit < other_bit)
1090  exclusive_ctrls.push_back(std::pair<RTLIL::SigBit, RTLIL::SigBit>(bit, other_bit));
1091 
1092  while (!shareable_cells.empty() && config.limit != 0)
1093  {
1094  RTLIL::Cell *cell = *shareable_cells.begin();
1095  shareable_cells.erase(cell);
1096 
1097  log(" Analyzing resource sharing options for %s:\n", log_id(cell));
1098 
1099  const std::set<std::pair<RTLIL::SigSpec, RTLIL::Const>> &cell_activation_patterns = find_cell_activation_patterns(cell, " ");
1100  RTLIL::SigSpec cell_activation_signals = bits_from_activation_patterns(cell_activation_patterns);
1101 
1102  if (cell_activation_patterns.empty()) {
1103  log(" Cell is never active. Sharing is pointless, we simply remove it.\n");
1104  cells_to_remove.insert(cell);
1105  continue;
1106  }
1107 
1108  if (cell_activation_patterns.count(std::pair<RTLIL::SigSpec, RTLIL::Const>())) {
1109  log(" Cell is always active. Therefore no sharing is possible.\n");
1110  continue;
1111  }
1112 
1113  log(" Found %d activation_patterns using ctrl signal %s.\n", GetSize(cell_activation_patterns), log_signal(cell_activation_signals));
1114 
1115  std::vector<RTLIL::Cell*> candidates;
1116  find_shareable_partners(candidates, cell);
1117 
1118  if (candidates.empty()) {
1119  log(" No candidates found.\n");
1120  continue;
1121  }
1122 
1123  log(" Found %d candidates:", GetSize(candidates));
1124  for (auto c : candidates)
1125  log(" %s", log_id(c));
1126  log("\n");
1127 
1128  for (auto other_cell : candidates)
1129  {
1130  log(" Analyzing resource sharing with %s:\n", log_id(other_cell));
1131 
1132  const std::set<std::pair<RTLIL::SigSpec, RTLIL::Const>> &other_cell_activation_patterns = find_cell_activation_patterns(other_cell, " ");
1133  RTLIL::SigSpec other_cell_activation_signals = bits_from_activation_patterns(other_cell_activation_patterns);
1134 
1135  if (other_cell_activation_patterns.empty()) {
1136  log(" Cell is never active. Sharing is pointless, we simply remove it.\n");
1137  shareable_cells.erase(other_cell);
1138  cells_to_remove.insert(other_cell);
1139  continue;
1140  }
1141 
1142  if (other_cell_activation_patterns.count(std::pair<RTLIL::SigSpec, RTLIL::Const>())) {
1143  log(" Cell is always active. Therefore no sharing is possible.\n");
1144  shareable_cells.erase(other_cell);
1145  continue;
1146  }
1147 
1148  log(" Found %d activation_patterns using ctrl signal %s.\n",
1149  GetSize(other_cell_activation_patterns), log_signal(other_cell_activation_signals));
1150 
1151  const std::set<RTLIL::SigBit> &cell_forbidden_controls = find_forbidden_controls(cell);
1152  const std::set<RTLIL::SigBit> &other_cell_forbidden_controls = find_forbidden_controls(other_cell);
1153 
1154  std::set<RTLIL::SigBit> union_forbidden_controls;
1155  union_forbidden_controls.insert(cell_forbidden_controls.begin(), cell_forbidden_controls.end());
1156  union_forbidden_controls.insert(other_cell_forbidden_controls.begin(), other_cell_forbidden_controls.end());
1157 
1158  if (!union_forbidden_controls.empty())
1159  log(" Forbidden control signals for this pair of cells: %s\n", log_signal(union_forbidden_controls));
1160 
1161  std::set<std::pair<RTLIL::SigSpec, RTLIL::Const>> filtered_cell_activation_patterns;
1162  std::set<std::pair<RTLIL::SigSpec, RTLIL::Const>> filtered_other_cell_activation_patterns;
1163 
1164  filter_activation_patterns(filtered_cell_activation_patterns, cell_activation_patterns, union_forbidden_controls);
1165  filter_activation_patterns(filtered_other_cell_activation_patterns, other_cell_activation_patterns, union_forbidden_controls);
1166 
1167  optimize_activation_patterns(filtered_cell_activation_patterns);
1168  optimize_activation_patterns(filtered_other_cell_activation_patterns);
1169 
1170  ezDefaultSAT ez;
1171  SatGen satgen(&ez, &modwalker.sigmap);
1172 
1173  std::set<RTLIL::Cell*> sat_cells;
1174  std::set<RTLIL::SigBit> bits_queue;
1175 
1176  std::vector<int> cell_active, other_cell_active;
1177  RTLIL::SigSpec all_ctrl_signals;
1178 
1179  for (auto &p : filtered_cell_activation_patterns) {
1180  log(" Activation pattern for cell %s: %s = %s\n", log_id(cell), log_signal(p.first), log_signal(p.second));
1181  cell_active.push_back(ez.vec_eq(satgen.importSigSpec(p.first), satgen.importSigSpec(p.second)));
1182  all_ctrl_signals.append(p.first);
1183  }
1184 
1185  for (auto &p : filtered_other_cell_activation_patterns) {
1186  log(" Activation pattern for cell %s: %s = %s\n", log_id(other_cell), log_signal(p.first), log_signal(p.second));
1187  other_cell_active.push_back(ez.vec_eq(satgen.importSigSpec(p.first), satgen.importSigSpec(p.second)));
1188  all_ctrl_signals.append(p.first);
1189  }
1190 
1191  for (auto &bit : cell_activation_signals.to_sigbit_vector())
1192  bits_queue.insert(bit);
1193 
1194  for (auto &bit : other_cell_activation_signals.to_sigbit_vector())
1195  bits_queue.insert(bit);
1196 
1197  while (!bits_queue.empty())
1198  {
1199  std::set<ModWalker::PortBit> portbits;
1200  modwalker.get_drivers(portbits, bits_queue);
1201  bits_queue.clear();
1202 
1203  for (auto &pbit : portbits)
1204  if (sat_cells.count(pbit.cell) == 0 && cone_ct.cell_known(pbit.cell->type)) {
1205  if (config.opt_fast && modwalker.cell_outputs[pbit.cell].size() >= 4)
1206  continue;
1207  // log(" Adding cell %s (%s) to SAT problem.\n", log_id(pbit.cell), log_id(pbit.cell->type));
1208  bits_queue.insert(modwalker.cell_inputs[pbit.cell].begin(), modwalker.cell_inputs[pbit.cell].end());
1209  satgen.importCell(pbit.cell);
1210  sat_cells.insert(pbit.cell);
1211  }
1212 
1213  if (config.opt_fast && sat_cells.size() > 100)
1214  break;
1215  }
1216 
1217  for (auto it : exclusive_ctrls)
1218  if (satgen.importedSigBit(it.first) && satgen.importedSigBit(it.second)) {
1219  log(" Adding exclusive control bits: %s vs. %s\n", log_signal(it.first), log_signal(it.second));
1220  ez.assume(ez.NOT(ez.AND(satgen.importSigBit(it.first), satgen.importSigBit(it.second))));
1221  }
1222 
1223  if (!ez.solve(ez.expression(ez.OpOr, cell_active))) {
1224  log(" According to the SAT solver the cell %s is never active. Sharing is pointless, we simply remove it.\n", log_id(cell));
1225  cells_to_remove.insert(cell);
1226  break;
1227  }
1228 
1229  if (!ez.solve(ez.expression(ez.OpOr, other_cell_active))) {
1230  log(" According to the SAT solver the cell %s is never active. Sharing is pointless, we simply remove it.\n", log_id(other_cell));
1231  cells_to_remove.insert(other_cell);
1232  shareable_cells.erase(other_cell);
1233  continue;
1234  }
1235 
1236  ez.non_incremental();
1237 
1238  all_ctrl_signals.sort_and_unify();
1239  std::vector<int> sat_model = satgen.importSigSpec(all_ctrl_signals);
1240  std::vector<bool> sat_model_values;
1241 
1242  ez.assume(ez.AND(ez.expression(ez.OpOr, cell_active), ez.expression(ez.OpOr, other_cell_active)));
1243 
1244  log(" Size of SAT problem: %d cells, %d variables, %d clauses\n",
1245  GetSize(sat_cells), ez.numCnfVariables(), ez.numCnfClauses());
1246 
1247  if (ez.solve(sat_model, sat_model_values)) {
1248  log(" According to the SAT solver this pair of cells can not be shared.\n");
1249  log(" Model from SAT solver: %s = %d'", log_signal(all_ctrl_signals), GetSize(sat_model_values));
1250  for (int i = GetSize(sat_model_values)-1; i >= 0; i--)
1251  log("%c", sat_model_values[i] ? '1' : '0');
1252  log("\n");
1253  continue;
1254  }
1255 
1256  log(" According to the SAT solver this pair of cells can be shared.\n");
1257 
1258  if (find_in_input_cone(cell, other_cell)) {
1259  log(" Sharing not possible: %s is in input cone of %s.\n", log_id(other_cell), log_id(cell));
1260  continue;
1261  }
1262 
1263  if (find_in_input_cone(other_cell, cell)) {
1264  log(" Sharing not possible: %s is in input cone of %s.\n", log_id(cell), log_id(other_cell));
1265  continue;
1266  }
1267 
1268  shareable_cells.erase(other_cell);
1269 
1270  int cell_select_score = 0;
1271  int other_cell_select_score = 0;
1272 
1273  for (auto &p : filtered_cell_activation_patterns)
1274  cell_select_score += p.first.size();
1275 
1276  for (auto &p : filtered_other_cell_activation_patterns)
1277  other_cell_select_score += p.first.size();
1278 
1279  RTLIL::Cell *supercell;
1280  std::set<RTLIL::Cell*> supercell_aux;
1281  if (cell_select_score <= other_cell_select_score) {
1282  RTLIL::SigSpec act = make_cell_activation_logic(filtered_cell_activation_patterns, supercell_aux);
1283  supercell = make_supercell(cell, other_cell, act, supercell_aux);
1284  log(" Activation signal for %s: %s\n", log_id(cell), log_signal(act));
1285  } else {
1286  RTLIL::SigSpec act = make_cell_activation_logic(filtered_other_cell_activation_patterns, supercell_aux);
1287  supercell = make_supercell(other_cell, cell, act, supercell_aux);
1288  log(" Activation signal for %s: %s\n", log_id(other_cell), log_signal(act));
1289  }
1290 
1291  log(" New cell: %s (%s)\n", log_id(supercell), log_id(supercell->type));
1292 
1293  cells_to_remove.insert(cell);
1294  cells_to_remove.insert(other_cell);
1295 
1296  for (auto c : supercell_aux)
1297  if (is_part_of_scc(c))
1298  goto do_rollback;
1299 
1300  if (0) {
1301  do_rollback:
1302  log(" New topology contains loops! Rolling back..\n");
1303  cells_to_remove.erase(cell);
1304  cells_to_remove.erase(other_cell);
1305  shareable_cells.insert(other_cell);
1306  for (auto cc : supercell_aux)
1307  module->remove(cc);
1308  continue;
1309  }
1310 
1311  std::set<std::pair<RTLIL::SigSpec, RTLIL::Const>> supercell_activation_patterns;
1312  supercell_activation_patterns.insert(filtered_cell_activation_patterns.begin(), filtered_cell_activation_patterns.end());
1313  supercell_activation_patterns.insert(filtered_other_cell_activation_patterns.begin(), filtered_other_cell_activation_patterns.end());
1314  optimize_activation_patterns(supercell_activation_patterns);
1315  activation_patterns_cache[supercell] = supercell_activation_patterns;
1316  shareable_cells.insert(supercell);
1317 
1318  for (auto bit : topo_sigmap(all_ctrl_signals))
1319  for (auto c : topo_bit_drivers[bit])
1320  topo_cell_drivers[supercell].insert(c);
1321 
1322  topo_cell_drivers[supercell].insert(topo_cell_drivers[cell].begin(), topo_cell_drivers[cell].end());
1323  topo_cell_drivers[supercell].insert(topo_cell_drivers[other_cell].begin(), topo_cell_drivers[other_cell].end());
1324 
1325  topo_cell_drivers[cell] = { supercell };
1326  topo_cell_drivers[other_cell] = { supercell };
1327 
1328  if (config.limit > 0)
1329  config.limit--;
1330 
1331  break;
1332  }
1333  }
1334 
1335  if (!cells_to_remove.empty()) {
1336  log("Removing %d cells in module %s:\n", GetSize(cells_to_remove), log_id(module));
1337  for (auto c : cells_to_remove) {
1338  log(" Removing cell %s (%s).\n", log_id(c), log_id(c->type));
1339  module->remove(c);
1340  }
1341  }
1342 
1343  log_assert(recursion_state.empty());
1344 
1345  bool after_scc = before_scc || module_has_scc();
1346  log_assert(before_scc == after_scc);
1347  }
bool find_in_input_cone(RTLIL::Cell *root, RTLIL::Cell *needle)
Definition: share.cc:1002
void find_terminal_bits()
Definition: share.cc:69
std::map< RTLIL::Cell *, std::set< RTLIL::SigBit > > cell_outputs
Definition: modtools.h:208
void find_shareable_cells()
Definition: share.cc:350
RTLIL::Design * design
Definition: share.cc:46
ShareWorkerConfig config
Definition: share.cc:43
SigMap sigmap
Definition: modtools.h:202
std::map< RTLIL::Cell *, std::set< RTLIL::Cell *, cell_ptr_cmp >, cell_ptr_cmp > topo_cell_drivers
Definition: share.cc:57
std::set< RTLIL::IdString > generic_other_ops
Definition: share.cc:38
bool is_part_of_scc(RTLIL::Cell *cell)
Definition: share.cc:1008
const char * log_signal(const RTLIL::SigSpec &sig, bool autoint)
Definition: log.cc:269
ezMiniSAT ez
Definition: puzzle3d.cc:31
RTLIL::IdString type
Definition: rtlil.h:854
std::set< RTLIL::IdString > generic_uni_ops
Definition: share.cc:38
std::set< RTLIL::Cell * > recursion_state
Definition: share.cc:54
bool get_drivers(std::set< PortBit > &result, RTLIL::SigBit bit) const
Definition: modtools.h:289
const std::set< std::pair< RTLIL::SigSpec, RTLIL::Const > > & find_cell_activation_patterns(RTLIL::Cell *cell, const char *indent)
Definition: share.cc:795
void filter_activation_patterns(std::set< std::pair< RTLIL::SigSpec, RTLIL::Const >> &out, const std::set< std::pair< RTLIL::SigSpec, RTLIL::Const >> &in, const std::set< RTLIL::SigBit > &filter_bits)
Definition: share.cc:896
YOSYS_NAMESPACE_BEGIN typedef ezMiniSAT ezDefaultSAT
Definition: satgen.h:32
std::map< RTLIL::IdString, CellType > cell_types
Definition: celltypes.h:36
RTLIL::Module * module
Definition: share.cc:47
bool cell_known(RTLIL::IdString type)
Definition: celltypes.h:188
ModIndex mi
Definition: share.cc:51
std::map< RTLIL::SigBit, std::set< RTLIL::Cell *, cell_ptr_cmp > > topo_bit_drivers
Definition: share.cc:58
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
#define log_assert(_assert_expr_)
Definition: log.h:85
SigMap topo_sigmap
Definition: share.cc:56
std::map< RTLIL::Cell *, std::set< RTLIL::SigBit > > cell_inputs
Definition: modtools.h:208
CellTypes fwd_ct
Definition: share.cc:49
std::map< RTLIL::Cell *, std::set< std::pair< RTLIL::SigSpec, RTLIL::Const > > > activation_patterns_cache
Definition: share.cc:763
Definition: satgen.h:34
ModWalker modwalker
Definition: share.cc:50
void optimize_activation_patterns(std::set< std::pair< RTLIL::SigSpec, RTLIL::Const >> &)
Definition: share.cc:789
std::set< RTLIL::IdString > generic_cbin_ops
Definition: share.cc:38
RTLIL::ObjRange< RTLIL::Cell * > cells()
Definition: rtlil.h:641
std::set< RTLIL::Cell *, RTLIL::sort_by_name_str< RTLIL::Cell > > shareable_cells
Definition: share.cc:348
void sort_and_unify()
Definition: rtlil.cc:2291
void setup(RTLIL::Design *design, RTLIL::Module *module, CellTypes *filter_ct=NULL)
Definition: modtools.h:265
void remove(const std::set< RTLIL::Wire * > &wires)
Definition: rtlil.cc:1158
std::set< RTLIL::Cell * > cells_to_remove
Definition: share.cc:53
void log(const char *format,...)
Definition: log.cc:180
RTLIL::SigSpec bits_from_activation_patterns(const std::set< std::pair< RTLIL::SigSpec, RTLIL::Const >> &activation_patterns)
Definition: share.cc:881
void setup_internals()
Definition: celltypes.h:83
RTLIL::SigSpec make_cell_activation_logic(const std::set< std::pair< RTLIL::SigSpec, RTLIL::Const >> &activation_patterns, std::set< RTLIL::Cell * > &supercell_aux)
Definition: share.cc:914
bool module_has_scc()
Definition: share.cc:936
void append(const RTLIL::SigSpec &signal)
Definition: rtlil.cc:2523
RTLIL::Cell * make_supercell(RTLIL::Cell *c1, RTLIL::Cell *c2, RTLIL::SigSpec act, std::set< RTLIL::Cell * > &supercell_aux)
Definition: share.cc:504
CellTypes cone_ct
Definition: share.cc:49
void find_shareable_partners(std::vector< RTLIL::Cell * > &results, RTLIL::Cell *cell)
Definition: share.cc:491
const char * log_id(RTLIL::IdString str)
Definition: log.cc:283
std::vector< std::pair< RTLIL::SigBit, RTLIL::SigBit > > exclusive_ctrls
Definition: share.cc:60
std::vector< RTLIL::SigBit > to_sigbit_vector() const
Definition: rtlil.cc:2921
std::set< RTLIL::IdString > generic_ops
Definition: share.cc:44
const std::set< RTLIL::SigBit > & find_forbidden_controls(RTLIL::Cell *cell)
Definition: share.cc:723
std::set< RTLIL::IdString > generic_bin_ops
Definition: share.cc:38

+ Here is the call graph for this function:

Member Function Documentation

RTLIL::SigSpec ShareWorker::bits_from_activation_patterns ( const std::set< std::pair< RTLIL::SigSpec, RTLIL::Const >> &  activation_patterns)
inline

Definition at line 881 of file share.cc.

882  {
883  std::set<RTLIL::SigBit> all_bits;
884  for (auto &it : activation_patterns) {
885  std::vector<RTLIL::SigBit> bits = it.first;
886  all_bits.insert(bits.begin(), bits.end());
887  }
888 
889  RTLIL::SigSpec signal;
890  for (auto &bit : all_bits)
891  signal.append_bit(bit);
892 
893  return signal;
894  }
void append_bit(const RTLIL::SigBit &bit)
Definition: rtlil.cc:2562

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int ShareWorker::bits_macc ( const Macc m,
int  width 
)
inlinestatic

Definition at line 119 of file share.cc.

120  {
121  int bits = GetSize(m.bit_ports);
122  for (auto &p : m.ports)
123  bits += bits_macc_port(p, width);
124  return bits;
125  }
RTLIL::SigSpec bit_ports
Definition: macc.h:35
std::vector< port_t > ports
Definition: macc.h:34
static int bits_macc_port(const Macc::port_t &p, int width)
Definition: share.cc:112
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int ShareWorker::bits_macc ( RTLIL::Cell c)
inlinestatic

Definition at line 127 of file share.cc.

128  {
129  Macc m(c);
130  int width = GetSize(c->getPort("\\Y"));
131  return bits_macc(m, width);
132  }
Definition: macc.h:27
static int bits_macc(const Macc &m, int width)
Definition: share.cc:119
const RTLIL::SigSpec & getPort(RTLIL::IdString portname) const
Definition: rtlil.cc:1809
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334

+ Here is the call graph for this function:

static int ShareWorker::bits_macc_port ( const Macc::port_t p,
int  width 
)
inlinestatic

Definition at line 112 of file share.cc.

113  {
114  if (GetSize(p.in_a) == 0 || GetSize(p.in_b) == 0)
115  return std::min(std::max(GetSize(p.in_a), GetSize(p.in_b)), width);
116  return std::min(GetSize(p.in_a), width) * std::min(GetSize(p.in_b), width) / 2;
117  }
RTLIL::SigSpec in_b
Definition: macc.h:30
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
RTLIL::SigSpec in_a
Definition: macc.h:30

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static bool ShareWorker::cmp_macc_ports ( const Macc::port_t p1,
const Macc::port_t p2 
)
inlinestatic

Definition at line 134 of file share.cc.

135  {
136  bool mul1 = GetSize(p1.in_a) && GetSize(p1.in_b);
137  bool mul2 = GetSize(p2.in_a) && GetSize(p2.in_b);
138 
139  int w1 = mul1 ? GetSize(p1.in_a) * GetSize(p1.in_b) : GetSize(p1.in_a) + GetSize(p1.in_b);
140  int w2 = mul2 ? GetSize(p2.in_a) * GetSize(p2.in_b) : GetSize(p2.in_a) + GetSize(p2.in_b);
141 
142  if (mul1 != mul2)
143  return mul1;
144 
145  if (w1 != w2)
146  return w1 > w2;
147 
148  if (p1.is_signed != p2.is_signed)
149  return p1.is_signed < p2.is_signed;
150 
151  if (p1.do_subtract != p2.do_subtract)
152  return p1.do_subtract < p2.do_subtract;
153 
154  if (p1.in_a != p2.in_a)
155  return p1.in_a < p2.in_a;
156 
157  if (p1.in_b != p2.in_b)
158  return p1.in_b < p2.in_b;
159 
160  return false;
161  }
RTLIL::SigSpec in_b
Definition: macc.h:30
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
bool is_signed
Definition: macc.h:31
RTLIL::SigSpec in_a
Definition: macc.h:30
bool do_subtract
Definition: macc.h:31

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void ShareWorker::filter_activation_patterns ( std::set< std::pair< RTLIL::SigSpec, RTLIL::Const >> &  out,
const std::set< std::pair< RTLIL::SigSpec, RTLIL::Const >> &  in,
const std::set< RTLIL::SigBit > &  filter_bits 
)
inline

Definition at line 896 of file share.cc.

898  {
899  for (auto &p : in)
900  {
901  std::vector<RTLIL::SigBit> p_first = p.first;
902  std::pair<RTLIL::SigSpec, RTLIL::Const> new_p;
903 
904  for (int i = 0; i < GetSize(p_first); i++)
905  if (filter_bits.count(p_first[i]) == 0) {
906  new_p.first.append_bit(p_first[i]);
907  new_p.second.bits.push_back(p.second.bits.at(i));
908  }
909 
910  out.insert(new_p);
911  }
912  }
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

const std::set<std::pair<RTLIL::SigSpec, RTLIL::Const> >& ShareWorker::find_cell_activation_patterns ( RTLIL::Cell cell,
const char *  indent 
)
inline

Definition at line 795 of file share.cc.

796  {
797  if (recursion_state.count(cell)) {
798  static std::set<std::pair<RTLIL::SigSpec, RTLIL::Const>> empty_patterns_set;
799  return empty_patterns_set;
800  }
801 
802  if (activation_patterns_cache.count(cell))
803  return activation_patterns_cache.at(cell);
804 
805  const std::set<RTLIL::SigBit> &cell_out_bits = modwalker.cell_outputs[cell];
806  std::set<RTLIL::Cell*> driven_cells, driven_data_muxes;
807 
808  for (auto &bit : cell_out_bits)
809  {
810  if (terminal_bits.count(bit)) {
811  // Terminal cells are always active: unconditional activation pattern
812  activation_patterns_cache[cell].insert(std::pair<RTLIL::SigSpec, RTLIL::Const>());
813  return activation_patterns_cache.at(cell);
814  }
815  for (auto &pbit : modwalker.signal_consumers[bit]) {
816  log_assert(fwd_ct.cell_known(pbit.cell->type));
817  if ((pbit.cell->type == "$mux" || pbit.cell->type == "$pmux") && (pbit.port == "\\A" || pbit.port == "\\B"))
818  driven_data_muxes.insert(pbit.cell);
819  else
820  driven_cells.insert(pbit.cell);
821  }
822  }
823 
824  recursion_state.insert(cell);
825 
826  for (auto c : driven_data_muxes)
827  {
828  const std::set<std::pair<RTLIL::SigSpec, RTLIL::Const>> &c_patterns = find_cell_activation_patterns(c, indent);
829 
830  bool used_in_a = false;
831  std::set<int> used_in_b_parts;
832 
833  int width = c->parameters.at("\\WIDTH").as_int();
834  std::vector<RTLIL::SigBit> sig_a = modwalker.sigmap(c->getPort("\\A"));
835  std::vector<RTLIL::SigBit> sig_b = modwalker.sigmap(c->getPort("\\B"));
836  std::vector<RTLIL::SigBit> sig_s = modwalker.sigmap(c->getPort("\\S"));
837 
838  for (auto &bit : sig_a)
839  if (cell_out_bits.count(bit))
840  used_in_a = true;
841 
842  for (int i = 0; i < GetSize(sig_b); i++)
843  if (cell_out_bits.count(sig_b[i]))
844  used_in_b_parts.insert(i / width);
845 
846  if (used_in_a)
847  for (auto p : c_patterns) {
848  for (int i = 0; i < GetSize(sig_s); i++)
849  p.first.append_bit(sig_s[i]), p.second.bits.push_back(RTLIL::State::S0);
851  activation_patterns_cache[cell].insert(p);
852  }
853 
854  for (int idx : used_in_b_parts)
855  for (auto p : c_patterns) {
856  p.first.append_bit(sig_s[idx]), p.second.bits.push_back(RTLIL::State::S1);
858  activation_patterns_cache[cell].insert(p);
859  }
860  }
861 
862  for (auto c : driven_cells) {
863  const std::set<std::pair<RTLIL::SigSpec, RTLIL::Const>> &c_patterns = find_cell_activation_patterns(c, indent);
864  activation_patterns_cache[cell].insert(c_patterns.begin(), c_patterns.end());
865  }
866 
867  log_assert(recursion_state.count(cell) != 0);
868  recursion_state.erase(cell);
869 
871  if (activation_patterns_cache[cell].empty()) {
872  log("%sFound cell that is never activated: %s\n", indent, log_id(cell));
873  RTLIL::SigSpec cell_outputs = modwalker.cell_outputs[cell];
874  module->connect(RTLIL::SigSig(cell_outputs, RTLIL::SigSpec(RTLIL::State::Sx, cell_outputs.size())));
875  cells_to_remove.insert(cell);
876  }
877 
878  return activation_patterns_cache[cell];
879  }
std::map< RTLIL::SigBit, std::set< PortBit > > signal_consumers
Definition: modtools.h:205
std::map< RTLIL::Cell *, std::set< RTLIL::SigBit > > cell_outputs
Definition: modtools.h:208
std::map< bitDef_t, shared_bit_data_t * > bits
Definition: sigtools.h:224
std::set< RTLIL::SigBit > terminal_bits
Definition: share.cc:67
SigMap sigmap
Definition: modtools.h:202
static std::string idx(std::string str)
Definition: test_autotb.cc:57
int size() const
Definition: rtlil.h:1019
std::set< RTLIL::Cell * > recursion_state
Definition: share.cc:54
const std::set< std::pair< RTLIL::SigSpec, RTLIL::Const > > & find_cell_activation_patterns(RTLIL::Cell *cell, const char *indent)
Definition: share.cc:795
RTLIL::Module * module
Definition: share.cc:47
bool cell_known(RTLIL::IdString type)
Definition: celltypes.h:188
void connect(const RTLIL::SigSig &conn)
Definition: rtlil.cc:1278
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
#define log_assert(_assert_expr_)
Definition: log.h:85
CellTypes fwd_ct
Definition: share.cc:49
std::map< RTLIL::Cell *, std::set< std::pair< RTLIL::SigSpec, RTLIL::Const > > > activation_patterns_cache
Definition: share.cc:763
ModWalker modwalker
Definition: share.cc:50
void optimize_activation_patterns(std::set< std::pair< RTLIL::SigSpec, RTLIL::Const >> &)
Definition: share.cc:789
std::set< RTLIL::Cell * > cells_to_remove
Definition: share.cc:53
void log(const char *format,...)
Definition: log.cc:180
std::pair< SigSpec, SigSpec > SigSig
Definition: rtlil.h:71
const char * log_id(RTLIL::IdString str)
Definition: log.cc:283
bool sort_check_activation_pattern(std::pair< RTLIL::SigSpec, RTLIL::Const > &p)
Definition: share.cc:765

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

const std::set<RTLIL::SigBit>& ShareWorker::find_forbidden_controls ( RTLIL::Cell cell)
inline

Definition at line 723 of file share.cc.

724  {
725  if (recursion_state.count(cell)) {
726  static std::set<RTLIL::SigBit> empty_controls_set;
727  return empty_controls_set;
728  }
729 
730  if (forbidden_controls_cache.count(cell))
731  return forbidden_controls_cache.at(cell);
732 
733  std::set<ModWalker::PortBit> pbits;
734  std::set<RTLIL::Cell*> consumer_cells;
735 
737 
738  for (auto &bit : pbits) {
739  if ((bit.cell->type == "$mux" || bit.cell->type == "$pmux") && bit.port == "\\S")
740  forbidden_controls_cache[cell].insert(bit.cell->getPort("\\S").extract(bit.offset, 1));
741  consumer_cells.insert(bit.cell);
742  }
743 
744  recursion_state.insert(cell);
745 
746  for (auto c : consumer_cells)
747  if (fwd_ct.cell_known(c->type)) {
748  const std::set<RTLIL::SigBit> &bits = find_forbidden_controls(c);
749  forbidden_controls_cache[cell].insert(bits.begin(), bits.end());
750  }
751 
752  log_assert(recursion_state.count(cell) != 0);
753  recursion_state.erase(cell);
754 
755  return forbidden_controls_cache[cell];
756  }
std::map< RTLIL::Cell *, std::set< RTLIL::SigBit > > cell_outputs
Definition: modtools.h:208
std::set< RTLIL::Cell * > recursion_state
Definition: share.cc:54
bool get_consumers(std::set< PortBit > &result, RTLIL::SigBit bit) const
Definition: modtools.h:301
bool cell_known(RTLIL::IdString type)
Definition: celltypes.h:188
#define log_assert(_assert_expr_)
Definition: log.h:85
CellTypes fwd_ct
Definition: share.cc:49
ModWalker modwalker
Definition: share.cc:50
std::map< RTLIL::Cell *, std::set< RTLIL::SigBit > > forbidden_controls_cache
Definition: share.cc:721
const std::set< RTLIL::SigBit > & find_forbidden_controls(RTLIL::Cell *cell)
Definition: share.cc:723

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool ShareWorker::find_in_input_cone ( RTLIL::Cell root,
RTLIL::Cell needle 
)
inline

Definition at line 1002 of file share.cc.

1003  {
1004  std::set<RTLIL::Cell*> stop;
1005  return find_in_input_cone_worker(root, needle, stop);
1006  }
bool find_in_input_cone_worker(RTLIL::Cell *root, RTLIL::Cell *needle, std::set< RTLIL::Cell * > &stop)
Definition: share.cc:986

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool ShareWorker::find_in_input_cone_worker ( RTLIL::Cell root,
RTLIL::Cell needle,
std::set< RTLIL::Cell * > &  stop 
)
inline

Definition at line 986 of file share.cc.

987  {
988  if (root == needle)
989  return true;
990 
991  if (stop.count(root))
992  return false;
993 
994  stop.insert(root);
995 
996  for (auto c : topo_cell_drivers[root])
997  if (find_in_input_cone_worker(c, needle, stop))
998  return true;
999  return false;
1000  }
std::map< RTLIL::Cell *, std::set< RTLIL::Cell *, cell_ptr_cmp >, cell_ptr_cmp > topo_cell_drivers
Definition: share.cc:57
bool find_in_input_cone_worker(RTLIL::Cell *root, RTLIL::Cell *needle, std::set< RTLIL::Cell * > &stop)
Definition: share.cc:986

+ Here is the caller graph for this function:

void ShareWorker::find_shareable_cells ( )
inline

Definition at line 350 of file share.cc.

351  {
352  for (auto cell : module->cells())
353  {
354  if (!design->selected(module, cell) || !modwalker.ct.cell_known(cell->type))
355  continue;
356 
357  for (auto &bit : modwalker.cell_outputs[cell])
358  if (terminal_bits.count(bit))
359  goto not_a_muxed_cell;
360 
361  if (0)
362  not_a_muxed_cell:
363  continue;
364 
365  if (config.opt_force) {
366  shareable_cells.insert(cell);
367  continue;
368  }
369 
370  if (cell->type == "$memrd") {
371  if (!cell->parameters.at("\\CLK_ENABLE").as_bool())
372  shareable_cells.insert(cell);
373  continue;
374  }
375 
376  if (cell->type == "$mul" || cell->type == "$div" || cell->type == "$mod") {
377  if (config.opt_aggressive || cell->parameters.at("\\Y_WIDTH").as_int() >= 4)
378  shareable_cells.insert(cell);
379  continue;
380  }
381 
382  if (cell->type == "$shl" || cell->type == "$shr" || cell->type == "$sshl" || cell->type == "$sshr") {
383  if (config.opt_aggressive || cell->parameters.at("\\Y_WIDTH").as_int() >= 8)
384  shareable_cells.insert(cell);
385  continue;
386  }
387 
388  if (generic_ops.count(cell->type)) {
389  if (config.opt_aggressive || cell->parameters.at("\\Y_WIDTH").as_int() >= 10)
390  shareable_cells.insert(cell);
391  continue;
392  }
393  }
394  }
bool selected(T1 *module) const
Definition: rtlil.h:551
std::map< RTLIL::Cell *, std::set< RTLIL::SigBit > > cell_outputs
Definition: modtools.h:208
RTLIL::Design * design
Definition: share.cc:46
ShareWorkerConfig config
Definition: share.cc:43
std::set< RTLIL::SigBit > terminal_bits
Definition: share.cc:67
RTLIL::Module * module
Definition: share.cc:47
bool cell_known(RTLIL::IdString type)
Definition: celltypes.h:188
bool opt_aggressive
Definition: share.cc:36
ModWalker modwalker
Definition: share.cc:50
RTLIL::ObjRange< RTLIL::Cell * > cells()
Definition: rtlil.h:641
std::set< RTLIL::Cell *, RTLIL::sort_by_name_str< RTLIL::Cell > > shareable_cells
Definition: share.cc:348
CellTypes ct
Definition: modtools.h:201
bool opt_force
Definition: share.cc:35
std::set< RTLIL::IdString > generic_ops
Definition: share.cc:44

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void ShareWorker::find_shareable_partners ( std::vector< RTLIL::Cell * > &  results,
RTLIL::Cell cell 
)
inline

Definition at line 491 of file share.cc.

492  {
493  results.clear();
494  for (auto c : shareable_cells)
495  if (c != cell && is_shareable_pair(c, cell))
496  results.push_back(c);
497  }
bool is_shareable_pair(RTLIL::Cell *c1, RTLIL::Cell *c2)
Definition: share.cc:396
std::set< RTLIL::Cell *, RTLIL::sort_by_name_str< RTLIL::Cell > > shareable_cells
Definition: share.cc:348

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void ShareWorker::find_terminal_bits ( )
inline

Definition at line 69 of file share.cc.

70  {
71  std::set<RTLIL::SigBit> queue_bits;
72  std::set<RTLIL::Cell*> visited_cells;
73 
74  queue_bits.insert(modwalker.signal_outputs.begin(), modwalker.signal_outputs.end());
75 
76  for (auto &it : module->cells_)
77  if (!fwd_ct.cell_known(it.second->type)) {
78  std::set<RTLIL::SigBit> &bits = modwalker.cell_inputs[it.second];
79  queue_bits.insert(bits.begin(), bits.end());
80  }
81 
82  terminal_bits.insert(queue_bits.begin(), queue_bits.end());
83 
84  while (!queue_bits.empty())
85  {
86  std::set<ModWalker::PortBit> portbits;
87  modwalker.get_drivers(portbits, queue_bits);
88  queue_bits.clear();
89 
90  for (auto &pbit : portbits) {
91  if (pbit.cell->type == "$mux" || pbit.cell->type == "$pmux") {
92  std::set<RTLIL::SigBit> bits = modwalker.sigmap(pbit.cell->getPort("\\S")).to_sigbit_set();
93  terminal_bits.insert(bits.begin(), bits.end());
94  queue_bits.insert(bits.begin(), bits.end());
95  visited_cells.insert(pbit.cell);
96  }
97  if (fwd_ct.cell_known(pbit.cell->type) && visited_cells.count(pbit.cell) == 0) {
98  std::set<RTLIL::SigBit> &bits = modwalker.cell_inputs[pbit.cell];
99  terminal_bits.insert(bits.begin(), bits.end());
100  queue_bits.insert(bits.begin(), bits.end());
101  visited_cells.insert(pbit.cell);
102  }
103  }
104  }
105  }
std::set< RTLIL::SigBit > signal_outputs
Definition: modtools.h:206
std::set< RTLIL::SigBit > terminal_bits
Definition: share.cc:67
SigMap sigmap
Definition: modtools.h:202
bool get_drivers(std::set< PortBit > &result, RTLIL::SigBit bit) const
Definition: modtools.h:289
RTLIL::Module * module
Definition: share.cc:47
bool cell_known(RTLIL::IdString type)
Definition: celltypes.h:188
std::map< RTLIL::Cell *, std::set< RTLIL::SigBit > > cell_inputs
Definition: modtools.h:208
CellTypes fwd_ct
Definition: share.cc:49
ModWalker modwalker
Definition: share.cc:50
std::map< RTLIL::IdString, RTLIL::Cell * > cells_
Definition: rtlil.h:596

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool ShareWorker::is_part_of_scc ( RTLIL::Cell cell)
inline

Definition at line 1008 of file share.cc.

1009  {
1010  CellTypes ct;
1011  ct.setup_internals();
1012  ct.setup_stdcells();
1013 
1014  std::set<RTLIL::Cell*> queue, covered;
1015  queue.insert(cell);
1016 
1017  while (!queue.empty())
1018  {
1019  std::set<RTLIL::Cell*> new_queue;
1020 
1021  for (auto c : queue) {
1022  if (!ct.cell_known(c->type))
1023  continue;
1024  for (auto &conn : c->connections())
1025  if (ct.cell_input(c->type, conn.first))
1026  for (auto bit : conn.second)
1027  for (auto &pi : mi.query_ports(bit))
1028  if (ct.cell_known(pi.cell->type) && ct.cell_output(pi.cell->type, pi.port))
1029  new_queue.insert(pi.cell);
1030  covered.insert(c);
1031  }
1032 
1033  queue.clear();
1034  for (auto c : new_queue) {
1035  if (cells_to_remove.count(c))
1036  continue;
1037  if (c == cell)
1038  return true;
1039  if (!covered.count(c))
1040  queue.insert(c);
1041  }
1042  }
1043 
1044  return false;
1045  }
void setup_stdcells()
Definition: celltypes.h:132
CellTypes ct
Definition: opt_clean.cc:33
bool cell_known(RTLIL::IdString type)
Definition: celltypes.h:188
ModIndex mi
Definition: share.cc:51
bool cell_output(RTLIL::IdString type, RTLIL::IdString port)
Definition: celltypes.h:193
std::set< RTLIL::Cell * > cells_to_remove
Definition: share.cc:53
void setup_internals()
Definition: celltypes.h:83
bool cell_input(RTLIL::IdString type, RTLIL::IdString port)
Definition: celltypes.h:199
std::set< PortInfo > & query_ports(RTLIL::SigBit bit)
Definition: modtools.h:171

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool ShareWorker::is_shareable_pair ( RTLIL::Cell c1,
RTLIL::Cell c2 
)
inline

Definition at line 396 of file share.cc.

397  {
398  if (c1->type != c2->type)
399  return false;
400 
401  if (c1->type == "$memrd")
402  {
403  if (c1->parameters.at("\\MEMID").decode_string() != c2->parameters.at("\\MEMID").decode_string())
404  return false;
405 
406  return true;
407  }
408 
409  if (config.generic_uni_ops.count(c1->type))
410  {
411  if (!config.opt_aggressive)
412  {
413  int a1_width = c1->parameters.at("\\A_WIDTH").as_int();
414  int y1_width = c1->parameters.at("\\Y_WIDTH").as_int();
415 
416  int a2_width = c2->parameters.at("\\A_WIDTH").as_int();
417  int y2_width = c2->parameters.at("\\Y_WIDTH").as_int();
418 
419  if (std::max(a1_width, a2_width) > 2 * std::min(a1_width, a2_width)) return false;
420  if (std::max(y1_width, y2_width) > 2 * std::min(y1_width, y2_width)) return false;
421  }
422 
423  return true;
424  }
425 
426  if (config.generic_bin_ops.count(c1->type) || c1->type == "$alu")
427  {
428  if (!config.opt_aggressive)
429  {
430  int a1_width = c1->parameters.at("\\A_WIDTH").as_int();
431  int b1_width = c1->parameters.at("\\B_WIDTH").as_int();
432  int y1_width = c1->parameters.at("\\Y_WIDTH").as_int();
433 
434  int a2_width = c2->parameters.at("\\A_WIDTH").as_int();
435  int b2_width = c2->parameters.at("\\B_WIDTH").as_int();
436  int y2_width = c2->parameters.at("\\Y_WIDTH").as_int();
437 
438  if (std::max(a1_width, a2_width) > 2 * std::min(a1_width, a2_width)) return false;
439  if (std::max(b1_width, b2_width) > 2 * std::min(b1_width, b2_width)) return false;
440  if (std::max(y1_width, y2_width) > 2 * std::min(y1_width, y2_width)) return false;
441  }
442 
443  return true;
444  }
445 
446  if (config.generic_cbin_ops.count(c1->type))
447  {
448  if (!config.opt_aggressive)
449  {
450  int a1_width = c1->parameters.at("\\A_WIDTH").as_int();
451  int b1_width = c1->parameters.at("\\B_WIDTH").as_int();
452  int y1_width = c1->parameters.at("\\Y_WIDTH").as_int();
453 
454  int a2_width = c2->parameters.at("\\A_WIDTH").as_int();
455  int b2_width = c2->parameters.at("\\B_WIDTH").as_int();
456  int y2_width = c2->parameters.at("\\Y_WIDTH").as_int();
457 
458  int min1_width = std::min(a1_width, b1_width);
459  int max1_width = std::max(a1_width, b1_width);
460 
461  int min2_width = std::min(a2_width, b2_width);
462  int max2_width = std::max(a2_width, b2_width);
463 
464  if (std::max(min1_width, min2_width) > 2 * std::min(min1_width, min2_width)) return false;
465  if (std::max(max1_width, max2_width) > 2 * std::min(max1_width, max2_width)) return false;
466  if (std::max(y1_width, y2_width) > 2 * std::min(y1_width, y2_width)) return false;
467  }
468 
469  return true;
470  }
471 
472  if (c1->type == "$macc")
473  {
474  if (!config.opt_aggressive)
475  if (share_macc(c1, c2) > 2 * std::min(bits_macc(c1), bits_macc(c2))) return false;
476 
477  return true;
478  }
479 
480  for (auto &it : c1->parameters)
481  if (c2->parameters.count(it.first) == 0 || c2->parameters.at(it.first) != it.second)
482  return false;
483 
484  for (auto &it : c2->parameters)
485  if (c1->parameters.count(it.first) == 0 || c1->parameters.at(it.first) != it.second)
486  return false;
487 
488  return true;
489  }
ShareWorkerConfig config
Definition: share.cc:43
static int bits_macc(const Macc &m, int width)
Definition: share.cc:119
RTLIL::IdString type
Definition: rtlil.h:854
std::map< RTLIL::IdString, RTLIL::Const > parameters
Definition: rtlil.h:856
std::set< RTLIL::IdString > generic_uni_ops
Definition: share.cc:38
bool opt_aggressive
Definition: share.cc:36
std::set< RTLIL::IdString > generic_cbin_ops
Definition: share.cc:38
int share_macc(RTLIL::Cell *c1, RTLIL::Cell *c2, RTLIL::SigSpec act=RTLIL::SigSpec(), RTLIL::Cell *supercell=nullptr, std::set< RTLIL::Cell * > *supercell_aux=nullptr)
Definition: share.cc:239
std::set< RTLIL::IdString > generic_bin_ops
Definition: share.cc:38

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RTLIL::SigSpec ShareWorker::make_cell_activation_logic ( const std::set< std::pair< RTLIL::SigSpec, RTLIL::Const >> &  activation_patterns,
std::set< RTLIL::Cell * > &  supercell_aux 
)
inline

Definition at line 914 of file share.cc.

915  {
916  RTLIL::Wire *all_cases_wire = module->addWire(NEW_ID, 0);
917 
918  for (auto &p : activation_patterns) {
919  all_cases_wire->width++;
920  supercell_aux.insert(module->addEq(NEW_ID, p.first, p.second, RTLIL::SigSpec(all_cases_wire, all_cases_wire->width - 1)));
921  }
922 
923  if (all_cases_wire->width == 1)
924  return all_cases_wire;
925 
926  RTLIL::Wire *result_wire = module->addWire(NEW_ID);
927  supercell_aux.insert(module->addReduceOr(NEW_ID, all_cases_wire, result_wire));
928  return result_wire;
929  }
int width
Definition: rtlil.h:826
RTLIL::Cell * addEq(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_y, bool is_signed=false)
RTLIL::Cell * addReduceOr(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed=false)
RTLIL::Module * module
Definition: share.cc:47
RTLIL::Wire * addWire(RTLIL::IdString name, int width=1)
Definition: rtlil.cc:1331
#define NEW_ID
Definition: yosys.h:166

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RTLIL::Cell* ShareWorker::make_supercell ( RTLIL::Cell c1,
RTLIL::Cell c2,
RTLIL::SigSpec  act,
std::set< RTLIL::Cell * > &  supercell_aux 
)
inline

Definition at line 504 of file share.cc.

505  {
506  log_assert(c1->type == c2->type);
507 
508  if (config.generic_uni_ops.count(c1->type))
509  {
510  if (c1->parameters.at("\\A_SIGNED").as_bool() != c2->parameters.at("\\A_SIGNED").as_bool())
511  {
512  RTLIL::Cell *unsigned_cell = c1->parameters.at("\\A_SIGNED").as_bool() ? c2 : c1;
513  if (unsigned_cell->getPort("\\A").to_sigbit_vector().back() != RTLIL::State::S0) {
514  unsigned_cell->parameters.at("\\A_WIDTH") = unsigned_cell->parameters.at("\\A_WIDTH").as_int() + 1;
515  RTLIL::SigSpec new_a = unsigned_cell->getPort("\\A");
517  unsigned_cell->setPort("\\A", new_a);
518  }
519  unsigned_cell->parameters.at("\\A_SIGNED") = true;
520  unsigned_cell->check();
521  }
522 
523  bool a_signed = c1->parameters.at("\\A_SIGNED").as_bool();
524  log_assert(a_signed == c2->parameters.at("\\A_SIGNED").as_bool());
525 
526  RTLIL::SigSpec a1 = c1->getPort("\\A");
527  RTLIL::SigSpec y1 = c1->getPort("\\Y");
528 
529  RTLIL::SigSpec a2 = c2->getPort("\\A");
530  RTLIL::SigSpec y2 = c2->getPort("\\Y");
531 
532  int a_width = std::max(a1.size(), a2.size());
533  int y_width = std::max(y1.size(), y2.size());
534 
535  a1.extend_u0(a_width, a_signed);
536  a2.extend_u0(a_width, a_signed);
537 
538  RTLIL::SigSpec a = module->addWire(NEW_ID, a_width);
539  supercell_aux.insert(module->addMux(NEW_ID, a2, a1, act, a));
540 
541  RTLIL::Wire *y = module->addWire(NEW_ID, y_width);
542 
543  RTLIL::Cell *supercell = module->addCell(NEW_ID, c1->type);
544  supercell->parameters["\\A_SIGNED"] = a_signed;
545  supercell->parameters["\\A_WIDTH"] = a_width;
546  supercell->parameters["\\Y_WIDTH"] = y_width;
547  supercell->setPort("\\A", a);
548  supercell->setPort("\\Y", y);
549 
550  supercell_aux.insert(module->addPos(NEW_ID, y, y1));
551  supercell_aux.insert(module->addPos(NEW_ID, y, y2));
552 
553  supercell_aux.insert(supercell);
554  return supercell;
555  }
556 
557  if (config.generic_bin_ops.count(c1->type) || config.generic_cbin_ops.count(c1->type) || c1->type == "$alu")
558  {
559  bool modified_src_cells = false;
560 
561  if (config.generic_cbin_ops.count(c1->type))
562  {
563  int score_unflipped = std::max(c1->parameters.at("\\A_WIDTH").as_int(), c2->parameters.at("\\A_WIDTH").as_int()) +
564  std::max(c1->parameters.at("\\B_WIDTH").as_int(), c2->parameters.at("\\B_WIDTH").as_int());
565 
566  int score_flipped = std::max(c1->parameters.at("\\A_WIDTH").as_int(), c2->parameters.at("\\B_WIDTH").as_int()) +
567  std::max(c1->parameters.at("\\B_WIDTH").as_int(), c2->parameters.at("\\A_WIDTH").as_int());
568 
569  if (score_flipped < score_unflipped)
570  {
571  RTLIL::SigSpec tmp = c2->getPort("\\A");
572  c2->setPort("\\A", c2->getPort("\\B"));
573  c2->setPort("\\B", tmp);
574 
575  std::swap(c2->parameters.at("\\A_WIDTH"), c2->parameters.at("\\B_WIDTH"));
576  std::swap(c2->parameters.at("\\A_SIGNED"), c2->parameters.at("\\B_SIGNED"));
577  modified_src_cells = true;
578  }
579  }
580 
581  if (c1->parameters.at("\\A_SIGNED").as_bool() != c2->parameters.at("\\A_SIGNED").as_bool())
582 
583  {
584  RTLIL::Cell *unsigned_cell = c1->parameters.at("\\A_SIGNED").as_bool() ? c2 : c1;
585  if (unsigned_cell->getPort("\\A").to_sigbit_vector().back() != RTLIL::State::S0) {
586  unsigned_cell->parameters.at("\\A_WIDTH") = unsigned_cell->parameters.at("\\A_WIDTH").as_int() + 1;
587  RTLIL::SigSpec new_a = unsigned_cell->getPort("\\A");
589  unsigned_cell->setPort("\\A", new_a);
590  }
591  unsigned_cell->parameters.at("\\A_SIGNED") = true;
592  modified_src_cells = true;
593  }
594 
595  if (c1->parameters.at("\\B_SIGNED").as_bool() != c2->parameters.at("\\B_SIGNED").as_bool())
596  {
597  RTLIL::Cell *unsigned_cell = c1->parameters.at("\\B_SIGNED").as_bool() ? c2 : c1;
598  if (unsigned_cell->getPort("\\B").to_sigbit_vector().back() != RTLIL::State::S0) {
599  unsigned_cell->parameters.at("\\B_WIDTH") = unsigned_cell->parameters.at("\\B_WIDTH").as_int() + 1;
600  RTLIL::SigSpec new_b = unsigned_cell->getPort("\\B");
602  unsigned_cell->setPort("\\B", new_b);
603  }
604  unsigned_cell->parameters.at("\\B_SIGNED") = true;
605  modified_src_cells = true;
606  }
607 
608  if (modified_src_cells) {
609  c1->check();
610  c2->check();
611  }
612 
613  bool a_signed = c1->parameters.at("\\A_SIGNED").as_bool();
614  bool b_signed = c1->parameters.at("\\B_SIGNED").as_bool();
615 
616  log_assert(a_signed == c2->parameters.at("\\A_SIGNED").as_bool());
617  log_assert(b_signed == c2->parameters.at("\\B_SIGNED").as_bool());
618 
619  if (c1->type == "$shl" || c1->type == "$shr" || c1->type == "$sshl" || c1->type == "$sshr")
620  b_signed = false;
621 
622  RTLIL::SigSpec a1 = c1->getPort("\\A");
623  RTLIL::SigSpec b1 = c1->getPort("\\B");
624  RTLIL::SigSpec y1 = c1->getPort("\\Y");
625 
626  RTLIL::SigSpec a2 = c2->getPort("\\A");
627  RTLIL::SigSpec b2 = c2->getPort("\\B");
628  RTLIL::SigSpec y2 = c2->getPort("\\Y");
629 
630  int a_width = std::max(a1.size(), a2.size());
631  int b_width = std::max(b1.size(), b2.size());
632  int y_width = std::max(y1.size(), y2.size());
633 
634  if (c1->type == "$shr" && a_signed)
635  {
636  a_width = std::max(y_width, a_width);
637 
638  if (a1.size() < y1.size()) a1.extend_u0(y1.size(), true);
639  if (a2.size() < y2.size()) a2.extend_u0(y2.size(), true);
640 
641  a1.extend_u0(a_width, false);
642  a2.extend_u0(a_width, false);
643  }
644  else
645  {
646  a1.extend_u0(a_width, a_signed);
647  a2.extend_u0(a_width, a_signed);
648  }
649 
650  b1.extend_u0(b_width, b_signed);
651  b2.extend_u0(b_width, b_signed);
652 
653  RTLIL::SigSpec a = module->addWire(NEW_ID, a_width);
654  RTLIL::SigSpec b = module->addWire(NEW_ID, b_width);
655 
656  supercell_aux.insert(module->addMux(NEW_ID, a2, a1, act, a));
657  supercell_aux.insert(module->addMux(NEW_ID, b2, b1, act, b));
658 
659  RTLIL::Wire *y = module->addWire(NEW_ID, y_width);
660  RTLIL::Wire *x = c1->type == "$alu" ? module->addWire(NEW_ID, y_width) : nullptr;
661  RTLIL::Wire *co = c1->type == "$alu" ? module->addWire(NEW_ID, y_width) : nullptr;
662 
663  RTLIL::Cell *supercell = module->addCell(NEW_ID, c1->type);
664  supercell->parameters["\\A_SIGNED"] = a_signed;
665  supercell->parameters["\\B_SIGNED"] = b_signed;
666  supercell->parameters["\\A_WIDTH"] = a_width;
667  supercell->parameters["\\B_WIDTH"] = b_width;
668  supercell->parameters["\\Y_WIDTH"] = y_width;
669  supercell->setPort("\\A", a);
670  supercell->setPort("\\B", b);
671  supercell->setPort("\\Y", y);
672  if (c1->type == "$alu") {
674  supercell_aux.insert(module->addMux(NEW_ID, c2->getPort("\\CI"), c1->getPort("\\CI"), act, ci));
675  supercell_aux.insert(module->addMux(NEW_ID, c2->getPort("\\BI"), c1->getPort("\\BI"), act, bi));
676  supercell->setPort("\\CI", ci);
677  supercell->setPort("\\BI", bi);
678  supercell->setPort("\\CO", co);
679  supercell->setPort("\\X", x);
680  }
681  supercell->check();
682 
683  supercell_aux.insert(module->addPos(NEW_ID, y, y1));
684  supercell_aux.insert(module->addPos(NEW_ID, y, y2));
685  if (c1->type == "$alu") {
686  supercell_aux.insert(module->addPos(NEW_ID, co, c1->getPort("\\CO")));
687  supercell_aux.insert(module->addPos(NEW_ID, co, c2->getPort("\\CO")));
688  supercell_aux.insert(module->addPos(NEW_ID, x, c1->getPort("\\X")));
689  supercell_aux.insert(module->addPos(NEW_ID, x, c2->getPort("\\X")));
690  }
691 
692  supercell_aux.insert(supercell);
693  return supercell;
694  }
695 
696  if (c1->type == "$macc")
697  {
698  RTLIL::Cell *supercell = module->addCell(NEW_ID, c1->type);
699  supercell_aux.insert(supercell);
700  share_macc(c1, c2, act, supercell, &supercell_aux);
701  supercell->check();
702  return supercell;
703  }
704 
705  if (c1->type == "$memrd")
706  {
707  RTLIL::Cell *supercell = module->addCell(NEW_ID, c1);
708  supercell_aux.insert(module->addPos(NEW_ID, supercell->getPort("\\DATA"), c2->getPort("\\DATA")));
709  supercell_aux.insert(supercell);
710  return supercell;
711  }
712 
713  log_abort();
714  }
RTLIL::Cell * addCell(RTLIL::IdString name, RTLIL::IdString type)
Definition: rtlil.cc:1353
ShareWorkerConfig config
Definition: share.cc:43
void setPort(RTLIL::IdString portname, RTLIL::SigSpec signal)
Definition: rtlil.cc:1789
RTLIL::IdString type
Definition: rtlil.h:854
std::map< RTLIL::IdString, RTLIL::Const > parameters
Definition: rtlil.h:856
void extend_u0(int width, bool is_signed=false)
Definition: rtlil.cc:2612
std::set< RTLIL::IdString > generic_uni_ops
Definition: share.cc:38
int size() const
Definition: rtlil.h:1019
#define log_abort()
Definition: log.h:84
RTLIL::Module * module
Definition: share.cc:47
void append_bit(const RTLIL::SigBit &bit)
Definition: rtlil.cc:2562
const RTLIL::SigSpec & getPort(RTLIL::IdString portname) const
Definition: rtlil.cc:1809
#define log_assert(_assert_expr_)
Definition: log.h:85
RTLIL::Wire * addWire(RTLIL::IdString name, int width=1)
Definition: rtlil.cc:1331
#define NEW_ID
Definition: yosys.h:166
std::set< RTLIL::IdString > generic_cbin_ops
Definition: share.cc:38
void check()
Definition: rtlil.cc:1839
int share_macc(RTLIL::Cell *c1, RTLIL::Cell *c2, RTLIL::SigSpec act=RTLIL::SigSpec(), RTLIL::Cell *supercell=nullptr, std::set< RTLIL::Cell * > *supercell_aux=nullptr)
Definition: share.cc:239
RTLIL::Cell * addMux(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, RTLIL::SigSpec sig_y)
RTLIL::Cell * addPos(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed=false)
std::vector< RTLIL::SigBit > to_sigbit_vector() const
Definition: rtlil.cc:2921
std::set< RTLIL::IdString > generic_bin_ops
Definition: share.cc:38

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool ShareWorker::module_has_scc ( )
inline

Definition at line 936 of file share.cc.

937  {
938  CellTypes ct;
939  ct.setup_internals();
940  ct.setup_stdcells();
941 
943  toposort.analyze_loops = false;
944 
946  topo_bit_drivers.clear();
947 
948  std::map<RTLIL::Cell*, std::set<RTLIL::SigBit>> cell_to_bits;
949  std::map<RTLIL::SigBit, std::set<RTLIL::Cell*>> bit_to_cells;
950 
951  for (auto cell : module->cells())
952  if (ct.cell_known(cell->type))
953  for (auto &conn : cell->connections()) {
954  if (ct.cell_output(cell->type, conn.first))
955  for (auto bit : topo_sigmap(conn.second)) {
956  cell_to_bits[cell].insert(bit);
957  topo_bit_drivers[bit].insert(cell);
958  }
959  else
960  for (auto bit : topo_sigmap(conn.second))
961  bit_to_cells[bit].insert(cell);
962  }
963 
964  for (auto &it : cell_to_bits)
965  {
966  RTLIL::Cell *c1 = it.first;
967 
968  for (auto bit : it.second)
969  for (auto c2 : bit_to_cells[bit])
970  toposort.edge(c1, c2);
971  }
972 
973  bool found_scc = !toposort.sort();
974  topo_cell_drivers = std::move(toposort.database);
975 
976  if (found_scc && toposort.analyze_loops)
977  for (auto &loop : toposort.loops) {
978  log("### loop ###\n");
979  for (auto &c : loop)
980  log("%s (%s)\n", log_id(c), log_id(c->type));
981  }
982 
983  return found_scc;
984  }
void setup_stdcells()
Definition: celltypes.h:132
std::set< std::set< T, C > > loops
Definition: utils.h:136
bool sort()
Definition: utils.h:194
CellTypes ct
Definition: opt_clean.cc:33
std::map< RTLIL::Cell *, std::set< RTLIL::Cell *, cell_ptr_cmp >, cell_ptr_cmp > topo_cell_drivers
Definition: share.cc:57
RTLIL::Module * module
Definition: share.cc:47
void set(RTLIL::Module *module)
Definition: sigtools.h:273
bool cell_known(RTLIL::IdString type)
Definition: celltypes.h:188
bool cell_output(RTLIL::IdString type, RTLIL::IdString port)
Definition: celltypes.h:193
void edge(T left, T right)
Definition: utils.h:151
std::map< RTLIL::SigBit, std::set< RTLIL::Cell *, cell_ptr_cmp > > topo_bit_drivers
Definition: share.cc:58
SigMap topo_sigmap
Definition: share.cc:56
std::map< T, std::set< T, C >, C > database
Definition: utils.h:135
RTLIL::ObjRange< RTLIL::Cell * > cells()
Definition: rtlil.h:641
bool analyze_loops
Definition: utils.h:134
void log(const char *format,...)
Definition: log.cc:180
void setup_internals()
Definition: celltypes.h:83
const char * log_id(RTLIL::IdString str)
Definition: log.cc:283

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void ShareWorker::optimize_activation_patterns ( std::set< std::pair< RTLIL::SigSpec, RTLIL::Const >> &  )
inline

Definition at line 789 of file share.cc.

790  {
791  // TODO: Remove patterns that are contained in other patterns
792  // TODO: Consolidate pairs of patterns that only differ in the value for one signal bit
793  }

+ Here is the caller graph for this function:

int ShareWorker::share_macc ( RTLIL::Cell c1,
RTLIL::Cell c2,
RTLIL::SigSpec  act = RTLIL::SigSpec(),
RTLIL::Cell supercell = nullptr,
std::set< RTLIL::Cell * > *  supercell_aux = nullptr 
)
inline

Definition at line 239 of file share.cc.

241  {
242  Macc m1(c1), m2(c2), supermacc;
243 
244  int w1 = GetSize(c1->getPort("\\Y")), w2 = GetSize(c2->getPort("\\Y"));
245  int width = std::max(w1, w2);
246 
247  m1.optimize(w1);
248  m2.optimize(w2);
249 
250  std::sort(m1.ports.begin(), m1.ports.end(), cmp_macc_ports);
251  std::sort(m2.ports.begin(), m2.ports.end(), cmp_macc_ports);
252 
253  std::set<int> m1_unmapped, m2_unmapped;
254 
255  for (int i = 0; i < GetSize(m1.ports); i++)
256  m1_unmapped.insert(i);
257 
258  for (int i = 0; i < GetSize(m2.ports); i++)
259  m2_unmapped.insert(i);
260 
261  while (1)
262  {
263  int best_i = -1, best_j = -1, best_score = 0;
264 
265  for (int i : m1_unmapped)
266  for (int j : m2_unmapped) {
267  int score = share_macc_ports(m1.ports[i], m2.ports[j], w1, w2);
268  if (score >= 0 && (best_i < 0 || best_score > score))
269  best_i = i, best_j = j, best_score = score;
270  }
271 
272  if (best_i >= 0) {
273  m1_unmapped.erase(best_i);
274  m2_unmapped.erase(best_j);
275  share_macc_ports(m1.ports[best_i], m2.ports[best_j], w1, w2, act, &supermacc, supercell_aux);
276  } else
277  break;
278  }
279 
280  for (int i : m1_unmapped)
281  {
282  RTLIL::SigSpec sig_a = m1.ports[i].in_a;
283  RTLIL::SigSpec sig_b = m1.ports[i].in_b;
284 
285  if (supercell_aux && GetSize(sig_a)) {
286  sig_a = module->addWire(NEW_ID, GetSize(sig_a));
287  supercell_aux->insert(module->addMux(NEW_ID, RTLIL::SigSpec(0, GetSize(sig_a)), m1.ports[i].in_a, act, sig_a));
288  }
289 
290  if (supercell_aux && GetSize(sig_b)) {
291  sig_b = module->addWire(NEW_ID, GetSize(sig_b));
292  supercell_aux->insert(module->addMux(NEW_ID, RTLIL::SigSpec(0, GetSize(sig_b)), m1.ports[i].in_b, act, sig_b));
293  }
294 
295  Macc::port_t p;
296  p.in_a = sig_a;
297  p.in_b = sig_b;
298  p.is_signed = m1.ports[i].is_signed;
299  p.do_subtract = m1.ports[i].do_subtract;
300  supermacc.ports.push_back(p);
301  }
302 
303  for (int i : m2_unmapped)
304  {
305  RTLIL::SigSpec sig_a = m2.ports[i].in_a;
306  RTLIL::SigSpec sig_b = m2.ports[i].in_b;
307 
308  if (supercell_aux && GetSize(sig_a)) {
309  sig_a = module->addWire(NEW_ID, GetSize(sig_a));
310  supercell_aux->insert(module->addMux(NEW_ID, m2.ports[i].in_a, RTLIL::SigSpec(0, GetSize(sig_a)), act, sig_a));
311  }
312 
313  if (supercell_aux && GetSize(sig_b)) {
314  sig_b = module->addWire(NEW_ID, GetSize(sig_b));
315  supercell_aux->insert(module->addMux(NEW_ID, m2.ports[i].in_b, RTLIL::SigSpec(0, GetSize(sig_b)), act, sig_b));
316  }
317 
318  Macc::port_t p;
319  p.in_a = sig_a;
320  p.in_b = sig_b;
321  p.is_signed = m2.ports[i].is_signed;
322  p.do_subtract = m2.ports[i].do_subtract;
323  supermacc.ports.push_back(p);
324  }
325 
326  if (supercell)
327  {
328  RTLIL::SigSpec sig_y = module->addWire(NEW_ID, width);
329 
330  supercell_aux->insert(module->addPos(NEW_ID, sig_y, c1->getPort("\\Y")));
331  supercell_aux->insert(module->addPos(NEW_ID, sig_y, c2->getPort("\\Y")));
332 
333  supercell->setParam("\\Y_WIDTH", width);
334  supercell->setPort("\\Y", sig_y);
335 
336  supermacc.optimize(width);
337  supermacc.to_cell(supercell);
338  }
339 
340  return bits_macc(supermacc, width);
341  }
void to_cell(RTLIL::Cell *cell) const
Definition: macc.h:152
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:57
Definition: macc.h:27
void setParam(RTLIL::IdString paramname, RTLIL::Const value)
Definition: rtlil.cc:1829
static int bits_macc(const Macc &m, int width)
Definition: share.cc:119
void setPort(RTLIL::IdString portname, RTLIL::SigSpec signal)
Definition: rtlil.cc:1789
std::vector< port_t > ports
Definition: macc.h:34
void optimize(int width)
Definition: macc.h:37
RTLIL::SigSpec in_b
Definition: macc.h:30
RTLIL::Module * module
Definition: share.cc:47
static bool cmp_macc_ports(const Macc::port_t &p1, const Macc::port_t &p2)
Definition: share.cc:134
const RTLIL::SigSpec & getPort(RTLIL::IdString portname) const
Definition: rtlil.cc:1809
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
RTLIL::Wire * addWire(RTLIL::IdString name, int width=1)
Definition: rtlil.cc:1331
#define NEW_ID
Definition: yosys.h:166
bool is_signed
Definition: macc.h:31
RTLIL::SigSpec in_a
Definition: macc.h:30
int share_macc_ports(Macc::port_t &p1, Macc::port_t &p2, int w1, int w2, RTLIL::SigSpec act=RTLIL::SigSpec(), Macc *supermacc=nullptr, std::set< RTLIL::Cell * > *supercell_aux=nullptr)
Definition: share.cc:163
RTLIL::Cell * addMux(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, RTLIL::SigSpec sig_y)
RTLIL::Cell * addPos(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_y, bool is_signed=false)
bool do_subtract
Definition: macc.h:31

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int ShareWorker::share_macc_ports ( Macc::port_t p1,
Macc::port_t p2,
int  w1,
int  w2,
RTLIL::SigSpec  act = RTLIL::SigSpec(),
Macc supermacc = nullptr,
std::set< RTLIL::Cell * > *  supercell_aux = nullptr 
)
inline

Definition at line 163 of file share.cc.

165  {
166  if (p1.do_subtract != p2.do_subtract)
167  return -1;
168 
169  bool mul1 = GetSize(p1.in_a) && GetSize(p1.in_b);
170  bool mul2 = GetSize(p2.in_a) && GetSize(p2.in_b);
171 
172  if (mul1 != mul2)
173  return -1;
174 
175  bool force_signed = false, force_not_signed = false;
176 
177  if ((GetSize(p1.in_a) && GetSize(p1.in_a) < w1) || (GetSize(p1.in_b) && GetSize(p1.in_b) < w1)) {
178  if (p1.is_signed)
179  force_signed = true;
180  else
181  force_not_signed = true;
182  }
183 
184  if ((GetSize(p2.in_a) && GetSize(p2.in_a) < w2) || (GetSize(p2.in_b) && GetSize(p2.in_b) < w2)) {
185  if (p2.is_signed)
186  force_signed = true;
187  else
188  force_not_signed = true;
189  }
190 
191  if (force_signed && force_not_signed)
192  return -1;
193 
194  if (supermacc)
195  {
196  RTLIL::SigSpec sig_a1 = p1.in_a, sig_b1 = p1.in_b;
197  RTLIL::SigSpec sig_a2 = p2.in_a, sig_b2 = p2.in_b;
198 
199  RTLIL::SigSpec sig_a = GetSize(sig_a1) > GetSize(sig_a2) ? sig_a1 : sig_a2;
200  RTLIL::SigSpec sig_b = GetSize(sig_b1) > GetSize(sig_b2) ? sig_b1 : sig_b2;
201 
202  sig_a1.extend_u0(GetSize(sig_a), p1.is_signed);
203  sig_b1.extend_u0(GetSize(sig_b), p1.is_signed);
204 
205  sig_a2.extend_u0(GetSize(sig_a), p2.is_signed);
206  sig_b2.extend_u0(GetSize(sig_b), p2.is_signed);
207 
208  if (supercell_aux && GetSize(sig_a)) {
209  sig_a = module->addWire(NEW_ID, GetSize(sig_a));
210  supercell_aux->insert(module->addMux(NEW_ID, sig_a2, sig_a1, act, sig_a));
211  }
212 
213  if (supercell_aux && GetSize(sig_b)) {
214  sig_b = module->addWire(NEW_ID, GetSize(sig_b));
215  supercell_aux->insert(module->addMux(NEW_ID, sig_b2, sig_b1, act, sig_b));
216  }
217 
218  Macc::port_t p;
219  p.in_a = sig_a;
220  p.in_b = sig_b;
221  p.is_signed = force_signed;
222  p.do_subtract = p1.do_subtract;
223  supermacc->ports.push_back(p);
224  }
225 
226  int score = 1000 + abs(GetSize(p1.in_a) - GetSize(p2.in_a)) * std::max(abs(GetSize(p1.in_b) - GetSize(p2.in_b)), 1);
227 
228  for (int i = 0; i < std::min(GetSize(p1.in_a), GetSize(p2.in_a)); i++)
229  if (p1.in_a[i] == p2.in_a[i] && score > 0)
230  score--;
231 
232  for (int i = 0; i < std::min(GetSize(p1.in_b), GetSize(p2.in_b)); i++)
233  if (p1.in_b[i] == p2.in_b[i] && score > 0)
234  score--;
235 
236  return score;
237  }
std::vector< port_t > ports
Definition: macc.h:34
void extend_u0(int width, bool is_signed=false)
Definition: rtlil.cc:2612
RTLIL::SigSpec in_b
Definition: macc.h:30
RTLIL::Module * module
Definition: share.cc:47
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
RTLIL::Wire * addWire(RTLIL::IdString name, int width=1)
Definition: rtlil.cc:1331
#define NEW_ID
Definition: yosys.h:166
bool is_signed
Definition: macc.h:31
RTLIL::SigSpec in_a
Definition: macc.h:30
RTLIL::Cell * addMux(RTLIL::IdString name, RTLIL::SigSpec sig_a, RTLIL::SigSpec sig_b, RTLIL::SigSpec sig_s, RTLIL::SigSpec sig_y)
bool do_subtract
Definition: macc.h:31

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool ShareWorker::sort_check_activation_pattern ( std::pair< RTLIL::SigSpec, RTLIL::Const > &  p)
inline

Definition at line 765 of file share.cc.

766  {
767  std::map<RTLIL::SigBit, RTLIL::State> p_bits;
768 
769  std::vector<RTLIL::SigBit> p_first_bits = p.first;
770  for (int i = 0; i < GetSize(p_first_bits); i++) {
771  RTLIL::SigBit b = p_first_bits[i];
772  RTLIL::State v = p.second.bits[i];
773  if (p_bits.count(b) && p_bits.at(b) != v)
774  return false;
775  p_bits[b] = v;
776  }
777 
778  p.first = RTLIL::SigSpec();
779  p.second.bits.clear();
780 
781  for (auto &it : p_bits) {
782  p.first.append_bit(it.first);
783  p.second.bits.push_back(it.second);
784  }
785 
786  return true;
787  }
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
State
Definition: rtlil.h:29

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Field Documentation

std::map<RTLIL::Cell*, std::set<std::pair<RTLIL::SigSpec, RTLIL::Const> > > ShareWorker::activation_patterns_cache

Definition at line 763 of file share.cc.

std::set<RTLIL::Cell*> ShareWorker::cells_to_remove

Definition at line 53 of file share.cc.

CellTypes ShareWorker::cone_ct

Definition at line 49 of file share.cc.

ShareWorkerConfig ShareWorker::config

Definition at line 43 of file share.cc.

RTLIL::Design* ShareWorker::design

Definition at line 46 of file share.cc.

std::vector<std::pair<RTLIL::SigBit, RTLIL::SigBit> > ShareWorker::exclusive_ctrls

Definition at line 60 of file share.cc.

std::map<RTLIL::Cell*, std::set<RTLIL::SigBit> > ShareWorker::forbidden_controls_cache

Definition at line 721 of file share.cc.

CellTypes ShareWorker::fwd_ct

Definition at line 49 of file share.cc.

std::set<RTLIL::IdString> ShareWorker::generic_ops

Definition at line 44 of file share.cc.

ModIndex ShareWorker::mi

Definition at line 51 of file share.cc.

RTLIL::Module* ShareWorker::module

Definition at line 47 of file share.cc.

ModWalker ShareWorker::modwalker

Definition at line 50 of file share.cc.

std::set<RTLIL::Cell*> ShareWorker::recursion_state

Definition at line 54 of file share.cc.

std::set<RTLIL::Cell*, RTLIL::sort_by_name_str<RTLIL::Cell> > ShareWorker::shareable_cells

Definition at line 348 of file share.cc.

std::set<RTLIL::SigBit> ShareWorker::terminal_bits

Definition at line 67 of file share.cc.

std::map<RTLIL::SigBit, std::set<RTLIL::Cell*, cell_ptr_cmp> > ShareWorker::topo_bit_drivers

Definition at line 58 of file share.cc.

std::map<RTLIL::Cell*, std::set<RTLIL::Cell*, cell_ptr_cmp>, cell_ptr_cmp> ShareWorker::topo_cell_drivers

Definition at line 57 of file share.cc.

SigMap ShareWorker::topo_sigmap

Definition at line 56 of file share.cc.


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