[Unity] Extend RemoveAllUnused to support relax::Expr#15807
[Unity] Extend RemoveAllUnused to support relax::Expr#15807csullivan merged 2 commits intoapache:unityfrom
Conversation
Prior to this commit, the `RemoveAllUnused` utility could only apply to `relax::Function` instances. This commit extends the allowed usage to apply to any `relax::Expr` that contains variable bindings, such as `relax::SeqExpr`.
c608e43 to
d8d7a1c
Compare
Because `RemoveAllUnused` now handles unused variables in a non-dataflow binding block, passes that use it as a utility function needed a few tests updated.
|
This PR has a bug when handling a use-def chain which ends at an impure call in a binding block. Then b is an unused var (by your definition), but not an impure call, so it will be removed, which breaks the program. |
|
btw, since we have an assumption that binding block is impure, why do we try to remove unused in binding block? |
Thank you, and I can reproduce the error with the test case below. def test_binding_block_keep_pure_func_used_only_for_impure():
"""Remove unused dataflow bindings
Removal of unused bindings may not remove side effects. Unused
bindings whose value is an impure operation (e.g. `R.call_packed`)
may not be removed, nor may any of their inputs.
"""
@R.function(private=True)
def before(x: R.Tensor((32, 32), "int32")):
y = x * R.const(2)
z = R.call_packed(
"function_maybe_with_side_effects", y, sinfo_args=(R.Tensor((32, 32), "int32"))
)
return R.tuple()
expected = before
after = remove_all_unused(before)
tvm.ir.assert_structural_equal(expected, after)For the purpose of removing unused bindings, an argument to an impure function is equivalent to an output, and should be retained. Thankfully, this should be a pretty straightforward change to make.
I'd phrase it slightly differently: A In context of this PR, this check is encoded here. A binding is removable if it is unused, and if it's computation is pure, where purity can be established either from being within a |
|
@jinhongyii I've opened #16036, which updates the variable usage tracking to check for arguments to impure functions. |
Prior to this commit, the
RemoveAllUnusedutility could only apply torelax::Functioninstances. This commit extends the allowed usage to apply to anyrelax::Exprthat contains variable bindings, such asrelax::SeqExpr.