Flatten a single call graph node. Expects to visit nodes in postorder.
| 68 | |
| 69 | // Flatten a single call graph node. Expects to visit nodes in postorder. |
| 70 | Status FlattenNode(const CallGraphNode& node) { |
| 71 | HloComputation* computation = node.computation(); |
| 72 | HloModule* module = computation->parent(); |
| 73 | // Clone callee for all call-sites except the first one. |
| 74 | for (int i = 0; i < node.caller_callsites().size(); ++i) { |
| 75 | CallSite call_site = node.caller_callsites()[i]; |
| 76 | // Only consider sequential call contexts. |
| 77 | if (call_site.context() == CallContext::kParallel) { |
| 78 | continue; |
| 79 | } |
| 80 | CHECK_EQ(call_site.context(), CallContext::kSequential); |
| 81 | |
| 82 | // Skip first element if this computation is only called from a sequential |
| 83 | // context. |
| 84 | if (node.context() != CallContext::kBoth && i == 0) { |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | // Clone computation for the remaining sequential context call sites. |
| 89 | HloComputation* clone = |
| 90 | module->AddEmbeddedComputation(computation->Clone()); |
| 91 | ReplaceCalledComputation(call_site.instruction(), computation, clone); |
| 92 | // Clone the sub-tree of all computations called from this node. |
| 93 | std::vector<HloComputation*> worklist; |
| 94 | worklist.push_back(clone); |
| 95 | while (!worklist.empty()) { |
| 96 | auto current = worklist.back(); |
| 97 | worklist.pop_back(); |
| 98 | for (auto* instruction : current->instructions()) { |
| 99 | if (GetInstructionCallContext(instruction->opcode()) != |
| 100 | CallContext::kSequential) { |
| 101 | continue; |
| 102 | } |
| 103 | for (auto callee : instruction->called_computations()) { |
| 104 | HloComputation* callee_clone = |
| 105 | module->AddEmbeddedComputation(callee->Clone()); |
| 106 | ReplaceCalledComputation(instruction, callee, callee_clone); |
| 107 | worklist.push_back(callee_clone); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | return Status::OK(); |
| 113 | } |
| 114 | |
| 115 | } // namespace |
| 116 |
nothing calls this directly
no test coverage detected