#20493·tvm

[Bug][Relax] RemoveUnusedOutputs changes a closure callee to return an empty tuple

Author: Yuhx141Created Sep 30, 2026Updated Sep 30, 2026

Description

relax.transform.RemoveUnusedOutputs() tracks tuple fields used by direct calls to a private function. A call through R.make_closure and R.invoke_pure_closure is not attributed to the private function, so all outputs are considered unused. The pass changes the callee to return an empty tuple while the closure call still expects the original tuple type. The resulting module is not well formed.

Environment

  • Ubuntu 24.04.4 LTS, x86-64
  • TVM 0.26.dev0
  • Runtime reproduction: e269315c90e3a061c9e1c77b370ce883b1b223f4
  • LLVM 18.1.3, CPU bytecode VM

Current upstream main was 2393db61bbd0100f048b1a6d3c2519fb70914564 when checked on 2026-09-30. src/relax/transform/remove_unused_outputs.cc has SHA-256 5b07b27de645d2e398cc7ccc5fe7d0308dc6a238542e47b516f4aa89e559b47c at both revisions. I did not have a binary build for that exact main revision, so the runtime reproduction below is from the stated fixed revision; the current implementation is byte-identical.

Minimal reproducer

python
from tvm import relax
from tvm.script import ir as I
from tvm.script import relax as R


@I.ir_module
class Before:
    @R.function(private=True)
    def outputs(
        value: R.Tensor([2], "float32"),
        env: R.Tensor([2], "float32"),
    ) -> R.Tuple(R.Tensor([2], "float32"), R.Tensor([2], "float32")):
        return (value, R.add(value, env))

    @R.function
    def main(
        x: R.Tensor([2], "float32"),
        y: R.Tensor([2], "float32"),
    ):
        closure = R.make_closure(Before.outputs, (x,))
        result = R.invoke_pure_closure(
            closure,
            (y,),
            ty_args=R.Tuple(
                R.Tensor([2], "float32"),
                R.Tensor([2], "float32"),
            ),
        )
        return result[0]


assert relax.analysis.check_well_formed(Before, check_ty=True)
After = relax.transform.RemoveUnusedOutputs()(Before)
assert relax.analysis.check_well_formed(After, check_ty=True)  # fails

Expected behavior

The pass should account for tuple fields observed through closure invocation, or skip changing a private function whose first-class uses are unsupported. A well-formed source module should remain well formed.

Actual behavior

The source module is well formed. After the pass, outputs returns R.Tuple() while invoke_pure_closure still expects the original two-field tuple. The target fails relax.analysis.check_well_formed(..., check_ty=True).

The result reproduces for tuple arities 2 through 7. Paired direct-call controls at every arity are rewritten successfully, remain well formed, and match the source exactly under the LLVM VM.

The tuple-returning closure is not used as a value oracle: this LLVM Relax VM cannot lower the source because the closure result is inferred as R.Any (The type of Tuple must be TupleType). The bug claim is therefore limited to the native invariant that a pass accepted a well-formed module and returned an ill-formed one.

Suspected cause

PartialTupleUsageCollector::GetCalleeUsageMask only recognizes a CallNode whose op is the private GlobalVar (remove_unused_outputs.cc, lines 124-138). A closure invocation calls relax.invoke_pure_closure, so the usage mask for outputs remains entirely false. The call-site mutator likewise updates only direct GlobalVar calls (lines 197-216).

Duplicate check

I searched existing TVM issues and pull requests using the pass and closure API names. I found reports about tuple use after direct calls, but none with the same cause: a private function used through a closure is not counted by RemoveUnusedOutputs.

I can send a fix PR and help with follow-up testing if this diagnosis looks right.

Triage

  • needs-triage
  • type: bug