Skip to content

Commit 95ff2ad

Browse files
Ivy Zhangcjdb
authored andcommitted
[MLIR][MathDialect] fix fp32 promotion crash when encounters scf.if (llvm#104451)
1. Expand legal op list in `legalizeToF32` 2. add legalization support for `math::rsqrtOp` in `mathToLibm`.
1 parent af72aa6 commit 95ff2ad

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

mlir/lib/Conversion/MathToLibm/MathToLibm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ void mlir::populateMathToLibmConversionPatterns(RewritePatternSet &patterns) {
191191
populatePatternsForOp<math::SinOp>(patterns, ctx, "sinf", "sin");
192192
populatePatternsForOp<math::SinhOp>(patterns, ctx, "sinhf", "sinh");
193193
populatePatternsForOp<math::SqrtOp>(patterns, ctx, "sqrtf", "sqrt");
194+
populatePatternsForOp<math::RsqrtOp>(patterns, ctx, "rsqrtf", "rsqrt");
194195
populatePatternsForOp<math::TanOp>(patterns, ctx, "tanf", "tan");
195196
populatePatternsForOp<math::TanhOp>(patterns, ctx, "tanhf", "tanh");
196197
populatePatternsForOp<math::TruncOp>(patterns, ctx, "truncf", "trunc");

mlir/lib/Dialect/Math/Transforms/LegalizeToF32.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@ void mlir::math::populateLegalizeToF32TypeConverter(
6565

6666
void mlir::math::populateLegalizeToF32ConversionTarget(
6767
ConversionTarget &target, TypeConverter &typeConverter) {
68-
target.addDynamicallyLegalDialect<MathDialect>(
69-
[&typeConverter](Operation *op) -> bool {
70-
return typeConverter.isLegal(op);
71-
});
68+
target.markUnknownOpDynamicallyLegal([&typeConverter](Operation *op) -> bool {
69+
if (isa<MathDialect>(op->getDialect()))
70+
return typeConverter.isLegal(op);
71+
return true;
72+
});
7273
target.addLegalOp<FmaOp>();
7374
target.addLegalOp<arith::ExtFOp, arith::TruncFOp>();
7475
}

mlir/test/Conversion/MathToLibm/convert-to-libm.mlir

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
// RUN: mlir-opt %s -convert-math-to-libm -canonicalize | FileCheck %s
23

34
// CHECK-DAG: @acos(f64) -> f64 attributes {llvm.readnone}
@@ -58,6 +59,8 @@
5859
// CHECK-DAG: @ceilf(f32) -> f32 attributes {llvm.readnone}
5960
// CHECK-DAG: @sqrt(f64) -> f64 attributes {llvm.readnone}
6061
// CHECK-DAG: @sqrtf(f32) -> f32 attributes {llvm.readnone}
62+
// CHECK-DAG: @rsqrt(f64) -> f64 attributes {llvm.readnone}
63+
// CHECK-DAG: @rsqrtf(f32) -> f32 attributes {llvm.readnone}
6164
// CHECK-DAG: @pow(f64, f64) -> f64 attributes {llvm.readnone}
6265
// CHECK-DAG: @powf(f32, f32) -> f32 attributes {llvm.readnone}
6366

@@ -999,6 +1002,43 @@ func.func @sqrt_vec_caller(%float: vector<2xf32>, %double: vector<2xf64>) -> (ve
9991002
// CHECK: return %[[VAL_11]], %[[VAL_17]] : vector<2xf32>, vector<2xf64>
10001003
// CHECK: }
10011004

1005+
// CHECK-LABEL: func @rsqrt_caller
1006+
// CHECK-SAME: %[[FLOAT:.*]]: f32
1007+
// CHECK-SAME: %[[DOUBLE:.*]]: f64
1008+
func.func @rsqrt_caller(%float: f32, %double: f64) -> (f32, f64) {
1009+
// CHECK-DAG: %[[FLOAT_RESULT:.*]] = call @rsqrtf(%[[FLOAT]]) : (f32) -> f32
1010+
%float_result = math.rsqrt %float : f32
1011+
// CHECK-DAG: %[[DOUBLE_RESULT:.*]] = call @rsqrt(%[[DOUBLE]]) : (f64) -> f64
1012+
%double_result = math.rsqrt %double : f64
1013+
// CHECK: return %[[FLOAT_RESULT]], %[[DOUBLE_RESULT]]
1014+
return %float_result, %double_result : f32, f64
1015+
}
1016+
1017+
func.func @rsqrt_vec_caller(%float: vector<2xf32>, %double: vector<2xf64>) -> (vector<2xf32>, vector<2xf64>) {
1018+
%float_result = math.rsqrt %float : vector<2xf32>
1019+
%double_result = math.rsqrt %double : vector<2xf64>
1020+
return %float_result, %double_result : vector<2xf32>, vector<2xf64>
1021+
}
1022+
// CHECK-LABEL: func @rsqrt_vec_caller(
1023+
// CHECK-SAME: %[[VAL_0:.*]]: vector<2xf32>,
1024+
// CHECK-SAME: %[[VAL_1:.*]]: vector<2xf64>) -> (vector<2xf32>, vector<2xf64>) {
1025+
// CHECK-DAG: %[[CVF:.*]] = arith.constant dense<0.000000e+00> : vector<2xf32>
1026+
// CHECK-DAG: %[[CVD:.*]] = arith.constant dense<0.000000e+00> : vector<2xf64>
1027+
// CHECK: %[[IN0_F32:.*]] = vector.extract %[[VAL_0]][0] : f32 from vector<2xf32>
1028+
// CHECK: %[[OUT0_F32:.*]] = call @rsqrtf(%[[IN0_F32]]) : (f32) -> f32
1029+
// CHECK: %[[VAL_8:.*]] = vector.insert %[[OUT0_F32]], %[[CVF]] [0] : f32 into vector<2xf32>
1030+
// CHECK: %[[IN1_F32:.*]] = vector.extract %[[VAL_0]][1] : f32 from vector<2xf32>
1031+
// CHECK: %[[OUT1_F32:.*]] = call @rsqrtf(%[[IN1_F32]]) : (f32) -> f32
1032+
// CHECK: %[[VAL_11:.*]] = vector.insert %[[OUT1_F32]], %[[VAL_8]] [1] : f32 into vector<2xf32>
1033+
// CHECK: %[[IN0_F64:.*]] = vector.extract %[[VAL_1]][0] : f64 from vector<2xf64>
1034+
// CHECK: %[[OUT0_F64:.*]] = call @rsqrt(%[[IN0_F64]]) : (f64) -> f64
1035+
// CHECK: %[[VAL_14:.*]] = vector.insert %[[OUT0_F64]], %[[CVD]] [0] : f64 into vector<2xf64>
1036+
// CHECK: %[[IN1_F64:.*]] = vector.extract %[[VAL_1]][1] : f64 from vector<2xf64>
1037+
// CHECK: %[[OUT1_F64:.*]] = call @rsqrt(%[[IN1_F64]]) : (f64) -> f64
1038+
// CHECK: %[[VAL_17:.*]] = vector.insert %[[OUT1_F64]], %[[VAL_14]] [1] : f64 into vector<2xf64>
1039+
// CHECK: return %[[VAL_11]], %[[VAL_17]] : vector<2xf32>, vector<2xf64>
1040+
// CHECK: }
1041+
10021042
// CHECK-LABEL: func @powf_caller(
10031043
// CHECK-SAME: %[[FLOATA:.*]]: f32, %[[FLOATB:.*]]: f32
10041044
// CHECK-SAME: %[[DOUBLEA:.*]]: f64, %[[DOUBLEB:.*]]: f64

mlir/test/Dialect/Math/legalize-to-f32.mlir

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,17 @@ func.func @sequences(%arg0: f16) -> f16 {
8383
%1 = math.sin %0 : f16
8484
return %1 : f16
8585
}
86+
87+
// CHECK-LABEL: @promote_in_if_block
88+
func.func @promote_in_if_block(%arg0: bf16, %arg1: bf16, %arg2: i1) -> bf16 {
89+
// CHECK: [[EXTF0:%.+]] = arith.extf
90+
// CHECK-NEXT: %[[RES:.*]] = scf.if
91+
%0 = scf.if %arg2 -> bf16 {
92+
%1 = math.absf %arg0 : bf16
93+
// CHECK: [[TRUNCF0:%.+]] = arith.truncf
94+
scf.yield %1 : bf16
95+
} else {
96+
scf.yield %arg1 : bf16
97+
}
98+
return %0 : bf16
99+
}

0 commit comments

Comments
 (0)