Skip to content
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

Merged
merged 3 commits into from
May 3, 2024

Conversation

cferry-AMD
Copy link
Contributor

@cferry-AMD cferry-AMD commented Apr 4, 2024

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.

Copy link

github-actions bot commented Apr 4, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

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
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

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.

@llvmbot llvmbot added the mlir label Apr 4, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 4, 2024

@llvm/pr-subscribers-mlir-emitc

@llvm/pr-subscribers-mlir

Author: Corentin Ferry (cferry-AMD)

Changes

Arith to EmitC currently does not handle floating-point to integer and vice-versa conversions (fptosi, fptoui, sitofp, uitofp).

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, float-to-int-truncates, to be set when the downstream rounding mode is guaranteed to be truncation. Otherwise, this conversion remains unsupported for the time being.


Full diff: https://github.com/llvm/llvm-project/pull/87614.diff

8 Files Affected:

  • (modified) mlir/include/mlir/Conversion/ArithToEmitC/ArithToEmitC.h (+2-1)
  • (modified) mlir/include/mlir/Conversion/Passes.td (+17)
  • (modified) mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp (+82-3)
  • (modified) mlir/lib/Conversion/ArithToEmitC/ArithToEmitCPass.cpp (+3-1)
  • (added) mlir/test/Conversion/ArithToEmitC/arith-to-emitc-cast-truncate.mlir (+20)
  • (added) mlir/test/Conversion/ArithToEmitC/arith-to-emitc-cast-unsupported.mlir (+48)
  • (added) mlir/test/Conversion/ArithToEmitC/arith-to-emitc-unsupported.mlir (+7)
  • (modified) mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir (+15)
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
+}

@cferry-AMD
Copy link
Contributor Author

The case of uitofp cast is triggering an issue (due to a missing cast). I'm getting this fixed and updating the PR. Stay tuned!

@cferry-AMD cferry-AMD force-pushed the corentin.upstream_arith_casts branch from 81e9f52 to db3765b Compare April 15, 2024 13:49
@simon-camp
Copy link
Contributor

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, float-to-int-truncates, to be set when the downstream rounding mode is guaranteed to be truncation. Otherwise, this conversion remains unsupported for the time being.

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:
Rounding Mode C / C++:

The current rounding mode does NOT affect the following:

floating-point to integer implicit conversion and casts (always towards zero)
...

@cferry-AMD
Copy link
Contributor Author

@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.

@marbre
Copy link
Member

marbre commented Apr 26, 2024

@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.

Would you like us to review again or do you intend to push further modifications? Fell free, to just ping us :)

@cferry-AMD
Copy link
Contributor Author

@marbre, @simon-camp, @mgehre-amd, yes, this PR can get a second round of reviews. Thanks!

@cferry-AMD cferry-AMD force-pushed the corentin.upstream_arith_casts branch from e7e14b2 to bc3f5ae Compare May 2, 2024 13:57
@cferry-AMD cferry-AMD requested a review from simon-camp May 2, 2024 13:58
Copy link
Contributor

@simon-camp simon-camp left a 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.

@marbre
Copy link
Member

marbre commented May 3, 2024

Can you edit the PR description please. Just state what this PR is adding. Something like the following would be descriptive enough I think.

This makes sense as the PR description will become the commit message of the squash commit.
I personally follow https://cbea.ms/git-commit/. Regarding additional information, It should be possible to add additional information that gets discarded after a --- delimiter or you just write a separate comment.

@mgehre-amd mgehre-amd merged commit 18e7dcb into llvm:main May 3, 2024
3 of 4 checks passed
@mgehre-amd mgehre-amd deleted the corentin.upstream_arith_casts branch May 3, 2024 11:47
Copy link

github-actions bot commented May 3, 2024

@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
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

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.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants