Skip to content

[Bug] PTODSL's ast_rewrite reports incorrect traceback line numbers #1052

Description

@liggest

Component

PTODSL

Description

PTODSL @pto.jitast_rewrite 默认开启,启用后,若编译期间在函数内发生异常,调用栈显示的行号可能是函数源码片段的相对行号,而不是它在文件中的实际行号

这会让报错指向完全无关的代码,增加定位难度

Reproduction (minimal)

# Copyright (c) 2026 Huawei Technologies Co., Ltd.
# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
# CANN Open Software License Agreement Version 2.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.

from ptodsl import pto


@pto.jit(target="a5")
def ast_rewrite_traceback():
    runtime_value = pto.const(1, dtype=pto.i32)
    _ = not runtime_value  # 实际出错位置


ast_rewrite_traceback.compile()

Expected behavior

编译报错后,调用栈中显示实际报错位置

Traceback (most recent call last):
  File "ast_rewrite_traceback.py", line 18, in <module>
    ast_rewrite_traceback.compile()
  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 "ast_rewrite_traceback.py", line 15, in ast_rewrite_traceback
    _ = not runtime_value  # 实际出错位置
  File "ptodsl/ptodsl/_surface_values.py", line 239, in __bool__
    raise native_python_control_flow_error("if/while condition")
ptodsl._diagnostics.PTODSLTracingMisuseError: native Python if/while condition cannot consume a PTODSL runtime value during tracing. This value is a device-side SSA/runtime-metadata value, not a Python bool/int. Use pto.if_(...) or pto.for_(...) for device-side control flow, or keep the bound/condition in pto.const_expr.

Actual behavior / error logs

编译报错后,调用栈中显示错误的位置和行号

Traceback (most recent call last):
  File "ast_rewrite_traceback.py", line 18, in <module>
    ast_rewrite_traceback.compile()
  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 "ast_rewrite_traceback.py", line 4, in ast_rewrite_traceback
    # Please refer to the License for details. You may not use this file except in compliance with the License.
  File "ptodsl/ptodsl/_surface_values.py", line 239, in __bool__
    raise native_python_control_flow_error("if/while condition")
ptodsl._diagnostics.PTODSLTracingMisuseError: native Python if/while condition cannot consume a PTODSL runtime value during tracing. This value is a device-side SSA/runtime-metadata value, not a Python bool/int. Use pto.if_(...) or pto.for_(...) for device-side control flow, or keep the bound/condition in pto.const_expr.

若手动关闭 ast_rewrite@pto.jit(target="a5", ast_rewrite=False),则可获得期望结果

Git commit

456ebb4

Host platform

None

Target Ascend arch (if relevant)

None

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