| 361 | } |
| 362 | |
| 363 | void SeqModifierForSublinearMemory::ModifyActionPlanner::make_discard_plan() { |
| 364 | ThinHashSet<Var*> cur_block_alive_vars; |
| 365 | std::vector<Opr*> cur_block_member; |
| 366 | VarSet cur_block_discard_vars; |
| 367 | |
| 368 | size_t nr_blocks = 0; |
| 369 | |
| 370 | auto flush_block_member = [&]() { |
| 371 | nr_blocks++; |
| 372 | auto begin = cur_block_member.front()->time, |
| 373 | end = cur_block_member.back()->time + 1; |
| 374 | for (auto i : cur_block_member) { |
| 375 | i->block_begin_time = begin; |
| 376 | i->block_end_time = end; |
| 377 | } |
| 378 | cur_block_member.clear(); |
| 379 | cur_block_alive_vars.clear(); |
| 380 | cur_block_discard_vars.clear(); |
| 381 | }; |
| 382 | |
| 383 | for (auto&& block : m_blocks) { |
| 384 | for (auto&& opr : block) { |
| 385 | cur_block_member.push_back(opr); |
| 386 | |
| 387 | for (auto i : opr->output) { |
| 388 | cur_block_alive_vars.insert(i); |
| 389 | i->next_access = 1; |
| 390 | } |
| 391 | |
| 392 | for (auto i : opr->input) { |
| 393 | if (opr == i->last_access_opr()) { |
| 394 | cur_block_alive_vars.erase(i); |
| 395 | i->next_access = None; |
| 396 | } else if (opr == i->visit_next_access()->opr) { |
| 397 | ++i->next_access.val(); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | // TODO: should rewrite for multi-outputs opr |
| 403 | // This loop only make sense for single-output oprs. Since all oprs |
| 404 | // only recompute once, it should serach best recomputing-time in opr-level |
| 405 | // rather than find best discarding-time in var-level for multi-outputs opr. |
| 406 | for (auto var : cur_block_alive_vars) { |
| 407 | if (is_bad_opr(var->owner_opr()->orig_opr)) |
| 408 | continue; |
| 409 | |
| 410 | Var::AccessRecord* best = nullptr; |
| 411 | auto&& rec = var->access_rec; |
| 412 | mgb_assert(var->next_access.val() >= 1); |
| 413 | |
| 414 | // find best future time to discard |
| 415 | for (size_t i = var->next_access.val() - 1; i < rec.size() - 1; ++i) { |
| 416 | if (!i && var->owner_opr()->output.size() == 1) { |
| 417 | // never discard output var directly |
| 418 | continue; |
| 419 | } |
| 420 |
nothing calls this directly
no test coverage detected