Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIR] Bugfix in StorageFlatten, index flattening in PrefetchNode #10657

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/tir/transforms/storage_flatten.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1489,10 +1489,6 @@ class StorageFlattener : public StmtExprMutator {
}

Stmt VisitStmt_(const PrefetchNode* op) final {
Stmt stmt = StmtExprMutator::VisitStmt_(op);
op = stmt.as<PrefetchNode>();
ICHECK(op != nullptr);

const BufferEntry& e = GetBufferEntry(op->buffer);

ICHECK(e.in_scope) << "Cannot prefetch " << op->buffer << ", out of scope.";
Expand Down Expand Up @@ -1524,6 +1520,8 @@ class StorageFlattener : public StmtExprMutator {
vars.push_back(Var("prefetch." + func_name + "." + std::to_string(i), DataType::Int(32)));
args.push_back(vars.back() + op->bounds[i]->min);
}

Stmt stmt = GetRef<Stmt>(op);
for (int i = starts; i >= 0; --i) {
if (i < starts) {
stmt = For(vars[i], 0, op->bounds[i]->extent, ForKind::kSerial, stmt);
Expand All @@ -1536,7 +1534,7 @@ class StorageFlattener : public StmtExprMutator {
stmt = For(vars[i], 0, extent, ForKind::kSerial, stmt);
}
}
return stmt;
return this->VisitStmt(stmt);
}

PrimExpr VisitExpr_(const ProducerLoadNode* op) final {
Expand Down
6 changes: 6 additions & 0 deletions tests/python/unittest/test_tir_transform_storage_flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ def test_flatten_prefetch():
assert isinstance(stmt.body, tvm.tir.For)
assert stmt.body.extent.value == 2

def assert_flat_loads(stmt):
if isinstance(stmt, tvm.tir.BufferLoad):
assert len(stmt.indices) == 1, "All prefetch indices should be flattened"

tvm.tir.stmt_functor.post_order_visit(stmt, assert_flat_loads)


def test_flatten_storage_align():
m = 8
Expand Down