[Bug][TIR] VectorizeLoop silently miscompiles lane-private AllocBuffer inside a vectorized loop by splitting producer/consumer into separate scalar loops

オープン
#21,122 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
49/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
compilers

調査の方向性

Start with src/tirx/transform/vectorize_loop.cc, especially the Vectorizer handling of AllocBufferNode and Scalarize around line 1114. Use the provided S-TIR schedule and reproduction script to confirm the incorrect output, then inspect nearby vectorization tests for coverage. Done means compilation preserves the unscheduled function’s output for this case and the regression is covered by a test.

索引モデルが issue の本文から書いたものです。

説明

needs-triage type: bug
Expected behavior

Compiling a valid S-TIR schedule (cache_write("local") -> reverse_compute_at -> split -> vectorize) with default tvm.compile(mod, target="llvm") must preserve the semantics of the unscheduled PrimFunc, outputting [1, 2, 3, ..., 18].

Actual behavior

tirx.transform.VectorizeLoop (src/tirx/transform/vectorize_loop.cc) silently miscompiles the function to output [4, 4, 4, 4, 8, 8, 8, 8, 12, 12, 12, 12, 16, 16, 16, 16, 18, 18].

Immediately before VectorizeLoop, PlanAndUpdateBufferAllocationLocation + CompactBufferAllocation legally place a lane-private scalar buffer B_local = T.alloc_local((1,)) inside for ax0_1 in T.vectorized(4):. Because Vectorizer does not handle AllocBufferNode (neither expanding its allocation by var_lanes_ nor forcing the enclosing SeqStmt to scalarize as a unit), each predicated IfThenElse independently calls Scalarize(stmt) (vectorize_loop.cc:1114). This fissions the body into two separate for ax0_1_s in range(4) loops that share the single size-1 B_local[0] slot: all 4 lanes overwrite B_local[0] in the first loop, and all 4 lanes read back only the final lane's value in the second loop.

Environment
  • TVM: 0.27.0 (7b5acb32bd271342a24130f4ef439ff6c2e52c6c)
  • Upstream status: verified still unfixed on apache/tvm main (84657c01e5d3672cfd299bd3d5c77bff3888a1a9, 2026-12-02) / submodule apache/tvm-tirx (f43b3030d33196d3a5f21e56b37549a6d3a5c70b, path 3rdparty/tvm-tirx/src/tirx/transform/vectorize_loop.cc)
  • OS: Linux x86_64
Steps to reproduce
import numpy as np
import tvm
from tvm import s_tir
from tvm.script import tirx as T

@T.prim_func(s_tir=True)
def copy18_1d(A: T.Buffer((1, 18), "float32"), B: T.Buffer((1, 18), "float32")):
    for ax0 in range(18):
        with T.sblock("copy"):
            v0 = T.axis.spatial(18, ax0)
            B[0, v0] = A[0, v0]

sch = s_tir.Schedule(copy18_1d)
blk = sch.get_sblock("copy")
(loop,) = sch.get_loops(blk)
cw = sch.cache_write(blk, 0, "local")
sch.reverse_compute_at(cw, loop, preserve_unit_loops=True)
_, inner = sch.split(sch.get_loops(blk)[-1], factors=[None, 4])
sch.vectorize(inner)

x = np.arange(1, 19, dtype=np.float32).reshape(1, 18)
out = tvm.runtime.tensor(np.zeros_like(x))
tvm.compile(sch.mod, target="llvm")(tvm.runtime.tensor(x), out)
print(out.numpy()[0])
# Actual:   [ 4.  4.  4.  4.  8.  8.  8.  8. 12. 12. 12. 12. 16. 16. 16. 16. 18. 18.]
# Expected: [ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15. 16. 17. 18.]
Suggested fix

In src/tirx/transform/vectorize_loop.cc, either:

  1. When a SeqStmt inside a ForKind::kVectorized loop contains an AllocBuffer whose buffer is written/read across child statements, scalarize the entire AllocBuffer scope together (instead of fissioning individual child statements around a shared size-1 buffer), OR
  2. Expand lane-private AllocBuffer allocations inside a vectorized loop by var_lanes_ (or reject vectorize in s_tir.Schedule when a loop body contains lane-private local buffers that cannot be widened).
主要言語
Python
スター
13.9k
フォーク
4.1k
平均マージ
4日 22時間
マージ済み PR(30日)
22

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/tvm のほかの issue

apache/tvm の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。