-
Notifications
You must be signed in to change notification settings - Fork 11.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[mlir][emitc] Arith to EmitC: handle floating-point<->integer conversions #87614
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-mlir-emitc @llvm/pr-subscribers-mlir Author: Corentin Ferry (cferry-AMD) ChangesArith to EmitC currently does not handle floating-point to integer and vice-versa conversions ( The Arith dialect only specifies a rounding mode for floating-point to integer conversions, which must be rounding to zero (aka truncation). For integer to floating-point, no rounding mode is specified. Integer to floating-point conversions can be achieved with C-style casts. However, for floating-point to integer conversions, we must ensure that the rounding mode downstream is actually rounding to zero to preserve the Arith semantics. We add an option, Full diff: https://github.com/llvm/llvm-project/pull/87614.diff 8 Files Affected:
diff --git a/mlir/include/mlir/Conversion/ArithToEmitC/ArithToEmitC.h b/mlir/include/mlir/Conversion/ArithToEmitC/ArithToEmitC.h
index 9cb43689d1ce64..32d039e9c89185 100644
--- a/mlir/include/mlir/Conversion/ArithToEmitC/ArithToEmitC.h
+++ b/mlir/include/mlir/Conversion/ArithToEmitC/ArithToEmitC.h
@@ -14,7 +14,8 @@ class RewritePatternSet;
class TypeConverter;
void populateArithToEmitCPatterns(TypeConverter &typeConverter,
- RewritePatternSet &patterns);
+ RewritePatternSet &patterns,
+ bool optionFloatToIntTruncates);
} // namespace mlir
#endif // MLIR_CONVERSION_ARITHTOEMITC_ARITHTOEMITC_H
diff --git a/mlir/include/mlir/Conversion/Passes.td b/mlir/include/mlir/Conversion/Passes.td
index d094ee3b36ab95..029cbd7aec2819 100644
--- a/mlir/include/mlir/Conversion/Passes.td
+++ b/mlir/include/mlir/Conversion/Passes.td
@@ -139,7 +139,24 @@ def ArithToAMDGPUConversionPass : Pass<"convert-arith-to-amdgpu"> {
def ConvertArithToEmitC : Pass<"convert-arith-to-emitc"> {
let summary = "Convert Arith dialect to EmitC dialect";
+ let description = [{
+ This pass converts `arith` dialect operations to `emitc`.
+
+ The semantics of floating-point to integer conversions `arith.fptosi`,
+ `arith.fptoui` require rounding towards zero. Typical C++ implementations
+ use this behavior for float-to-integer casts, but that is not mandated by
+ C++ and there are implementation-defined means to change the default behavior.
+
+ If casts can be guaranteed to use round-to-zero, use the
+ `float-to-int-truncates` flag to allow conversion of `arith.fptosi` and
+ `arith.fptoui` operations.
+ }];
let dependentDialects = ["emitc::EmitCDialect"];
+ let options = [
+ Option<"floatToIntTruncates", "float-to-int-truncates", "bool",
+ /*default=*/"false",
+ "Whether the behavior of float-to-int cast in emitc is truncation">,
+ ];
}
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp b/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
index db493c1294ba2d..311978ea6c40e0 100644
--- a/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
+++ b/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
@@ -128,6 +128,78 @@ class SelectOpConversion : public OpConversionPattern<arith::SelectOp> {
}
};
+// Floating-point to integer conversions.
+template <typename CastOp>
+class FtoICastOpConversion : public OpConversionPattern<CastOp> {
+private:
+ bool floatToIntTruncates;
+
+public:
+ FtoICastOpConversion(const TypeConverter &typeConverter, MLIRContext *context,
+ bool optionFloatToIntTruncates)
+ : OpConversionPattern<CastOp>(typeConverter, context),
+ floatToIntTruncates(optionFloatToIntTruncates) {}
+
+ LogicalResult
+ matchAndRewrite(CastOp castOp, typename CastOp::Adaptor adaptor,
+ ConversionPatternRewriter &rewriter) const override {
+
+ Type operandType = adaptor.getIn().getType();
+ if (!emitc::isSupportedFloatType(operandType))
+ return rewriter.notifyMatchFailure(castOp,
+ "unsupported cast source type");
+
+ if (!floatToIntTruncates)
+ return rewriter.notifyMatchFailure(
+ castOp, "conversion currently requires EmitC casts to use truncation "
+ "as rounding mode");
+
+ Type dstType = this->getTypeConverter()->convertType(castOp.getType());
+ if (!dstType)
+ return rewriter.notifyMatchFailure(castOp, "type conversion failed");
+
+ if (!emitc::isSupportedIntegerType(dstType))
+ return rewriter.notifyMatchFailure(castOp,
+ "unsupported cast destination type");
+
+ rewriter.replaceOpWithNewOp<emitc::CastOp>(castOp, dstType,
+ adaptor.getOperands());
+
+ return success();
+ }
+};
+
+// Integer to floating-point conversions.
+template <typename CastOp>
+class ItoFCastOpConversion : public OpConversionPattern<CastOp> {
+public:
+ ItoFCastOpConversion(const TypeConverter &typeConverter, MLIRContext *context)
+ : OpConversionPattern<CastOp>(typeConverter, context) {}
+
+ LogicalResult
+ matchAndRewrite(CastOp castOp, typename CastOp::Adaptor adaptor,
+ ConversionPatternRewriter &rewriter) const override {
+
+ Type operandType = adaptor.getIn().getType();
+ if (!emitc::isSupportedIntegerType(operandType))
+ return rewriter.notifyMatchFailure(castOp,
+ "unsupported cast source type");
+
+ Type dstType = this->getTypeConverter()->convertType(castOp.getType());
+ if (!dstType)
+ return rewriter.notifyMatchFailure(castOp, "type conversion failed");
+
+ if (!emitc::isSupportedFloatType(dstType))
+ return rewriter.notifyMatchFailure(castOp,
+ "unsupported cast destination type");
+
+ rewriter.replaceOpWithNewOp<emitc::CastOp>(castOp, dstType,
+ adaptor.getOperands());
+
+ return success();
+ }
+};
+
} // namespace
//===----------------------------------------------------------------------===//
@@ -135,7 +207,8 @@ class SelectOpConversion : public OpConversionPattern<arith::SelectOp> {
//===----------------------------------------------------------------------===//
void mlir::populateArithToEmitCPatterns(TypeConverter &typeConverter,
- RewritePatternSet &patterns) {
+ RewritePatternSet &patterns,
+ bool optionFloatToIntTruncates) {
MLIRContext *ctx = patterns.getContext();
// clang-format off
@@ -148,7 +221,13 @@ void mlir::populateArithToEmitCPatterns(TypeConverter &typeConverter,
IntegerOpConversion<arith::AddIOp, emitc::AddOp>,
IntegerOpConversion<arith::MulIOp, emitc::MulOp>,
IntegerOpConversion<arith::SubIOp, emitc::SubOp>,
- SelectOpConversion
- >(typeConverter, ctx);
+ SelectOpConversion,
+ ItoFCastOpConversion<arith::SIToFPOp>,
+ ItoFCastOpConversion<arith::UIToFPOp>
+ >(typeConverter, ctx)
+ .add<
+ FtoICastOpConversion<arith::FPToSIOp>,
+ FtoICastOpConversion<arith::FPToUIOp>
+ >(typeConverter, ctx, optionFloatToIntTruncates);
// clang-format on
}
diff --git a/mlir/lib/Conversion/ArithToEmitC/ArithToEmitCPass.cpp b/mlir/lib/Conversion/ArithToEmitC/ArithToEmitCPass.cpp
index 45a088ed144f17..546bbfe2082eff 100644
--- a/mlir/lib/Conversion/ArithToEmitC/ArithToEmitCPass.cpp
+++ b/mlir/lib/Conversion/ArithToEmitC/ArithToEmitCPass.cpp
@@ -29,6 +29,8 @@ using namespace mlir;
namespace {
struct ConvertArithToEmitC
: public impl::ConvertArithToEmitCBase<ConvertArithToEmitC> {
+ using Base::Base;
+
void runOnOperation() override;
};
} // namespace
@@ -44,7 +46,7 @@ void ConvertArithToEmitC::runOnOperation() {
TypeConverter typeConverter;
typeConverter.addConversion([](Type type) { return type; });
- populateArithToEmitCPatterns(typeConverter, patterns);
+ populateArithToEmitCPatterns(typeConverter, patterns, floatToIntTruncates);
if (failed(
applyPartialConversion(getOperation(), target, std::move(patterns))))
diff --git a/mlir/test/Conversion/ArithToEmitC/arith-to-emitc-cast-truncate.mlir b/mlir/test/Conversion/ArithToEmitC/arith-to-emitc-cast-truncate.mlir
new file mode 100644
index 00000000000000..f45b6306b0292b
--- /dev/null
+++ b/mlir/test/Conversion/ArithToEmitC/arith-to-emitc-cast-truncate.mlir
@@ -0,0 +1,20 @@
+// RUN: mlir-opt -split-input-file --pass-pipeline="builtin.module(convert-arith-to-emitc{float-to-int-truncates})" %s | FileCheck %s
+
+func.func @arith_float_to_int_cast_ops(%arg0: f32, %arg1: f64) {
+ // CHECK: emitc.cast %arg0 : f32 to i32
+ %0 = arith.fptosi %arg0 : f32 to i32
+
+ // CHECK: emitc.cast %arg1 : f64 to i32
+ %1 = arith.fptosi %arg1 : f64 to i32
+
+ // CHECK: emitc.cast %arg0 : f32 to i16
+ %2 = arith.fptosi %arg0 : f32 to i16
+
+ // CHECK: emitc.cast %arg1 : f64 to i16
+ %3 = arith.fptosi %arg1 : f64 to i16
+
+ // CHECK: emitc.cast %arg0 : f32 to i32
+ %4 = arith.fptoui %arg0 : f32 to i32
+
+ return
+}
diff --git a/mlir/test/Conversion/ArithToEmitC/arith-to-emitc-cast-unsupported.mlir b/mlir/test/Conversion/ArithToEmitC/arith-to-emitc-cast-unsupported.mlir
new file mode 100644
index 00000000000000..34fc9f3dffc0c8
--- /dev/null
+++ b/mlir/test/Conversion/ArithToEmitC/arith-to-emitc-cast-unsupported.mlir
@@ -0,0 +1,48 @@
+// RUN: mlir-opt -split-input-file --pass-pipeline="builtin.module(convert-arith-to-emitc{float-to-int-truncates})" -verify-diagnostics %s
+
+func.func @arith_cast_tensor(%arg0: tensor<5xf32>) -> tensor<5xi32> {
+ // expected-error @+1 {{failed to legalize operation 'arith.fptosi'}}
+ %t = arith.fptosi %arg0 : tensor<5xf32> to tensor<5xi32>
+ return %t: tensor<5xi32>
+}
+
+// -----
+
+func.func @arith_cast_vector(%arg0: vector<5xf32>) -> vector<5xi32> {
+ // expected-error @+1 {{failed to legalize operation 'arith.fptosi'}}
+ %t = arith.fptosi %arg0 : vector<5xf32> to vector<5xi32>
+ return %t: vector<5xi32>
+}
+
+// -----
+
+func.func @arith_cast_bf16(%arg0: bf16) -> i32 {
+ // expected-error @+1 {{failed to legalize operation 'arith.fptosi'}}
+ %t = arith.fptosi %arg0 : bf16 to i32
+ return %t: i32
+}
+
+// -----
+
+func.func @arith_cast_f16(%arg0: f16) -> i32 {
+ // expected-error @+1 {{failed to legalize operation 'arith.fptosi'}}
+ %t = arith.fptosi %arg0 : f16 to i32
+ return %t: i32
+}
+
+
+// -----
+
+func.func @arith_cast_to_bf16(%arg0: i32) -> bf16 {
+ // expected-error @+1 {{failed to legalize operation 'arith.sitofp'}}
+ %t = arith.sitofp %arg0 : i32 to bf16
+ return %t: bf16
+}
+
+// -----
+
+func.func @arith_cast_to_f16(%arg0: i32) -> f16 {
+ // expected-error @+1 {{failed to legalize operation 'arith.sitofp'}}
+ %t = arith.sitofp %arg0 : i32 to f16
+ return %t: f16
+}
diff --git a/mlir/test/Conversion/ArithToEmitC/arith-to-emitc-unsupported.mlir b/mlir/test/Conversion/ArithToEmitC/arith-to-emitc-unsupported.mlir
new file mode 100644
index 00000000000000..bbec664100564b
--- /dev/null
+++ b/mlir/test/Conversion/ArithToEmitC/arith-to-emitc-unsupported.mlir
@@ -0,0 +1,7 @@
+// RUN: mlir-opt -split-input-file -convert-arith-to-emitc -verify-diagnostics %s
+
+func.func @arith_cast_f32(%arg0: f32) -> i32 {
+ // expected-error @+1 {{failed to legalize operation 'arith.fptosi'}}
+ %t = arith.fptosi %arg0 : f32 to i32
+ return %t: i32
+}
diff --git a/mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir b/mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
index 76ba518577ab8e..406aa254ecfee1 100644
--- a/mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
+++ b/mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
@@ -93,3 +93,18 @@ func.func @arith_select(%arg0: i1, %arg1: tensor<8xi32>, %arg2: tensor<8xi32>) -
%0 = arith.select %arg0, %arg1, %arg2 : i1, tensor<8xi32>
return
}
+
+// -----
+
+func.func @arith_int_to_float_cast_ops(%arg0: i8, %arg1: i64) {
+ // CHECK: emitc.cast %arg0 : i8 to f32
+ %0 = arith.sitofp %arg0 : i8 to f32
+
+ // CHECK: emitc.cast %arg1 : i64 to f32
+ %1 = arith.sitofp %arg1 : i64 to f32
+
+ // CHECK: emitc.cast %arg0 : i8 to f32
+ %2 = arith.uitofp %arg0 : i8 to f32
+
+ return
+}
|
The case of |
81e9f52
to
db3765b
Compare
Do you have links to documentation on this? As far as I understand implicit conversions and casts ignore the current rounding mode for floating point to integer casts:
|
@simon-camp Thanks for your review and documentation, you're right. There is no need to check the rounding mode for cast operations, so we can just drop the extra flag. |
bf83b3f
to
e7e14b2
Compare
Would you like us to review again or do you intend to push further modifications? Fell free, to just ping us :) |
@marbre, @simon-camp, @mgehre-amd, yes, this PR can get a second round of reviews. Thanks! |
e7e14b2
to
bc3f5ae
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you edit the PR description please. Just state what this PR is adding. Something like the following would be descriptive enough I think.
Add support for cast operations in the conversion. Floating point conversions to 1-bit integer types are not handled at the moment, as theses don't map to boolean conversions directly.
This makes sense as the PR description will become the commit message of the squash commit. |
@cferry-AMD Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Add support for floating-point to integer, integer to floating-point conversions. Floating point conversions to 1-bit integer types are not handled at the moment, as these don't map directly to boolean conversions.