| 64 | } |
| 65 | |
| 66 | void trim_module(module& m, std::size_t loc, std::size_t n) |
| 67 | { |
| 68 | if(loc > m.size()) |
| 69 | MIGRAPHX_THROW("Trim out of range."); |
| 70 | auto last = std::prev(m.end(), loc); |
| 71 | auto start = std::prev(last, n); |
| 72 | m.remove_instructions(last, m.end()); |
| 73 | if(n == 0) |
| 74 | return; |
| 75 | if(n > m.size()) |
| 76 | MIGRAPHX_THROW("Trim size out of range."); |
| 77 | std::unordered_map<instruction_ref, instruction_ref> map_ins; |
| 78 | std::unordered_set<instruction_ref> instruction_set; |
| 79 | auto instructions = range(start, m.end()); |
| 80 | for(instruction_ref ins : iterator_for(instructions)) |
| 81 | { |
| 82 | instruction_set.insert(ins); |
| 83 | for(auto input : ins->inputs()) |
| 84 | { |
| 85 | if(contains(instruction_set, input)) |
| 86 | continue; |
| 87 | auto arg = capture_arg(instruction_set, input); |
| 88 | auto placeholder = add_placeholder(m, arg); |
| 89 | assert(placeholder->get_shape() == arg->get_shape()); |
| 90 | if(placeholder == arg) |
| 91 | continue; |
| 92 | instruction_set.insert(placeholder); |
| 93 | map_ins[arg] = placeholder; |
| 94 | } |
| 95 | } |
| 96 | for(auto [old_ins, new_ins] : map_ins) |
| 97 | m.replace_instruction(old_ins, new_ins); |
| 98 | run_passes(m, {dead_code_elimination{}}); |
| 99 | for(auto pins : m.get_parameters()) |
| 100 | { |
| 101 | if(not pins->outputs().empty()) |
| 102 | continue; |
| 103 | m.remove_instruction(pins); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | } // namespace MIGRAPHX_INLINE_NS |
| 108 | } // namespace driver |
no test coverage detected