Skip to content

[Bug] PTODSL 可变变量会导致 SSA 值跨物理 Section 泄漏【阻塞】 #1095

Description

@erhsh

Component

Python bindings (python/)

Description

PTODSL 当前使用同一个 Python 可变变量环境,依次追踪相邻的
pto.section("cube")pto.section("vector")

当一个 Python 变量在某个物理 Section 内被重新赋值后,该变量可能会指向
这个 Section 内部定义的 SSA 值。后续追踪相邻 Section 时,可能错误地继续
使用这个 SSA 值。

这违反了物理 Section 之间的隔离要求。Cube 和 Vector Section 最终会被
拆分为不同的物理 Kernel,因此一个 Section 内定义的 SSA 值不能被另一个
Section 引用。

该问题主要在 mixed Cube/Vector Kernel(mixed=true)中暴露,因为其中的
Cube 和 Vector Section 会被拆分为不同的物理区域或函数。在普通的单 Section
Kernel 中,代码位于同一个顺序执行区域,正常的 SSA 支配关系可能会掩盖这个问题。

期望的 Section 语义如下:

  • 函数根作用域定义的值可以作为入口值被任意物理 Section 使用。
  • 在某个 Section 内对 Python 可变变量的重新赋值,只能影响该 Section。
  • 在物理 Section 内定义的值不能逃逸到相邻 Section 或函数根作用域。
  • 非法的跨 Section SSA 引用应在 Split CV 之前被明确诊断,而不是生成错误的
    物理 Kernel。

建议采用多层防护方案:

  1. PTODSL AST rewrite 将 pto.section() 视为独立的变量作用域边界。
  2. 每个物理 Section 使用函数根作用域提供的入口值独立追踪,而不是继承前一个
    Section 追踪结束后的 Python 可变状态。
  3. PTODSL tracing 检查 Section 内定义的 surface value 是否发生逃逸,包括关闭
    AST rewrite 的情况。
  4. PTOAS Split CV pass 增加跨 Section SSA 校验,同时覆盖 operation result 和
    block argument。

Reproduction (minimal)

from ptodsl import pto


@pto.jit(target="a5", mode="explicit")
def section_scope_repro():
    event_id = pto.const(1, dtype=pto.i32)
    one = pto.const(1, dtype=pto.i32)

    with pto.section("cube"):
        event_id = event_id + one
        pto.wait_flag("S", "MTE2", event_id=event_id)

    with pto.section("vector"):
        # 这里应该使用根作用域的入口值 1,
        # 而不是 Cube Section 内定义的值 2。
        pto.wait_flag("MTE2", "S", event_id=event_id)


print(section_scope_repro.compile().mlir_text())



python section_scope_repro.py


问题 IR 在语义上相当于:


%root = ... : i32
pto.section.cube {
  %cube_value = arith.addi %root, %root : i32
  // ...
}
pto.section.vector {
  // 非法:%cube_value 定义在相邻的 Cube Section 中。
  "some.op"(%cube_value) : (i32) -> ()
}

Expected behavior

每个物理 Section 都应从函数根作用域的入口状态开始执行。

在上述示例中,Cube Section 可以在自身内部生成并使用 event_id = 2,但
Vector Section 应独立使用根作用域定义的 event_id = 1。

如果代码直接在另一个物理 Section 或函数根作用域中使用某个 Section 内定义的
SSA 值,应在编译期间给出清晰的错误诊断。

Actual behavior / error logs

追踪 Cube Section 时,对 event_id 的重新赋值修改了共享的 Python 变量环境。
随后追踪 Vector Section 时,event_id 仍然指向 Cube Section 内生成的 SSA 值。

PTODSL 没有阻止该值逃逸,因此跨 Section 的 SSA 引用能够进入 Split CV
转换。Section 被拆分后,对应 SSA 值并没有定义在生成的物理 Kernel 中,从而
产生非法 IR 或错误的生成代码。

Git commit

9d0d6a5

Host platform

Linux (x86_64)

Target Ascend arch (if relevant)

a5

PTOAS build level (if relevant)

None

关联ISSUE & PR

此问题发现基于PR #1041
关联ISSUE #1076

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