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] Handle DeclBuffer in StorageFlatten's input #15042

Merged
merged 1 commit into from
Jun 7, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/tir/transforms/storage_flatten.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,15 @@ class StorageFlattener : public StmtExprMutator {
return body;
}

Stmt VisitStmt_(const DeclBufferNode* op) final {
auto node = Downcast<DeclBuffer>(StmtExprMutator::VisitStmt_(op));
const BufferEntry& entry = GetBufferEntry(node->buffer);
if (!entry.flattened_buffer.same_as(node->buffer)) {
node.CopyOnWrite()->buffer = entry.flattened_buffer;
}
return std::move(node);
}

// AllocateNodes may be present from tvm.tir.ir_builder. This can
// be simplified in the future by having AllocateNode hold a buffer,
// rather than a buffer_var.
Expand Down
16 changes: 16 additions & 0 deletions tests/python/unittest/test_tir_transform_storage_flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,21 @@ def test_flatten_tir():
) # StorageFlatten should do nothing to TIR functions


class TestPreserveDeclBuffer(tvm.testing.CompareBeforeAfter):
transform = tvm.tir.transform.StorageFlatten(64)

def before():
T.func_attr({"from_legacy_te_schedule": True})
A = T.decl_buffer([16, 16], "float32")
for i, j in T.grid(16, 16):
A[i, j] = 0.0

def expected():
T.func_attr({"from_legacy_te_schedule": True})
A = T.decl_buffer([256], "float32")
for i, j in T.grid(16, 16):
A[i * 16 + j] = 0.0


if __name__ == "__main__":
tvm.testing.main()