Skip to content

[Bug][阻塞] PTODSL's for loop incorrectly carries variables initialized inside pto.vecscope() #1097

Description

@liggest

Component

PTODSL

Description

PTODSL 中,若 for ... in range(...) 循环中包含 pto.vecscope() 块,完全在 pto.vecscope() 内声明并读写的变量会被暴露到 for 循环中,成为循环内携带(carry)的变量

但由于变量只在 pto.vecscope() 块内创建,导致触发 UnboundLocalError,使得编译失败

Reproduction (minimal)

from ptodsl import pto


@pto.jit(kernel_kind="vector", target="a5", mode="explicit")
def kernel(n: pto.i32):
    for _ in range(0, n):
        with pto.vecscope():
            acc = pto.const(0, dtype=pto.i32)
            acc = acc + 1



print(kernel.compile().mlir_text())

Expected behavior

编译成功通过

Actual behavior / error logs

Traceback (most recent call last):
  File "ptodsl_for_vecscope.py", line 13, in <module>
    print(kernel.compile().mlir_text())
  File "ptodsl/ptodsl/_jit.py", line 290, in compile
    return self._compiler.compile(**constexpr_bindings)
  File "ptodsl/ptodsl/_kernel_compilation.py", line 138, in compile
    compiled.build()
  File "ptodsl/ptodsl/_tracing/artifacts.py", line 34, in build
    built = self._module_factory()
  File "ptodsl/ptodsl/_tracing/runtime.py", line 86, in build_module
    self.trace_entry(*args)
  File "ptodsl/ptodsl/_tracing/runtime.py", line 128, in trace_entry
    result = self._callback(*args, **kwargs)
  File "ptodsl_for_vecscope.py", line 3, in kernel
    
UnboundLocalError: local variable 'acc' referenced before assignment

Git commit

370e626

Host platform

Linux (x86_64)

Target Ascend arch (if relevant)

a5

PTOAS build level (if relevant)

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions