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

Aperta
#21,122 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
49/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
compilers

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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).
Lingua principale
Python
Stelle
13.9k
Fork
4.1k
Merge medio
4g 22h
PR unite (30g)
22

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di apache/tvm

Tutte le issue di apache/tvm

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.