[Mlir-commits] [mlir] [MLIR][MathDialect] fix fp32 promotion crash when encounters scf.if (PR #104451)

Ivy Zhang llvmlistbot at llvm.org
Thu Aug 15 08:01:40 PDT 2024


crazydemo wrote:

take the below case as an example:
```
func.func @promote_in_if_block(%arg0: bf16, %arg1: bf16, %arg2: i1) -> bf16 {
  %0 = scf.if %arg2 -> bf16 {
    %1 = math.absf %arg0 : bf16
    scf.yield %1 : bf16
  } else {
    scf.yield %arg1 : bf16
  }
  return %0 : bf16
}
```

after applied `LegalizeToF32RewritePattern` will get:
```
"builtin.module"() ({
  "func.func"() <{function_type = (bf16, bf16, i1) -> bf16, sym_name = "promote_in_if_block"}> ({
  ^bb0(%arg0: bf16, %arg1: bf16, %arg2: i1):
    %0 = "scf.if"(%arg2) : (i1) -> f32
    %1 = "arith.truncf"(%0) <{fastmath = #arith.fastmath<contract>}> : (f32) -> bf16
    %2 = "scf.if"(%arg2) ({
      "scf.yield"(%arg0) : (bf16) -> ()
    }, {
      "scf.yield"(%arg1) : (bf16) -> ()
    }) : (i1) -> bf16
    "func.return"(%2) : (bf16) -> ()
  }) : () -> ()
}) : () -> ()
```
Note that here's a newly generated `%0 = "scf.if"(%arg2) : (i1) -> f32`, which is invalid for further scf::IfOp folder, as its `numRegions=1`. The folder function needs the `numRegions=2`, otherwise when `getElseRegion()` will exceed the index range.  

https://github.com/llvm/llvm-project/pull/104451


More information about the Mlir-commits mailing list