Use `uitofp` when converting a boolean to floating-point.

It was lowered to `sitofp` which resulted in `-1.0`.

PiperOrigin-RevId: 352958489
This commit is contained in:
Hanhan Wang 2021-01-21 00:14:31 -08:00 committed by TensorFlow MLIR Team
parent cf08128862
commit 46112c95c6
3 changed files with 31 additions and 1 deletions

View File

@ -288,7 +288,12 @@ inline Value MapLhloOpToStdScalarOp<lmhlo::ConvertOp>(
Type sourceType = getElementTypeOrSelf(args.front().getType()); Type sourceType = getElementTypeOrSelf(args.front().getType());
Type targetType = getElementTypeOrSelf(result_types.front()); Type targetType = getElementTypeOrSelf(result_types.front());
if (mlir::SIToFPOp::areCastCompatible(sourceType, targetType)) { // A boolean value is considered to be unsigned when converting to
// floating-point. Otherwise, it will become `-1`.
if (sourceType.isInteger(/*width=*/1) &&
mlir::UIToFPOp::areCastCompatible(sourceType, targetType)) {
return b->create<mlir::UIToFPOp>(loc, result_types, args, mlir::None);
} else if (mlir::SIToFPOp::areCastCompatible(sourceType, targetType)) {
return b->create<mlir::SIToFPOp>(loc, result_types, args, mlir::None); return b->create<mlir::SIToFPOp>(loc, result_types, args, mlir::None);
} else if (sourceType.isa<FloatType>() && targetType.isa<FloatType>()) { } else if (sourceType.isa<FloatType>() && targetType.isa<FloatType>()) {
FloatType src = sourceType.cast<FloatType>(); FloatType src = sourceType.cast<FloatType>();

View File

@ -608,6 +608,19 @@ func @reshape_multiple_collapse
// ----- // -----
// CHECK-LABEL: func @convert_i1_to_f32
func @convert_i1_to_f32(%input: tensor<2x2xi1>) -> tensor<2x2xf32> {
%result = "mhlo.convert"(%input) : (tensor<2x2xi1>) -> tensor<2x2xf32>
return %result : tensor<2x2xf32>
}
// CHECK: linalg.init_tensor
// CHECK: linalg.generic
// CHECK-NEXT: ^bb0(%[[OPERAND_IN:.*]]: i1, %{{.*}}: f32):
// CHECK-NEXT: %[[RESULT:.*]] = uitofp %[[OPERAND_IN]] : i1 to f32
// CHECK-NEXT: linalg.yield %[[RESULT]] : f32
// -----
// CHECK-LABEL: func @convert_i32_to_f32 // CHECK-LABEL: func @convert_i32_to_f32
func @convert_i32_to_f32(%input: tensor<2x2xi32>) -> tensor<2x2xf32> { func @convert_i32_to_f32(%input: tensor<2x2xi32>) -> tensor<2x2xf32> {
%result = "mhlo.convert"(%input) : (tensor<2x2xi32>) -> tensor<2x2xf32> %result = "mhlo.convert"(%input) : (tensor<2x2xi32>) -> tensor<2x2xf32>

View File

@ -404,6 +404,18 @@ func @ceil(%input: memref<2x2xf32>, %result: memref<2x2xf32>) {
// ----- // -----
// CHECK-LABEL: func @convert_i1_to_f32
func @convert_i1_to_f32(%input: memref<2x2xi1>, %result: memref<2x2xf32>) {
"lmhlo.convert"(%input, %result) : (memref<2x2xi1>, memref<2x2xf32>) -> ()
return
}
// CHECK: linalg.generic
// CHECK-NEXT: ^bb0(%[[OPERAND_IN:.*]]: i1, %[[RESULT_OUT:.*]]: f32):
// CHECK-NEXT: %[[RESULT:.*]] = uitofp %[[OPERAND_IN]] : i1 to f32
// CHECK-NEXT: linalg.yield %[[RESULT]] : f32
// -----
// CHECK-LABEL: func @convert_i32_to_f32 // CHECK-LABEL: func @convert_i32_to_f32
func @convert_i32_to_f32(%input: memref<2x2xi32>, %result: memref<2x2xf32>) { func @convert_i32_to_f32(%input: memref<2x2xi32>, %result: memref<2x2xf32>) {
"lmhlo.convert"(%input, %result) : (memref<2x2xi32>, memref<2x2xf32>) -> () "lmhlo.convert"(%input, %result) : (memref<2x2xi32>, memref<2x2xf32>) -> ()