[Mlir-commits] [mlir] [mlir][IndexToLLVM] Lower UnrealizedConversionCastOp related to IndexType in IndexToLLVM (PR #75255)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Dec 12 15:55:54 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Xiang Li (python3kgae)

<details>
<summary>Changes</summary>


Lower pattern like
```
    %5 = builtin.unrealized_conversion_cast %a : i32 to index
    %6 = builtin.unrealized_conversion_cast %5 : index to i64
```
into
```
    llvm.zext %a : i32 to i64
```
This is for https://github.com/llvm/llvm-project/issues/74939

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


2 Files Affected:

- (modified) mlir/lib/Conversion/IndexToLLVM/IndexToLLVM.cpp (+41-1) 
- (modified) mlir/test/Conversion/IndexToLLVM/index-to-llvm.mlir (+36) 


``````````diff
diff --git a/mlir/lib/Conversion/IndexToLLVM/IndexToLLVM.cpp b/mlir/lib/Conversion/IndexToLLVM/IndexToLLVM.cpp
index 9d8a5d8a0e1c0..db073f542e131 100644
--- a/mlir/lib/Conversion/IndexToLLVM/IndexToLLVM.cpp
+++ b/mlir/lib/Conversion/IndexToLLVM/IndexToLLVM.cpp
@@ -285,6 +285,45 @@ using ConvertIndexXor = mlir::OneToOneConvertToLLVMPattern<XOrOp, LLVM::XOrOp>;
 using ConvertIndexBoolConstant =
     mlir::OneToOneConvertToLLVMPattern<BoolConstantOp, LLVM::ConstantOp>;
 
+/// Define lowering patterns for UnrealizedConversionCastOp ops
+struct UnrealizedConversionCastOpLowering
+    : public ConvertOpToLLVMPattern<UnrealizedConversionCastOp> {
+  UnrealizedConversionCastOpLowering(const LLVMTypeConverter &converter)
+      : ConvertOpToLLVMPattern<UnrealizedConversionCastOp>(converter) {}
+
+  LogicalResult
+  matchAndRewrite(UnrealizedConversionCastOp castOp,
+                  typename UnrealizedConversionCastOp::Adaptor adaptor,
+                  ConversionPatternRewriter &rewriter) const override {
+    Type srcType = castOp.getOperand(0).getType();
+    Type dstType = castOp.getType(0);
+    // Skip cast not related to IndexType.
+    if (!isa<IndexType>(srcType) && !isa<IndexType>(dstType))
+      return failure();
+
+    srcType = getTypeConverter()->convertType(srcType);
+    IntegerType srcIntType = dyn_cast<IntegerType>(srcType);
+    if (!srcIntType)
+      return failure();
+    dstType = getTypeConverter()->convertType(dstType);
+    IntegerType dstIntType = dyn_cast<IntegerType>(dstType);
+    if (!dstIntType)
+      return failure();
+
+    Value src = adaptor.getInputs()[0];
+    uint32_t srcWidth = srcIntType.getWidth();
+    uint32_t dstWidth = dstIntType.getWidth();
+    if (srcWidth == dstWidth)
+      rewriter.replaceOp(castOp, src);
+    else if (srcWidth > dstWidth)
+      rewriter.replaceOpWithNewOp<LLVM::TruncOp>(castOp, dstType, src);
+    else
+      rewriter.replaceOpWithNewOp<LLVM::ZExtOp>(castOp, dstType, src);
+
+    return success();
+  }
+};
+
 } // namespace
 
 //===----------------------------------------------------------------------===//
@@ -320,7 +359,8 @@ void index::populateIndexToLLVMConversionPatterns(
       ConvertIndexCmp,
       ConvertIndexSizeOf,
       ConvertIndexConstant,
-      ConvertIndexBoolConstant
+      ConvertIndexBoolConstant,
+      UnrealizedConversionCastOpLowering
       // clang-format on
       >(typeConverter);
 }
diff --git a/mlir/test/Conversion/IndexToLLVM/index-to-llvm.mlir b/mlir/test/Conversion/IndexToLLVM/index-to-llvm.mlir
index 1b13ebb38dc9e..8930265a6a102 100644
--- a/mlir/test/Conversion/IndexToLLVM/index-to-llvm.mlir
+++ b/mlir/test/Conversion/IndexToLLVM/index-to-llvm.mlir
@@ -194,3 +194,39 @@ func.func @index_constant() {
   %3 = index.constant 3000000000
   return
 }
+
+// CHECK-LABEL: @unrealized_conversion_cast_i32_to_i64
+// CHECK-SAME: %[[A:.*]]: i32
+// INDEX32-LABEL: @unrealized_conversion_cast_i32_to_i64
+// INDEX32-SAME: %[[A:.*]]: i32
+// INDEX64-LABEL: @unrealized_conversion_cast_i32_to_i64
+// INDEX64-SAME: %[[A:.*]]: i32
+func.func @unrealized_conversion_cast_i32_to_i64(%a: i32) -> i64 {
+  // CHECK-NEXT: %[[RET:.*]] = llvm.zext %[[A]] : i32 to i64
+  // CHECK-NEXT: return %[[RET]] : i64
+  // INDEX32-NEXT: %[[RET:.*]] = llvm.zext %[[A]] : i32 to i64
+  // INDEX32-NEXT: return %[[RET]] : i64
+  // INDEX64-NEXT: %[[RET:.*]] = llvm.zext %[[A]] : i32 to i64
+  // INDEX64-NEXT: return %[[RET]] : i64
+  %0 = builtin.unrealized_conversion_cast %a : i32 to index
+  %1 = builtin.unrealized_conversion_cast %0 : index to i64
+  return %1 : i64
+}
+
+// CHECK-LABEL: @unrealized_conversion_cast_i64_to_i32
+// CHECK-SAME: %[[A:.*]]: i64
+// INDEX32-LABEL: @unrealized_conversion_cast_i64_to_i32
+// INDEX32-SAME: %[[A:.*]]: i64
+// INDEX64-LABEL: @unrealized_conversion_cast_i64_to_i32
+// INDEX64-SAME: %[[A:.*]]: i64
+func.func @unrealized_conversion_cast_i64_to_i32(%a: i64) -> i32 {
+  // CHECK-NEXT: %[[RET:.*]] = llvm.trunc %[[A]] : i64 to i32
+  // CHECK-NEXT: return %[[RET]] : i32
+  // INDEX32-NEXT: %[[RET:.*]] = llvm.trunc %[[A]] : i64 to i32
+  // INDEX32-NEXT: return %[[RET]] : i32
+  // INDEX64-NEXT: %[[RET:.*]] = llvm.trunc %[[A]] : i64 to i32
+  // INDEX64-NEXT: return %[[RET]] : i32
+  %0 = builtin.unrealized_conversion_cast %a : i64 to index
+  %1 = builtin.unrealized_conversion_cast %0 : index to i32
+  return %1 : i32
+}
\ No newline at end of file

``````````

</details>


https://github.com/llvm/llvm-project/pull/75255


More information about the Mlir-commits mailing list