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

Abierto
#21,122 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 3 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
49/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python
Área
compilers

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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).
Lenguaje dominante
Python
Estrellas
13.9k
Forks
4.1k
Merge medio
4 d 22 h
PR fusionados (30 d)
22

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de apache/tvm

Todos los issues de apache/tvm

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.