[Bug][Relax] FoldConstant substitutes immutable constant into call_tir_inplace target, silently corrupting the constant pool across invocations

未关闭
#21,121 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp, python
领域
backend, compilers

调研方向

Start in src/relax/transform/fold_constant.cc, especially ConstantFolder::VisitExpr_(const CallNode*), and trace how folded constants are substituted into relax.call_tir_inplace arguments. Read the handling of inplace_indices and the related normalization in src/relax/op/op.cc; the reproduction in the issue gives the first behavior to verify. Done means repeated calls, including calls from a new VM using the same executable, return the unchanged expected result without mutating the constant pool.

由索引模型根据 Issue 内容生成。

描述

needs-triage type: bug
Expected behavior

Running a compiled Relax function y = broadcast_to(0.0, [4]) + x with x = [1.0, 1.0, 1.0, 1.0] should return [1.0, 1.0, 1.0, 1.0] on every invocation, regardless of whether DataflowUseInplaceCalls and FoldConstant are both enabled.

Actual behavior

When DataflowUseInplaceCalls runs before LegalizeOps and FoldConstant, the compiled executable silently mutates its constant pool in-place on every call, producing [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4] on vm1, and [5, 5, 5, 5] even on a brand-new VirtualMachine instance created from the same executable.

Inspecting the Relax IR right after FoldConstant shows that metadata["ir.GenericConst"][0] is substituted directly into the mutated argument slot (inplace_indices=[0]) of R.call_tir_inplace:

@R.function
def main(x: R.Tensor((4,), dtype="float32")) -> R.Tensor((4,), dtype="float32"):
    with R.dataflow():
        y: R.Tensor((4,), dtype="float32") = R.call_tir_inplace(
            add_inplace,
            (metadata["ir.GenericConst"][0], x),
            out_ty=R.Tensor((4,), dtype="float32"),
            inplace_indices=[0],
        )
        R.output(y)
    return y

Removing FoldConstant (control) produces [1.0, 1.0, 1.0, 1.0] on all 5 calls across both VMs.

Environment
  • TVM: 0.27.0 (7b5acb32bd271342a24130f4ef439ff6c2e52c6c)
  • Upstream status: verified still unfixed on apache/tvm main (84657c01e5d3672cfd299bd3d5c77bff3888a1a9, 2026-12-02) — src/relax/transform/fold_constant.cc still has no handling of call_tir_inplace / inplace_indices
  • OS: Linux x86_64
  • Note: Distinct from #19577 (concat alias) and #20904 (RewriteDataflowReshape + call_tir_inplace).
Steps to reproduce
import numpy as np
import tvm
from tvm import relax
from tvm.script import ir as I, relax as R

@I.ir_module
class Mod:
    @R.function
    def main(x: R.Tensor((4,), "float32")) -> R.Tensor((4,), "float32"):
        with R.dataflow():
            z = R.broadcast_to(R.const(0.0, "float32"), R.shape([4]))
            y = R.add(z, x)
            R.output(y)
        return y

passes = [
    relax.transform.DataflowUseInplaceCalls(),
    relax.transform.LegalizeOps(),
    relax.transform.FoldConstant(),
]
with tvm.target.Target("llvm"), tvm.transform.PassContext(opt_level=3):
    mod = tvm.transform.Sequential(passes)(Mod)
    ex = tvm.compile(mod, target="llvm")

x = tvm.runtime.tensor(np.ones((4,), dtype="float32"), device=tvm.cpu())
vm1 = relax.VirtualMachine(ex, tvm.cpu())
print([vm1["main"](x).numpy().tolist() for _ in range(4)])
# Observed: [[1.0, 1.0, 1.0, 1.0], [2.0, 2.0, 2.0, 2.0], [3.0, 3.0, 3.0, 3.0], [4.0, 4.0, 4.0, 4.0]]
vm2 = relax.VirtualMachine(ex, tvm.cpu())
print(vm2["main"](x).numpy().tolist())
# Observed: [5.0, 5.0, 5.0, 5.0]
Suggested fix

In src/relax/transform/fold_constant.cc (ConstantFolder::VisitExpr_(const CallNode*)), VisitExprPostOrder_ substitutes folded GenericConst values into all argument positions of every downstream CallNode without checking CallTIRInplaceAttrs::inplace_indices. Either or both:

  1. In ConstantFolder, detect when a folded GenericConst is substituted into a mutated input slot of relax.call_tir_inplace, and either downgrade that slot out of inplace_indices (converting to out-of-place call_tir when no in-place slots remain) or refuse to fold bindings used as in-place mutation targets.
  2. Additionally, in NormalizeCallTIRInPlace (src/relax/op/op.cc) or the WellFormed checker, reject GenericConst (immutable constant) operands in inplace_indices slots so constant-pool mutation cannot silently pass lowering.
主要语言
Python
星标
13.9k
派生
4.1k
平均合并
4 天 22 小时
30 天内合并 PR
22

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

apache/tvm 的其他 Issue

查看 apache/tvm 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。