Skip to content

[MLIR][EmitC] arith-to-emitc: Fix lowering of fptoui #118504

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

Merged
merged 1 commit into from
Dec 5, 2024

Conversation

mgehre-amd
Copy link
Contributor

arith.fptoui %arg0 : f32 to i16 was lowered to

%0 = emitc.cast %arg0 : f32 to ui32
emitc.cast %0 : ui32 to i16

and is now lowered to

%0 = emitc.cast %arg0 : f32 to ui16
emitc.cast %0 : ui16 to i16

`arith.fptoui %arg0 : f32 to i16` was lowered to
```
%0 = emitc.cast %arg0 : f32 to ui32
emitc.cast %0 : ui32 to i16
```
and is now lowered to
```
%0 = emitc.cast %arg0 : f32 to ui16
emitc.cast %0 : ui16 to i16
```
@llvmbot
Copy link
Member

llvmbot commented Dec 3, 2024

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-emitc

Author: Matthias Gehre (mgehre-amd)

Changes

arith.fptoui %arg0 : f32 to i16 was lowered to

%0 = emitc.cast %arg0 : f32 to ui32
emitc.cast %0 : ui32 to i16

and is now lowered to

%0 = emitc.cast %arg0 : f32 to ui16
emitc.cast %0 : ui16 to i16

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

2 Files Affected:

  • (modified) mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp (+1-1)
  • (modified) mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir (+4)
diff --git a/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp b/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
index 50384d9a08e5d9..ccbc1669b7a92a 100644
--- a/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
+++ b/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
@@ -674,7 +674,7 @@ class FtoICastOpConversion : public OpConversionPattern<CastOp> {
     Type actualResultType = dstType;
     if (isa<arith::FPToUIOp>(castOp)) {
       actualResultType =
-          rewriter.getIntegerType(operandType.getIntOrFloatBitWidth(),
+          rewriter.getIntegerType(dstType.getIntOrFloatBitWidth(),
                                   /*isSigned=*/false);
     }
 
diff --git a/mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir b/mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
index afd1198ede0f76..1728c3a2557e07 100644
--- a/mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
+++ b/mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
@@ -587,6 +587,10 @@ func.func @arith_float_to_int_cast_ops(%arg0: f32, %arg1: f64) {
   // CHECK: emitc.cast %[[CAST0]] : ui32 to i32
   %4 = arith.fptoui %arg0 : f32 to i32
 
+  // CHECK: %[[CAST0:.*]] = emitc.cast %arg0 : f32 to ui16
+  // CHECK: emitc.cast %[[CAST0]] : ui16 to i16
+  %5 = arith.fptoui %arg0 : f32 to i16
+
   return
 }
 

@@ -674,7 +674,7 @@ class FtoICastOpConversion : public OpConversionPattern<CastOp> {
Type actualResultType = dstType;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be wrong and only real quick at this but do we need the if clause at all if actualResultType is set to dstType here already?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't dstType signless? If I'm not mistaking, this condition is there so that the first cast actually goes to unsigned no matter what the signedness of dstType is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, what @cferry-AMD said is also my understanding.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying.

@mgehre-amd mgehre-amd requested a review from marbre December 4, 2024 15:41
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.

LGTM

Copy link
Member

@marbre marbre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix :)

@mgehre-amd mgehre-amd merged commit 1f93282 into llvm:main Dec 5, 2024
11 checks passed
@mgehre-amd mgehre-amd deleted the matthias.emitc_fix_fptoui branch December 5, 2024 13:50
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.

5 participants