Merged
Add scalar limit check for int and float #5812
gaoxiang618Add on Aug 31
Add scalar limit check for int and float #5812
Merged
gaoxiang618Add on Aug 31
gaoxiang618
gaoxiang618Collaborators
Aug 31
  1. 增加对于pl和vf接口的scalar范围校验
  2. 修正vf接口的scalar类型推导
likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for gaoxiang618's contribution.)
gaoxiang618gaoxiang618Collaborators
Aug 31 Created pull request, commit bb80d562
atomgit-bot
atomgit-bot
Aug 31 Comments:

变更摘要

该 PR 为 int/float 标量在进入 IR 之前增加范围校验:新增统一的数值范围基础设施(pypto_pro/ir/_limits.py 与 pypto_pro/language/parser/diagnostics/_range.py),以 FinalRejectionError 拒绝超出 dtype 可表示范围或超出 ir.ConstInt 存储带 [INT64_MIN, UINT64_MAX] 的常量,并将 ConstInt 的无符号折叠存储(int64_t 兼作 uint64_t 位镜像)在打印与代码生成阶段按 dtype 有符号性正确还原;同时把硬件资源 ID 上限统一为共享常量并由各处复用同一套 check_in_range 检查。

主要改动

  • 新增数值范围校验基础设施:新增 _limits.py(NumericLimits、limits_of、fits、fits_storage_int、to_storage_int、from_storage_int,含各浮点格式最大有限值与 BOOL/INDEX 特例)和 diagnostics/_range.py(make_const_int、check_fits_dtype、check_in_range、check_ir_int、check_const_expr_fits_dtype、range_message),统一"<subject> must be in [lo, hi], got <value>"消息语法,并提供 error= 参数供非解析路径抛出普通 ValueError。
  • 字面量标量按 dtype 做 API 级范围校验:_call_parser.py 新增 _VF_SCALAR_OPERAND_OPS 与 _check_vf_scalar_operands,对 vf.adds/subs/muls/mins/maxs/axpy 等操作的常量标量操作数按元素 dtype 校验(shift_left/right、bit_cast 豁免);scalar_ops.py 的 pl.const 按显式 dtype 校验;_utils.py、_expr_evaluator.py、_expression_parser.py 中整数常量统一改走 make_const_int(INDEX 占位符在值超过 INT64_MAX 时落定为 UINT64 并折叠存储)。
  • 常量打印与代码生成支持往返和无符号折叠:printer.h/cpp 公开 FormatFloatLiteral(整数值保留 .0、超大值用科学计数法保证往返)与 FormatIntLiteral(按 dtype 有符号性拼写),codegen_base.h/cpp 新增 FormatIntCLiteral(仅对折叠为负的 uint64 拼 uLL 后缀),io_text_dumper.cpp、cce_codegen.cpp、TryGenerateSimpleExprString 等改用上述函数。
  • 硬件资源上限统一为共享常量并复用范围检查:system_ops.py 将私有 _MAX_EVENT_ID/_MAX_FLAG_EVENT_ID/_MAX_MUTEX_ID 改为导出的含端点常量 MAX_EVENT_ID=15、MAX_FLAG_EVENT_ID=7、MAX_MUTEX_ID=31,_buffer_parser.py(make_tile_group mutex)、_cross_core_scanner.py、_analyzer.py、_call_parser.py(SIMT max_threads ≤ 2048)、_tiling.py(数组大小 ≤ 2048)统一改用 check_in_range。
  • CCE 后端标量强转逻辑重写:backend_cce_vf_ops.cpp 的 CoerceScalarToInt 由逐 dtype 分支改为通用实现——按 src_dt 位宽截断、无符号加 u 后缀、有符号做符号扩展,配合前端新增的标量范围校验避免静默回绕。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 31 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 31 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 31 Comments:

CLA Signature Pass

gaoxiang618, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
121 messages are collapsed here。 See more
lidongsheng
lidongshengCollaborators
Sep 3 Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Sep 3 Added label: lgtm
CANN-robot
CANN-robotCollaborators
Sep 3 Comments:

The MR can not be merged, because of CodeReview discussion not resolved

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
poorwillpoorwillCollaborators
Sep 3 Resolved final issue
CANN-robotCANN-robotCollaborators
Sep 3 Merged pull request