Skip to content

Commit

Permalink
[TIR] Handle DeclBuffer in StorageFlatten's input (apache#15042)
Browse files Browse the repository at this point in the history
This is a subset of changes, being split out from
apache#14778 into independent portions.
  • Loading branch information
Lunderberg authored and junrushao committed Jun 22, 2023
1 parent a2f4a09 commit 6dc56c3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
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()

0 comments on commit 6dc56c3

Please sign in to comment.