Skip to content

Commit e835762

Browse files
[Enhancement] Optimize loop body handling in IR (#749)
- Updated the loop body construction in `ir.cc` to conditionally include an output statement based on the analyzable condition of the `waves` variable. - This change enhances performance by avoiding unnecessary statement wrapping when the condition is met, improving the efficiency of loop execution. Co-authored-by: LeiWang1999 <leiwang1999@outlook.com>
1 parent 6b12502 commit e835762

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/ir.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,13 @@ ForFrame PersistentFor(Array<PrimExpr> domain, PrimExpr wave_size,
158158
tvm::tir::Call(DataType::Handle(), tvm::tl::loop_break(), {})),
159159
Stmt());
160160

161-
Stmt outer = For(loop_var, 0, waves, ForKind::kSerial,
162-
SeqStmt({out_if, body}), std::nullopt, anno);
161+
arith::Analyzer analyzer;
162+
Stmt new_body = body;
163+
if (analyzer.CanProveGreaterEqual(waves, 2)) {
164+
new_body = SeqStmt({out_if, body});
165+
}
166+
Stmt outer =
167+
For(loop_var, 0, waves, ForKind::kSerial, new_body, std::nullopt, anno);
163168
for (int i = 0; i < vars.size() - 1; ++i) {
164169
outer = tvm::tir::LetStmt(vars[i], idxs[i + 1], outer);
165170
}

0 commit comments

Comments
 (0)