Skip to content

[Bug][PTODSL/VPTO] [阻塞]合并多个 Vector Kernel 时 init_core 生成重复符号并导致 fatobj 链接失败 #1582

Description

@erhsh

问题概述

在 PTOAS 0.65 中,将两个包含 pto.init_core() 的 VPTO vector kernel 通过 pto.merge_jit_modules() 合并后,PTOAS 在生成 fatobj 时链接失败:

ld.lld: error: duplicate symbol: init_core__ptodsl_0b526dc856.vector

两个 kernel 单独编译均成功;只有多 kernel 合并场景失败。

环境

  • CANN: 9.3.0
  • PTOAS: 0.65
  • Python: 3.11
  • PTO target architecture: a5
  • PTO backend: vpto
  • Device compiler architecture: dav-c310

最小复现

from ptodsl import pto

@pto.jit(name="main_kernel", kernel_kind="vector",
         target="a5", mode="explicit")
def main_kernel(a: pto.ptr(pto.f32, "gm"),
                b: pto.ptr(pto.f32, "gm"),
                c: pto.ptr(pto.f32, "gm")):
    pto.init_core()
    # vector/SIMT operations ...

@pto.jit(name="main_kernel_1", kernel_kind="vector",
         target="a5", mode="explicit")
def main_kernel_1(c: pto.ptr(pto.f32, "gm")):
    pto.init_core()
    # vector/SIMT operations ...

merged = str(pto.merge_jit_modules(main_kernel, main_kernel_1))
open("merged.pto", "w").write(merged)

执行:

ptoas --pto-arch=a5 --pto-backend=vpto merged.pto -o merged.fatobj.o

实际结果:

PTOAS: VF_SIMT size verification passed; no patch required
PTOAS: VF_SIMT size verification passed; no patch required
Error: fatobj link failed

ld.lld: error: duplicate symbol: init_core__ptodsl_0b526dc856.vector

TileLang 复现

pytest examples/ascend/test_simtvf_multi_kernel.py::test_simtvf_multi_kernel[pto]

测试程序包含两个独立的 T.Kernel/T.SimtVF 区域,分别执行向量加法和向量缩放。TileLang 生成的两个 PTODSL kernel 都包含:

pto.init_core()

测试在 PTO fatobj link 阶段失败。

分析结果

合并后的 PTO IR 中出现两份同名 helper:

func.func @init_core__ptodsl_0b526dc856()

两个 entry 都调用该符号:

main_kernel   -> init_core__ptodsl_0b526dc856
main_kernel_1 -> init_core__ptodsl_0b526dc856

两个 helper 的函数体相同,负责配置 control register、UB/OUT loop size 和 store atomic configuration。

但 merge_jit_modules() 保留了两个定义,PTOAS 随后将它们转换为同名全局符号:

init_core__ptodsl_0b526dc856.vector

最终 fatobj link 阶段报 duplicate symbol。

已完成的对照实验

两个 kernel 分别编译

分别执行:

ptoas --pto-arch=a5 --pto-backend=vpto main_kernel.pto -o main_kernel.fatobj.o
ptoas --pto-arch=a5 --pto-backend=vpto main_kernel_1.pto -o main_kernel_1.fatobj.o

结果均成功:

rc=0
PTOAS: VF_SIMT size verification passed; no patch required

去掉 pto.init_core()

去掉两个 kernel 中的 pto.init_core() 后:

  • merge_jit_modules() 生成的 PTO 可以成功编译;
  • fatobj link 成功;
  • launch object 和 shared library 均成功生成。

因此问题与多 kernel host launch、参数 ABI 或 SIMT lowering 无关,触发条件是多个 kernel 同时包含 pto.init_core()。

期望行为

以下任一方案都可以解决问题:

  1. merge_jit_modules() 对完全相同的 helper 自动去重;
  2. helper 在每个 kernel/module 内部化,避免导出冲突的全局符号;
  3. helper 按 kernel 名称唯一化;
  4. PTOAS 在 lowering/link 阶段正确处理相同逻辑 helper。

