[Mlir-commits] [mlir] [mlir][Transforms] `OneToNTypeConversion.cpp`: Fix invalid IR (PR #77922)

Matthias Springer llvmlistbot at llvm.org
Sun Jan 14 02:35:40 PST 2024


https://github.com/matthias-springer updated https://github.com/llvm/llvm-project/pull/77922

>From 49139cb169a358abeaebea98cce7c390fc9ff537 Mon Sep 17 00:00:00 2001
From: Matthias Springer <springerm at google.com>
Date: Fri, 12 Jan 2024 13:21:20 +0000
Subject: [PATCH] [mlir][Transforms] `OneToNTypeConversion.cpp`: Fix invalid IR

`buildUnrealizedCast` used to generate invalid `builtin.unrealized_conversion_cast` ops with zero results. This commit fixes `test/Conversion/OneToNTypeConversion/one-to-n-type-conversion.mlir` when running with `MLIR_ENABLE_EXPENSIVE_PATTERN_API_CHECKS`.

```
  * Pattern (anonymous namespace)::ConvertMakeTupleOp : 'test.make_tuple -> ()' {
Trying to match "(anonymous namespace)::ConvertMakeTupleOp"
    ** Insert  : 'builtin.unrealized_conversion_cast'(0x559cb66b1180)
    ** Insert  : 'builtin.unrealized_conversion_cast'(0x559cb66aa830)
    ** Insert  : 'builtin.unrealized_conversion_cast'(0x559cb66b1210)
    ** Replace : 'test.make_tuple'(0x559cb6692420)
    ** Modified: 'builtin.unrealized_conversion_cast'(0x559cb66b0ff0)
    ** Modified: 'builtin.unrealized_conversion_cast'(0x559cb66b0ac0)
    ** Erase   : 'test.make_tuple'(0x559cb6692420)
"(anonymous namespace)::ConvertMakeTupleOp" result 1
  } -> success : pattern applied successfully
// *** IR Dump After Pattern Application ***
mlir-asm-printer: Verifying operation: func.func
'builtin.unrealized_conversion_cast' op expected at least one result for cast operation
mlir-asm-printer: 'func.func' failed to verify and will be printed in generic form
"func.func"() <{function_type = (i1, i2) -> (i1, i2), sym_name = "pack_unpack"}> ({
^bb0(%arg0: i1, %arg1: i2):
  %0 = "test.make_tuple"() : () -> tuple<>
  %1 = "test.make_tuple"(%arg1) : (i2) -> tuple<i2>
  %2 = "test.make_tuple"(%1) : (tuple<i2>) -> tuple<tuple<i2>>
  "builtin.unrealized_conversion_cast"(%0) {"__one-to-n-type-conversion_cast-kind__" = "target"} : (tuple<>) -> ()
  %3 = "builtin.unrealized_conversion_cast"(%2) {"__one-to-n-type-conversion_cast-kind__" = "target"} : (tuple<tuple<i2>>) -> i2
  %4 = "builtin.unrealized_conversion_cast"(%arg0, %3) {"__one-to-n-type-conversion_cast-kind__" = "source"} : (i1, i2) -> tuple<tuple<>, i1, tuple<tuple<i2>>>
  %5:2 = "builtin.unrealized_conversion_cast"(%4) {"__one-to-n-type-conversion_cast-kind__" = "target"} : (tuple<tuple<>, i1, tuple<tuple<i2>>>) -> (i1, i2)
  %6:2 = "builtin.unrealized_conversion_cast"(%4) {"__one-to-n-type-conversion_cast-kind__" = "target"} : (tuple<tuple<>, i1, tuple<tuple<i2>>>) -> (i1, i2)
  "func.return"(%5#0, %6#1) : (i1, i2) -> ()
}) : () -> ()

within split at /usr/local/google/home/springerm/mlir_public/llvm-project/mlir/test/Conversion/OneToNTypeConversion/one-to-n-type-conversion.mlir:1 offset :20:8: error: 'builtin.unrealized_conversion_cast' op expected at least one result for cast operation
  %0 = "test.make_tuple"() : () -> tuple<>
       ^
within split at /usr/local/google/home/springerm/mlir_public/llvm-project/mlir/test/Conversion/OneToNTypeConversion/one-to-n-type-conversion.mlir:1 offset :20:8: note: see current operation: "builtin.unrealized_conversion_cast"(%0) {"__one-to-n-type-conversion_cast-kind__" = "target"} : (tuple<>) -> ()
LLVM ERROR: IR failed to verify after pattern application
```
---
 mlir/lib/Transforms/Utils/OneToNTypeConversion.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/mlir/lib/Transforms/Utils/OneToNTypeConversion.cpp b/mlir/lib/Transforms/Utils/OneToNTypeConversion.cpp
index c95aa608636d8a..4c67ee30d21f75 100644
--- a/mlir/lib/Transforms/Utils/OneToNTypeConversion.cpp
+++ b/mlir/lib/Transforms/Utils/OneToNTypeConversion.cpp
@@ -113,6 +113,11 @@ static const char *const castKindAttrName =
 /// result types. Returns the result values of the cast.
 static ValueRange buildUnrealizedCast(OpBuilder &builder, TypeRange resultTypes,
                                       ValueRange inputs, CastKind kind) {
+  // Special case: 1-to-N conversion with N = 0. No need to build an
+  // UnrealizedConversionCastOp because the op will always be dead.
+  if (resultTypes.empty())
+    return ValueRange();
+
   // Create cast.
   Location loc = builder.getUnknownLoc();
   if (!inputs.empty())



More information about the Mlir-commits mailing list