@@ -61,6 +61,7 @@ struct FlattenWorker
6161 bool create_scopeinfo = true ;
6262 bool create_scopename = false ;
6363 std::string separator = " ." ;
64+ bool barriers = false ;
6465
6566 template <class T >
6667 void map_attributes (RTLIL::Cell *cell, T *object, IdString orig_object_name)
@@ -247,7 +248,27 @@ struct FlattenWorker
247248 log_error (" Cell port %s.%s.%s is driving constant bits: %s <= %s\n " ,
248249 log_id (module ), log_id (cell), log_id (port_it.first ), log_signal (new_conn.first ), log_signal (new_conn.second ));
249250
250- module ->connect (new_conn);
251+ if (barriers) {
252+ // Drive public output wires with barriers and the rest with
253+ // connections
254+ RTLIL::SigSig skip_conn, barrier_conn;
255+
256+ for (int i = 0 ; i < GetSize (new_conn.first ); i++) {
257+ const auto lhs = new_conn.first [i], rhs = new_conn.second [i];
258+ auto & sigsig = !lhs.is_wire () || !lhs.wire ->name .isPublic () ? skip_conn : barrier_conn;
259+ sigsig.first .append (lhs);
260+ sigsig.second .append (rhs);
261+ }
262+
263+ if (!skip_conn.first .empty ())
264+ module ->connect (skip_conn);
265+
266+ if (!barrier_conn.first .empty ())
267+ module ->addBarrier (NEW_ID, barrier_conn.second , barrier_conn.first );
268+ } else {
269+ module ->connect (new_conn);
270+ }
271+
251272 sigmap.add (new_conn.first , new_conn.second );
252273 }
253274
@@ -353,6 +374,10 @@ struct FlattenPass : public Pass {
353374 log (" Don't remove unused submodules, leave a flattened version of each\n " );
354375 log (" submodule in the design.\n " );
355376 log (" \n " );
377+ log (" -barriers\n " );
378+ log (" Introduce an optimization barrier (a $barrier cell) when a flattened\n " );
379+ log (" module drives a public wire.\n " );
380+ log (" \n " );
356381 }
357382 void execute (std::vector<std::string> args, RTLIL::Design *design) override
358383 {
@@ -386,6 +411,9 @@ struct FlattenPass : public Pass {
386411 }
387412 if (args[argidx] == " -nocleanup" ) {
388413 cleanup = false ;
414+ }
415+ if (args[argidx] == " -barriers" ) {
416+ worker.barriers = true ;
389417 continue ;
390418 }
391419 break ;
0 commit comments