static */
| 277 | |
| 278 | /* static */ |
| 279 | std::unique_ptr<CallGraph> CallGraph::Build(const HloModule* module) { |
| 280 | // Constructor for CallGraph is private so absl::make_unique can't be used. |
| 281 | auto call_graph = absl::WrapUnique<CallGraph>(new CallGraph(module)); |
| 282 | |
| 283 | VLOG(3) << "Building call graph for:"; |
| 284 | XLA_VLOG_LINES(3, module->ToString()); |
| 285 | |
| 286 | // Construct nodes of the call graph and populate the callsites. |
| 287 | for (HloComputation* computation : module->computations()) { |
| 288 | auto it_added = call_graph->node_indices_.insert( |
| 289 | {computation, call_graph->nodes_.size()}); |
| 290 | // All computations should be unique, so the computation should not already |
| 291 | // exist in the map. |
| 292 | CHECK(it_added.second); |
| 293 | call_graph->nodes_.emplace_back(computation); |
| 294 | |
| 295 | // Add all callsites in this computation. |
| 296 | for (HloInstruction* instruction : computation->instructions()) { |
| 297 | call_graph->nodes_.back().AddCallSiteForInstruction(instruction); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // Add caller callsites to each node. |
| 302 | for (const HloComputation* computation : module->computations()) { |
| 303 | for (const CallSite& callsite : |
| 304 | call_graph->GetNode(computation).callsites()) { |
| 305 | for (auto* callee : callsite.called_computations()) { |
| 306 | // Add caller callsites. |
| 307 | call_graph->GetNode(callee).AddCallerCallSite(callsite); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | call_graph->SetCallContexts(); |
| 313 | call_graph->SetNodeDepths(); |
| 314 | |
| 315 | XLA_VLOG_LINES(2, call_graph->ToString()); |
| 316 | |
| 317 | return call_graph; |
| 318 | } |
| 319 | |
| 320 | Status CallGraph::VisitNodesInternal( |
| 321 | const VisitorFunction& visitor_func, const CallGraphNode& node, |