已开启
[MLIR][Arith] arith.expand 的 f8E5M2→f32 扩展转换把 ±0 转成 NaN、±Inf 转成 65536、NaN 转成有限值 #128
cactusBalll创建于  8月30日
cactusBalll
8月30日 创建

【缺陷描述】:请补充详细的缺陷问题现象描述

mlir-opt --arith-expand='include-f8e5m2=true' 将 arith.extf %v : f8E5M2 to f32 展开为整数位操作(F8E5M2ExtFOpConverter,mlir/lib/Dialect/Arith/Transforms/ExpandOps.cpp:456)。对指数域全 0 的 ±0.0 与指数域全 1 的 ±Inf/NaN,转换结果错误:

输入 (f8E5M2) 实际 f32 正确 f32
0x00 (+0.0) 0x7FC00000 (NaN) 0x00000000
0x80 (-0.0) 0x7FC00000 (NaN) 0x80000000
0x7C (+Inf) 0x47800000 (65536.0) 0x7F800000 (+Inf)
0x7D (NaN) 0x47A00000 (98304.0) NaN
0xFF (-NaN) 0xC7E00000 (-114688.0) NaN

一、缺陷信息

  • 缺陷组件:MLIR Arith dialect(arith-expand pass,ExpandOps.cpp)
  • 缺陷类型:错误代码(silent miscompile,无任何诊断)
  • 根因:ExpandOps.cpp:543-566 的结果 select 链中,±0(exp=0 且 mantissa=0)不满足任何分支条件,落入兜底常量 NaN(0x7FC00000);指数域 31(Inf/NaN)完全没有特判,按"正规数"路径组装为有限值(ExpandOps.cpp:504-508 计算 float_exponent=143 后打包符号/尾数)
  • 缺陷引入提交:
    • 0133bb8c7d28aa34affeb322c1f35f87e4b25aa8 "Added f8e5m2 and f8e4m3fn support for the expand ops pass in the arith dialect"(经合并提交 3356bb977a37b12dcad52096f26e2fd147f00a18 "!407 [MLIR] Add Support for Triton3.2 + Triton-Shared" 进入分支)

【缺陷所属的os版本】

Ubuntu 22.04.3 LTS 缺陷位于编译器/MLIR 工具链本身,与操作系统版本无关。

【内核版本】

6.6.87.2-microsoft-standard-WSL2(uname -r)。缺陷与内核版本无关。

【缺陷所属软件及版本号】

LLVM 20.1.8

【环境信息】

编译选项:
CMAKE_BUILD_TYPE=RelWithDebInfo,LLVM_ENABLE_ASSERTIONS=ON,LLVM_ENABLE_PROJECTS="clang;mlir",LLVM_TARGETS_TO_BUILD="X86;AArch64"

COMMIT ID:35f464110c04e89bd0cab0e3fee4a70ec6ec2eb9

【问题复现步骤】

// BUG: arith.expand f8E5M2 extf mishandles 0, Inf and NaN.
// Expected (APFloat / IEEE 754 semantics):
//   0x00 (+0.0)  -> 0x00000000 (+0.0)
//   0x80 (-0.0)  -> 0x80000000 (-0.0)
//   0x7C (+Inf)  -> 0x7F800000 (+Inf)
//   0x7D (NaN)   -> any NaN
// Actual (see ACTUAL_OUTPUT.txt):
//   0x00 -> 0x7FC00000 (NaN)          [zero converted to NaN]
//   0x80 -> 0x7FC00000 (+NaN)         [-0 converted to +NaN]
//   0x7C -> 0x47800000 (65536.0)      [Inf converted to finite]
//   0x7D -> 0x47A00000 (98304.0)      [NaN converted to finite]
//   0xFF -> 0x47E00000 (245760.0)     [-NaN converted to finite]
module {
  func.func @t_poszero() -> i32 {
    %c = arith.constant 0 : i8
    %v = arith.bitcast %c : i8 to f8E5M2
    %r = arith.extf %v : f8E5M2 to f32
    %z = arith.bitcast %r : f32 to i32
    return %z : i32
  }
  func.func @t_negzero() -> i32 {
    %c = arith.constant 128 : i8
    %v = arith.bitcast %c : i8 to f8E5M2
    %r = arith.extf %v : f8E5M2 to f32
    %z = arith.bitcast %r : f32 to i32
    return %z : i32
  }
  func.func @t_inf() -> i32 {
    %c = arith.constant 124 : i8
    %v = arith.bitcast %c : i8 to f8E5M2
    %r = arith.extf %v : f8E5M2 to f32
    %z = arith.bitcast %r : f32 to i32
    return %z : i32
  }
  func.func @t_nan() -> i32 {
    %c = arith.constant 125 : i8
    %v = arith.bitcast %c : i8 to f8E5M2
    %r = arith.extf %v : f8E5M2 to f32
    %z = arith.bitcast %r : f32 to i32
    return %z : i32
  }
  func.func @t_negnan() -> i32 {
    %c = arith.constant 255 : i8
    %v = arith.bitcast %c : i8 to f8E5M2
    %r = arith.extf %v : f8E5M2 to f32
    %z = arith.bitcast %r : f32 to i32
    return %z : i32
  }
}

执行build/bin/mlir-opt repro.mlir --arith-expand='include-f8e5m2=true' --canonicalize, repro.mlir为上面的MLIR代码。输出经常量折叠后即为各编码的实际计算结果。

【实际结果】

【期望结果】

【其他相关附件信息】

【缺陷详情及分析指导参考链接】

likedislike
openeuler-ci-botopeneuler-ci-bot成员
8月30日 将 alexanderbill、li-yancheng、cf-zhao、eastb233、wangqiang95、kuenking111_admin、SegFault、wd-gitcode、chenzheng1030、gcw_LWQavIsb 设为负责人
openeuler-ci-botopeneuler-ci-bot成员
8月30日 添加了label:sig/Compiler
openeuler-ci-bot
openeuler-ci-bot成员
8月30日 评论:

Welcome To openEuler Community

Hey @gcw_LWQavIsb , thanks for your contribution to the community.

Bot Usage Manual

I'm the Bot here serving you. You can find the instructions on how to interact with me at Here . That means you can comment below every pull request or issue to trigger Bot Commands. You can self-configure the PR merge rules for this repository. For more details, please refer to Here.

Contact Guide

If you have any questions, please contact the SIG: Compiler ,
and any of the maintainers: @SegFault, @alexanderbill, @cf-zhao, @chenzheng1030, @eastb233, @kuenking111_admin, @li-yancheng, @wangqiang95, @wd-gitcode ,
and any of the committers: @liyunfei33, @zhongyunde .

likedislike