Skip to content

Commit

Permalink
[DCE] Support removing dead loop body
Browse files Browse the repository at this point in the history
  • Loading branch information
Weiming Zhao authored and weimingzha0 committed Mar 24, 2021
1 parent ac5adc1 commit 7e9d186
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/target/generic_cpp/generic_cxx_codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,9 @@ void GenericCXXCodeGen::RunOnFunction(Function& function) {
void GenericCXXCodeGen::RunOnConstant(Constant& constant, bool decl) {
const auto& uses = constant.GetIthResultUses(0);
bool only_used_by_reshape = true;
if (uses.empty()) {
return;
}
for (const auto& u : uses) {
if (!IsA<Instruction>(u.GetUse()) ||
DynCast<Instruction>(u.GetUse())->GetOpCode() != OpCode::RESHAPE ||
Expand Down
15 changes: 15 additions & 0 deletions lib/transforms/dce.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@

namespace halo {

static void RemoveLoopBody(LoopInst* loop_inst) {
auto body = loop_inst->GetBody();
auto return_inst = body->GetReturnInst();
if (return_inst != nullptr) {
// Drop all the operands of the return instruction so the rest of the body
// loop will be DCE'ed automatically.
// Note that the return inst cannot be erased because the current legalizer
// will try to append one if no return inst exists for a block.
return_inst->DropAllOperands();
}
}

bool DCE::RunOnBasicBlock(BasicBlock* bb) {
bool changed = false;
std::set<Instruction*> dead_instrs;
Expand All @@ -42,6 +54,9 @@ bool DCE::RunOnBasicBlock(BasicBlock* bb) {
for (auto it = bb->begin(), e = bb->end(); it != e;) {
Instruction* inst = it->get();
if (dead_instrs.count(inst) > 0) {
if (inst->GetOpCode() == OpCode::LOOP) {
RemoveLoopBody(DynCast<LoopInst>(inst));
}
it = bb->Instructions().erase(it);
} else {
it = std::next(it);
Expand Down

0 comments on commit 7e9d186

Please sign in to comment.