已开启
[MLIR][LLVM] --llvm-legalize-float8-types 生成非法 IR:extractelement 未列入改写白名单、containsFloat8Type 不穿透 !llvm.array/!llvm.struct 聚合类型(合法输入被拒) #129
cactusBalll创建于  6 天前
cactusBalll
6 天前 创建

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

  • 缺陷组件:MLIR LLVMIR dialect(LegalizeFloat8Types.cpp)

  • 缺陷类型:生成非法 IR / 合法输入被拒(pass 硬失败,exit 1)

  • 根因:类型替换按 op 白名单逐个应用,op 集合与类型集合均不完整,导致同一值的生产者与消费者类型不一致(i8 vs f8):

    1. LLVM::ExtractElementOp 未列入 isSafeOp 白名单(与 insertelement/shufflevector 同属"向量打包 op"类别);LLVM::StoreOp 也未列入
    2. containsFloat8Type 不递归进入 !llvm.array/!llvm.struct 聚合类型
  • 缺陷引入提交:

    • 6e8803fe54a830d8ba434363c01520653963e7e6 "Add an LLVMIR transform pass to convert f8 types to int8 types in safe memory ops"(pass 本体)
    • f27b622df264b79d4a5db1032014a48aebc3f8a2 "[MLIR][Transforms] Add MaskedLoad support to LegalizeFloat8Types"(向同一不完整白名单追加 MaskedLoadOp,未修补整体缺陷)
    • 合并提交 3356bb977a37b12dcad52096f26e2fd147f00a18 "!407 [MLIR] Add Support for Triton3.2 + Triton-Shared"

一、缺陷信息

mlir-opt --llvm-legalize-float8-types 在两类完全合法的 LLVM dialect 输入上生成无法通过 verifier 的 IR,pass 以 exit 1 失败:

  1. extractelement 链:llvm.insertelement 在"安全 op"白名单内,其结果 vector<4xf8E4M3FN> 被改写为 vector<4xi8>(LegalizeFloat8Types.cpp:159-168);但消费方 llvm.extractelement 不在白名单(仅 LoadOp/MaskedLoadOp/GEPOp/UndefOp/InsertElementOp/ShuffleVectorOp/BitcastOp,LegalizeFloat8Types.cpp:118-121),结果仍为 f8E4M3FN——操作数已是 i8 向量而结果是 f8 标量,'llvm.extractelement' op failed to verify that result type matches vector element type
  2. 聚合类型:containsFloat8Type(LegalizeFloat8Types.cpp:48-61)只检查 VectorType/LLVMFixedVectorType/LLVMScalableVectorType,不穿透 !llvm.array/!llvm.struct;load 结果被合法化 f8→i8,但 !llvm.array<4 x f8E4M3FN> 从未被改写,llvm.insertvalue %i8val, %arrType mismatch: cannot insert 'i8' into '!llvm.array<4 x f8E4M3FN>'

【缺陷所属的os版本】

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

【内核版本】

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

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: --llvm-legalize-float8-types emits IR that fails the LLVM-dialect
// verifier (mlir-opt exits 1) on legal input.
module {
  // Case A: insertelement result is legalized (vector<4xf8> -> vector<4xi8>)
  // but the consuming extractelement keeps its f8 result type.
  llvm.func @case_a_extractelement(%ptr: !llvm.ptr, %idx: i32, %dst: !llvm.ptr) {
    %vec = llvm.mlir.undef : vector<4 x f8E4M3FN>
    %val = llvm.load %ptr : !llvm.ptr -> f8E4M3FN
    %v1 = llvm.insertelement %val, %vec[%idx : i32] : vector<4 x f8E4M3FN>
    %e = llvm.extractelement %v1[%idx : i32] : vector<4 x f8E4M3FN>
    llvm.store %e, %dst : f8E4M3FN, !llvm.ptr
    llvm.return
  }
  // Case B: load result is legalized to i8, but !llvm.array element types are
  // never legalized (containsFloat8Type ignores LLVM array/struct types), so
  // insertvalue(i8 into array<4 x f8>) fails verification.
  llvm.func @case_b_insertvalue(%ptr: !llvm.ptr, %dst: !llvm.ptr) {
    %v = llvm.load %ptr : !llvm.ptr -> f8E4M3FN
    %arr = llvm.mlir.undef : !llvm.array<4 x f8E4M3FN>
    %r = llvm.insertvalue %v, %arr[0] : !llvm.array<4 x f8E4M3FN>
    llvm.store %r, %dst : !llvm.array<4 x f8E4M3FN>, !llvm.ptr
    llvm.return
  }
}

执行build/bin/mlir-opt repro.mlir --llvm-legalize-float8-types
输出

./repro.mlir:10:10: warning: LegalizeFloat8Types: unexpected Float8 type on op 'llvm.extractelement'; f8 arithmetic should have been emulated to f32 earlier in the pipeline
    %e = llvm.extractelement %v1[%idx : i32] : vector<4 x f8E4M3FN>
         ^
./repro.mlir:10:10: note: see current operation: %3 = "llvm.extractelement"(%2, %arg1) : (vector<4xi8>, i32) -> f8E4M3FN
./repro.mlir:11:5: warning: LegalizeFloat8Types: unexpected Float8 type on op 'llvm.store'; f8 arithmetic should have been emulated to f32 earlier in the pipeline
    llvm.store %e, %dst : f8E4M3FN, !llvm.ptr
    ^
./repro.mlir:11:5: note: see current operation: "llvm.store"(%3, %arg2) <{ordering = 0 : i64}> : (f8E4M3FN, !llvm.ptr) -> ()
./repro.mlir:10:10: error: 'llvm.extractelement' op failed to verify that result type matches vector element type
    %e = llvm.extractelement %v1[%idx : i32] : vector<4 x f8E4M3FN>
         ^
./repro.mlir:10:10: note: see current operation: %3 = "llvm.extractelement"(%2, %arg1) : (vector<4xi8>, i32) -> f8E4M3FN
./repro.mlir:20:10: error: 'llvm.insertvalue' op Type mismatch: cannot insert 'i8' into '!llvm.array<4 x f8E4M3FN>'
    %r = llvm.insertvalue %v, %arr[0] : !llvm.array<4 x f8E4M3FN>
         ^
./repro.mlir:20:10: note: see current operation: %2 = "llvm.insertvalue"(%1, %0) <{position = array<i64: 0>}> : (!llvm.array<4 x f8E4M3FN>, i8) -> !llvm.array<4 x f8E4M3FN>

【实际结果】

【期望结果】

【其他相关附件信息】

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

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

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