42 #include <igraph/igraph.h>
46 #include <unordered_map>
47 #include <unordered_set>
55 std::unordered_set<const Gate *> get_toggle_ffs(
const Netlist *netlist )
57 const std::vector<Gate *> ffs = netlist->get_gates(
is_ff );
59 std::unordered_set<const Gate *> result;
60 for(
const Gate *ff : ffs )
62 const std::vector<Endpoint *> successor_endpoints =
ff->get_successors();
63 const std::size_t successor_endpoints_size = successor_endpoints.size();
65 if( successor_endpoints_size == 0 )
70 std::vector<const Gate *> successors;
71 successors.reserve( successor_endpoints_size );
73 std::transform( successor_endpoints.begin(),
74 successor_endpoints.end(),
75 std::back_inserter( successors ),
76 [](
const Endpoint *ep ) { return ep->get_gate(); } );
78 if( std::find( successors.begin(), successors.end(), ff ) != successors.end() )
87 igraph_error_t in_callback(
const igraph_t *graph, igraph_int_t vid, igraph_int_t dist,
void *extra )
89 return igraph_vector_int_push_back(
static_cast<igraph_vector_int_t *
>( extra ), vid );
100 class ScopedIgraphErrorHandler
103 ScopedIgraphErrorHandler()
104 : m_previous( igraph_set_error_handler( &handle_error ) )
108 ~ScopedIgraphErrorHandler()
110 igraph_set_error_handler( m_previous );
113 ScopedIgraphErrorHandler(
const ScopedIgraphErrorHandler & ) =
delete;
114 ScopedIgraphErrorHandler &operator=(
const ScopedIgraphErrorHandler & ) =
delete;
117 static void handle_error(
const char *reason,
const char *file,
int line, igraph_error_t igraph_errno )
122 "igraph error at {}:{}: {} ({})",
126 igraph_strerror( igraph_errno ) );
129 IGRAPH_FINALLY_FREE();
132 igraph_error_handler_t *m_previous;
138 std::unordered_set<igraph_int_t> &&roots,
139 std::unordered_map<igraph_int_t, const void *> &&vertices_to_ptrs,
140 std::unordered_map<const void *, PtrType> &&ptrs_to_types )
141 : m_netlist( netlist )
143 , m_roots(
std::move( roots ) )
144 , m_vertices_to_ptrs(
std::move( vertices_to_ptrs ) )
145 , m_ptrs_to_types(
std::move( ptrs_to_types ) )
147 for(
const auto &[vertex, ptr] : m_vertices_to_ptrs )
149 m_ptrs_to_vertices[ptr] = vertex;
153 ClockTree::~ClockTree()
155 igraph_destroy( &m_igraph );
160 if( netlist ==
nullptr )
162 return ERR(
"no netlist provided" );
165 std::vector<Diagnostic> diagnostics;
166 const auto report = [&diagnostics](
Severity severity,
171 std::string message ) {
172 if( severity == Severity::error )
174 log_error(
"clock_tree_extractor",
"{}", message );
178 log_warning(
"clock_tree_extractor",
"{}", message );
180 diagnostics.push_back( { severity, code,
type, ptr,
id, std::move( message ) } );
183 std::unordered_set<const void *> vertices;
184 std::unordered_set<std::pair<const void *, const void *>,
VoidPtrHash> edges;
185 std::unordered_map<const void *, PtrType> ptrs_to_type;
187 std::queue<std::tuple<const Gate *, const Gate *, std::vector<const Gate *>>> queue;
188 std::unordered_set<std::pair<const Gate *, const Gate *>,
VoidPtrHash> visited;
189 std::unordered_set<const Gate *> expanded;
193 vertices.insert(
ff );
196 const std::vector<GatePin *> clock_pins =
ff->get_type()->get_pins( [](
const auto &p ) {
197 return ( p->get_direction() == PinDirection::input ) && ( p->get_type() == PinType::clock );
200 if( clock_pins.size() != 1 )
202 report( Severity::error,
203 DiagnosticCode::invalid_clock_pin_count,
207 "invalid number of input clock pins at gate '" +
ff->get_name() +
"' with ID "
208 + std::to_string(
ff->get_id() ) );
212 const Net *clk =
ff->get_fan_in_net( clock_pins.front() );
215 report( Severity::error,
216 DiagnosticCode::unconnected_clock_pin,
220 "no net connected to clock pin at gate '" +
ff->get_name() +
"' with ID "
221 + std::to_string(
ff->get_id() ) );
230 const Gate *gate = source_ep->get_gate();
243 report( Severity::error,
244 DiagnosticCode::invalid_clock_net_sources,
248 "invalid number of sources for clock net with ID " + std::to_string( clk->
get_id() ) );
255 vertices.insert( clk );
256 ptrs_to_type[clk] =
NET;
257 edges.insert( { clk,
ff } );
262 report( Severity::warning,
263 DiagnosticCode::unrouted_clock_net,
267 "unrouted clock net with ID " + std::to_string( clk->
get_id() ) +
" ignored" );
273 const Gate *gate = source_ep->get_gate();
274 queue.emplace(
ff, gate, std::vector<const Gate *>{
ff } );
278 const std::unordered_set<const Gate *> toggle_ffs = get_toggle_ffs( netlist );
280 while( !queue.empty() )
282 const Gate *reference = std::get<0>( queue.front() );
283 const Gate *source = std::get<1>( queue.front() );
284 std::vector<const Gate *> path = std::move( std::get<2>( queue.front() ) );
287 path.push_back( source );
296 if(
is_ff( source ) && toggle_ffs.find( source ) == toggle_ffs.end() )
302 for(
const Gate *gate : path )
304 vertices.insert( gate );
305 ptrs_to_type[gate] =
GATE;
308 for( std::size_t idx = 0; idx < path.size() - 1; idx++ )
310 edges.insert( { path[idx + 1], path[idx] } );
314 path.push_back( source );
316 if(
is_ff( source ) )
322 if( !expanded.insert( source ).second )
331 visited.insert( std::make_pair( reference, source ) );
341 const Net *
net = ep->get_net();
342 if(
net->get_name() ==
"'0'" ||
net->get_name() ==
"'1'" )
348 if(
net->is_global_input_net() )
350 for(
const Gate *gate : path )
352 vertices.insert( gate );
353 ptrs_to_type[gate] =
GATE;
356 for( std::size_t idx = 0; idx < path.size() - 1; idx++ )
358 edges.insert( { path[idx + 1], path[idx] } );
361 vertices.insert(
net );
364 edges.insert( {
net, path.back() } );
367 path.push_back( source );
372 if(
net->get_num_of_sources() == 0 )
374 report( Severity::warning,
375 DiagnosticCode::unrouted_clock_net,
379 "unrouted clock net with ID " + std::to_string(
net->get_id() ) +
" ignored" );
382 if(
net->get_num_of_sources() > 1 )
384 report( Severity::warning,
385 DiagnosticCode::multi_driven_clock_net,
389 "multi-driven clock net with ID " + std::to_string(
net->get_id() ) +
" ignored" );
393 const Gate *new_source =
net->get_sources().front()->get_gate();
394 if( visited.find( { reference, new_source } ) == visited.end() )
396 queue.emplace( reference, new_source, path );
401 const ScopedIgraphErrorHandler igraph_error_handler;
405 std::unordered_map<igraph_int_t, const void *> vertices_to_ptrs;
406 std::unordered_map<const void *, igraph_int_t> ptrs_to_vertices;
408 igraph_int_t idx = 0;
409 for(
const void *vertex : vertices )
411 const igraph_int_t vertex_id = idx++;
413 vertices_to_ptrs[vertex_id] = vertex;
414 ptrs_to_vertices[vertex] = vertex_id;
417 igraph_error_t ierror;
418 igraph_vector_int_t iedges;
419 if( ( ierror = igraph_vector_int_init( &iedges, 2 * edges.size() ) ) != IGRAPH_SUCCESS )
421 return ERR( igraph_strerror( ierror ) );
425 for(
const auto &[src, dst] : edges )
427 VECTOR( iedges )[idx++] = ptrs_to_vertices.at( src );
428 VECTOR( iedges )[idx++] = ptrs_to_vertices.at( dst );
432 if( ( ierror = igraph_create( &igraph, &iedges, vertices.size(), IGRAPH_DIRECTED ) ) != IGRAPH_SUCCESS )
434 igraph_vector_int_destroy( &iedges );
435 return ERR( igraph_strerror( ierror ) );
438 igraph_vector_int_destroy( &iedges );
440 igraph_vector_int_t indegrees;
441 if( ( ierror = igraph_vector_int_init( &indegrees, 0 ) ) != IGRAPH_SUCCESS )
443 igraph_destroy( &igraph );
444 return ERR( igraph_strerror( ierror ) );
447 if( ( ierror = igraph_degree( &igraph, &indegrees, igraph_vss_all(), IGRAPH_IN, IGRAPH_NO_LOOPS ) )
450 igraph_destroy( &igraph );
451 igraph_vector_int_destroy( &indegrees );
452 return ERR( igraph_strerror( ierror ) );
455 std::unordered_set<igraph_int_t> roots;
456 for( idx = 0; idx < igraph_vector_int_size( &indegrees ); idx++ )
458 if( VECTOR( indegrees )[idx] != 0 )
465 igraph_vector_int_destroy( &indegrees );
469 auto clock_tree = std::unique_ptr<ClockTree>(
new ClockTree( netlist,
472 std::move( vertices_to_ptrs ),
473 std::move( ptrs_to_type ) ) );
474 clock_tree->m_diagnostics = std::move( diagnostics );
476 return OK( std::move( clock_tree ) );
481 const ScopedIgraphErrorHandler igraph_error_handler;
483 auto it = m_ptrs_to_vertices.find( ptr );
484 if( it == m_ptrs_to_vertices.end() )
486 return ERR(
"object is not part of clock tree" );
489 igraph_error_t ierror;
490 igraph_int_t root = it->second;
493 igraph_vector_int_t parents;
494 if( ( ierror = igraph_vector_int_init( &parents, 0 ) ) != IGRAPH_SUCCESS )
496 return ERR( igraph_strerror( ierror ) );
500 igraph_neighbors( &m_igraph, &parents, root, IGRAPH_IN, IGRAPH_NO_LOOPS, IGRAPH_NO_MULTIPLE ) )
503 igraph_vector_int_destroy( &parents );
504 return ERR( igraph_strerror( ierror ) );
508 if( igraph_vector_int_size( &parents ) == 1 )
510 root = VECTOR( parents )[0];
513 igraph_vector_int_destroy( &parents );
516 igraph_vector_int_t vertices;
517 if( ( ierror = igraph_vector_int_init( &vertices, 0 ) ) != IGRAPH_SUCCESS )
519 return ERR( igraph_strerror( ierror ) );
522 if( ( ierror = igraph_dfs( &m_igraph,
535 igraph_vector_int_destroy( &vertices );
536 return ERR( igraph_strerror( ierror ) );
540 if( ( ierror = igraph_vs_vector( &vs, &vertices ) ) != IGRAPH_SUCCESS )
542 igraph_vector_int_destroy( &vertices );
543 return ERR( igraph_strerror( ierror ) );
546 igraph_vector_int_t map;
547 if( ( ierror = igraph_vector_int_init( &map, igraph_vcount( &m_igraph ) ) ) != IGRAPH_SUCCESS )
549 igraph_vs_destroy( &vs );
550 igraph_vector_int_destroy( &vertices );
551 return ERR( igraph_strerror( ierror ) );
555 if( ( ierror = igraph_induced_subgraph_map( &m_igraph, &igraph, vs, IGRAPH_SUBGRAPH_AUTO, &map,
nullptr ) )
558 igraph_vs_destroy( &vs );
559 igraph_vector_int_destroy( &map );
560 igraph_vector_int_destroy( &vertices );
561 return ERR( igraph_strerror( ierror ) );
564 igraph_vs_destroy( &vs );
565 igraph_vector_int_destroy( &vertices );
567 std::unordered_set<igraph_int_t> roots;
568 std::unordered_map<const void *, PtrType> ptrs_to_types;
569 std::unordered_map<igraph_int_t, const void *> vertices_to_ptrs;
571 for( igraph_int_t idx = 0; idx < igraph_vector_int_size( &map ); idx++ )
573 const igraph_int_t vertex = VECTOR( map )[idx];
579 const void *ptr = m_vertices_to_ptrs.at( idx );
581 vertices_to_ptrs[vertex] = ptr;
582 ptrs_to_types[ptr] = m_ptrs_to_types.at( ptr );
585 igraph_vector_int_destroy( &map );
587 igraph_vector_int_t indegrees;
588 if( ( ierror = igraph_vector_int_init( &indegrees, igraph_vcount( &igraph ) ) ) != IGRAPH_SUCCESS )
590 igraph_destroy( &igraph );
591 return ERR( igraph_strerror( ierror ) );
594 if( ( ierror = igraph_degree( &igraph, &indegrees, igraph_vss_all(), IGRAPH_IN, IGRAPH_NO_LOOPS ) )
597 igraph_destroy( &igraph );
598 igraph_vector_int_destroy( &indegrees );
599 return ERR( igraph_strerror( ierror ) );
602 for( igraph_int_t idx = 0; idx < igraph_vector_int_size( &indegrees ); idx++ )
604 if( VECTOR( indegrees )[idx] != 0 )
611 igraph_vector_int_destroy( &indegrees );
613 return OK( std::unique_ptr<ClockTree>(
new ClockTree( m_netlist,
616 std::move( vertices_to_ptrs ),
617 std::move( ptrs_to_types ) ) ) );
622 auto it = m_ptrs_to_vertices.find( ptr );
623 if( it == m_ptrs_to_vertices.end() )
625 return ERR(
"object is not part of clock tree" );
628 return OK( it->second );
633 auto it = m_vertices_to_ptrs.find( vertex );
634 if( it == m_vertices_to_ptrs.end() )
636 return ERR(
"object is not part of clock tree" );
639 return OK( std::make_pair( it->second, m_ptrs_to_types.at( it->second ) ) );
644 std::vector<igraph_int_t> result;
646 for(
const void *ptr : ptrs )
648 auto res = get_vertex_from_ptr( ptr );
651 return ERR( res.get_error().get() );
654 result.push_back( res.get() );
661 ClockTree::get_ptrs_from_vertices(
const std::vector<igraph_int_t> &vertices )
const
663 std::vector<std::pair<const void *, PtrType>> result;
665 for(
const igraph_int_t vertex : vertices )
667 auto res = get_ptr_from_vertex( vertex );
670 return ERR( res.get_error().get() );
673 result.push_back( res.get() );
679 std::vector<const Gate *> ClockTree::get_gates()
const
681 std::vector<const Gate *> result;
683 for(
const auto &[ptr,
type] : m_ptrs_to_types )
687 result.push_back(
static_cast<const Gate *
>( ptr ) );
694 std::vector<const Net *> ClockTree::get_nets()
const
696 std::vector<const Net *> result;
698 for(
const auto &[ptr,
type] : m_ptrs_to_types )
702 result.push_back(
static_cast<const Net *
>( ptr ) );
709 const std::unordered_map<const void *, PtrType> &ClockTree::get_all()
const
711 return m_ptrs_to_types;
719 const std::vector<Diagnostic> &ClockTree::get_diagnostics()
const
721 return m_diagnostics;
724 const std::unordered_set<igraph_int_t> &ClockTree::get_roots()
const
729 const igraph_t *ClockTree::get_igraph()
const
735 ClockTree::get_neighbors(
const void *ptr,
const igraph_neimode_t
direction )
const
737 const ScopedIgraphErrorHandler igraph_error_handler;
739 auto it = m_ptrs_to_vertices.find( ptr );
740 if( it == m_ptrs_to_vertices.end() )
742 return ERR(
"object is not part of clock tree" );
745 igraph_error_t ierror;
746 igraph_vector_int_t neighbors;
748 if( ( ierror = igraph_vector_int_init( &neighbors, 0 ) ) != IGRAPH_SUCCESS )
750 return ERR( igraph_strerror( ierror ) );
753 if( ( ierror = igraph_neighbors(
754 &m_igraph, &neighbors, it->second,
direction, IGRAPH_NO_LOOPS, IGRAPH_NO_MULTIPLE ) )
757 igraph_vector_int_destroy( &neighbors );
758 return ERR( igraph_strerror( ierror ) );
761 std::vector<std::pair<const void *, PtrType>> result;
762 for( igraph_int_t idx = 0; idx < igraph_vector_int_size( &neighbors ); idx++ )
764 const void *n_ptr = m_vertices_to_ptrs.at( VECTOR( neighbors )[idx] );
765 result.emplace_back( n_ptr, m_ptrs_to_types.at( n_ptr ) );
768 igraph_vector_int_destroy( &neighbors );
const std::vector< Endpoint * > & get_fan_in_endpoints() const
u32 get_num_of_sources(const std::function< bool(Endpoint *ep)> &filter=nullptr) const
bool is_global_input_net() const
std::vector< Endpoint * > get_sources(const std::function< bool(Endpoint *ep)> &filter=nullptr) const
const std::vector< Gate * > & get_gates() const
ClockTree(const ClockTree &)=delete
#define log_error(channel,...)
#define log_debug(channel,...)
#define log_warning(channel,...)
bool is_delay(const Gate *gate)
bool is_connected_to_control_pin(const Endpoint *endpoint)
bool is_inverter(const Gate *gate)
bool is_buffer(const Gate *gate)
bool is_latch(const Gate *gate)
bool is_ff(const Gate *gate)