临时规避

不能简单删除 pto.init_core() 作为通用 workaround,因为该函数负责硬件控制寄存器和存储配置,在复杂 kernel 中删除可能导致错误行为。

当前只能避免合并多个包含 pto.init_core() 的 vector kernel,或使用包含相关修复的 PTOAS/PTODSL 版本。

请确认该问题应由 PTODSL module merge、PTOAS lowering,还是 fatobj linker 侧修复。

Activity

  1. changed the title [-][Bug][PTODSL/VPTO] Multiple vector kernels with init_core fail fatobj link due to duplicate symbol[/-] [+][Bug][PTODSL/VPTO] [阻塞]合并多个 Vector Kernel 时 init_core 生成重复符号并导致 fatobj 链接失败[/+] on Sep 24, 2026
  2. TaoTao-real commented on Sep 28, 2026

    @TaoTao-real
    Contributor

    已确认这是 PTODSL/PTOAS 的符号可见性问题,修复已提交:

    merge_jit_modules() 会保留独立的 backend child module,并分别生成 fatobj。此前普通 @pto.func helper(包括 init_core)保持外部可见;当两个 kernel 产生同名 specialization 时,两个 fatobj 都导出相同全局符号,最终链接报 duplicate symbol: init_core...。

    MR 将 child module 中的普通 helper 标记为 private;真正的 kernel primary function 仍通过独立路径保持 public/external,并新增了合并两个 vector kernel 的回归测试。

    验证结果:

    • test_stdlib_init_core.py 通过;
    • test_jit_compile.py 全量通过;
    • CANN 9.2 对照验证:修复前稳定复现 duplicate symbol,修复后相同输入成功生成 fatobj。
  3. erhsh commented on Oct 9, 2026

    @erhsh
    ContributorAuthor

    使用930weekly版本问题还在:

    ptoas --pto-arch=a5 --pto-backend=vpto merged.pto -o merged.fatobj.o
    Warning: PTO_ISA_PATH/PTO_ISA_ROOT is not set; C++ device object emission may fail to include pto/pto-inst.hpp.
    Warning: Bisheng scheduler auto: SIMD VF stack report unavailable; keeping on, so auto behaves like --bisheng-vec-misched=on and no stack size is compared. Check that this Bisheng toolchain can emit SIMD VF stack reports with -mllvm --cce-res-usage.
    PTOAS: VF_SIMT size verification passed; no patch required
    Warning: Bisheng scheduler auto: SIMD VF stack report unavailable; keeping on, so auto behaves like --bisheng-vec-misched=on and no stack size is compared. Check that this Bisheng toolchain can emit SIMD VF stack reports with -mllvm --cce-res-usage.
    PTOAS: VF_SIMT size verification passed; no patch required
    Error: fatobj link failed
    Command: /data/caojian/env/CANN260930/cann-9.3.0/bin/bisheng --cce-fatobj-link --cce-aicore-arch=dav-c310 -r -o merged.fatobj.o /data/caojian/tmp/ptoas-vpto-fatobj-b8300e..o /data/caojian/tmp/ptoas-vpto-fatobj-f7f50e..o
    ld.lld: error: duplicate symbol: init_core__ptodsl_cccb7eefe6.vector
    >>> defined at ptoas.hivm.official.vector
    >>>            /data/caojian/tmp/ptoas-vpto-fatobj-b8300e..o-be2d4b.extract:(init_core__ptodsl_cccb7eefe6.vector)
    >>> defined at ptoas.hivm.official.vector
    >>>            /data/caojian/tmp/ptoas-vpto-fatobj-f7f50e..o-6449fc.extract:(.text+0x98)
    cceld: ccec ReturnCode: 1
    cceld: ExecutionFailed: 0
    cceld: ErrMsg: 
    bisheng: error: linker command failed with exit code 1 (use -v to see invocation)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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