Skip to content

Commit 7e9d186

Browse files
Weiming Zhaoweimingzha0
Weiming Zhao
authored andcommitted
[DCE] Support removing dead loop body
1 parent ac5adc1 commit 7e9d186

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/target/generic_cpp/generic_cxx_codegen.cc

+3
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,9 @@ void GenericCXXCodeGen::RunOnFunction(Function& function) {
768768
void GenericCXXCodeGen::RunOnConstant(Constant& constant, bool decl) {
769769
const auto& uses = constant.GetIthResultUses(0);
770770
bool only_used_by_reshape = true;
771+
if (uses.empty()) {
772+
return;
773+
}
771774
for (const auto& u : uses) {
772775
if (!IsA<Instruction>(u.GetUse()) ||
773776
DynCast<Instruction>(u.GetUse())->GetOpCode() != OpCode::RESHAPE ||

lib/transforms/dce.cc

+15
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424

2525
namespace halo {
2626

27+
static void RemoveLoopBody(LoopInst* loop_inst) {
28+
auto body = loop_inst->GetBody();
29+
auto return_inst = body->GetReturnInst();
30+
if (return_inst != nullptr) {
31+
// Drop all the operands of the return instruction so the rest of the body
32+
// loop will be DCE'ed automatically.
33+
// Note that the return inst cannot be erased because the current legalizer
34+
// will try to append one if no return inst exists for a block.
35+
return_inst->DropAllOperands();
36+
}
37+
}
38+
2739
bool DCE::RunOnBasicBlock(BasicBlock* bb) {
2840
bool changed = false;
2941
std::set<Instruction*> dead_instrs;
@@ -42,6 +54,9 @@ bool DCE::RunOnBasicBlock(BasicBlock* bb) {
4254
for (auto it = bb->begin(), e = bb->end(); it != e;) {
4355
Instruction* inst = it->get();
4456
if (dead_instrs.count(inst) > 0) {
57+
if (inst->GetOpCode() == OpCode::LOOP) {
58+
RemoveLoopBody(DynCast<LoopInst>(inst));
59+
}
4560
it = bb->Instructions().erase(it);
4661
} else {
4762
it = std::next(it);

0 commit comments

Comments
 (0)