[Mlir-commits] [mlir] [mlir] fix Undefined behavior in CastInfo::castFailed with From=<MLIRinterface> (PR #87145)

Ingo Müller llvmlistbot at llvm.org
Wed Jul 10 01:54:57 PDT 2024


================
@@ -84,3 +87,40 @@ TEST(InterfaceTest, TestImplicitConversion) {
   typeA = typeB;
   EXPECT_EQ(typeA, typeB);
 }
+
+TEST(OperationInterfaceTest, CastOpToInterface) {
+  DialectRegistry registry;
+  MLIRContext ctx;
+
+  const char *ir = R"MLIR(
+      func.func @map(%arg : tensor<1xi64>) {
+        %0 = arith.constant dense<[10]> : tensor<1xi64>
+        %1 = arith.addi %arg, %0 : tensor<1xi64>
+        return
+      }
+    )MLIR";
+
+  registry.insert<func::FuncDialect, arith::ArithDialect>();
+  ctx.appendDialectRegistry(registry);
+  OwningOpRef<ModuleOp> module = parseSourceString<ModuleOp>(ir, &ctx);
+  Operation &op = cast<func::FuncOp>(module->front()).getBody().front().front();
+
+  OpAsmOpInterface interface = llvm::cast<OpAsmOpInterface>(op);
+
+  bool constantOp =
+      llvm::TypeSwitch<OpAsmOpInterface, bool>(interface)
+          .Case<VectorUnrollOpInterface, arith::ConstantOp>([&](auto op) {
+            return std::is_same_v<decltype(op), arith::ConstantOp>;
+          });
+
+  EXPECT_TRUE(constantOp);
+
+  EXPECT_FALSE(llvm::isa<VectorUnrollOpInterface>(interface));
+  EXPECT_FALSE(llvm::dyn_cast<VectorUnrollOpInterface>(interface));
+
+  EXPECT_TRUE(llvm::isa<InferTypeOpInterface>(interface));
+  EXPECT_TRUE(llvm::dyn_cast<InferTypeOpInterface>(interface));
+
+  EXPECT_TRUE(llvm::isa<OpAsmOpInterface>(interface));
+  EXPECT_TRUE(llvm::dyn_cast<OpAsmOpInterface>(interface));
+}
----------------
ingomueller-net wrote:

The CI jobs finished and failed both. I have just rerun them now with the tracing from #86647. Unfortunately, the version with the patch ([CI job](https://github.com/iree-org/iree-llvm-sandbox/actions/runs/9870555351/job/27256554874?pr=850)) still fails and the tracing shows that the type switch takes the wrong branch:

```
auto (anonymous namespace)::exportOperation(mlir::substrait::ExpressionOpInterface)::(anonymous class)::operator()(auto) const [op:auto = mlir::substrait::FieldReferenceOp]
%3 = substrait.literal -1 : si1
```

I also made sure that `OpDefinition.h` is included by including it directly but CI still [fails](https://github.com/iree-org/iree-llvm-sandbox/actions/runs/9870726034/job/27257087019?pr=850) with the same tracing output.

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


More information about the Mlir-commits mailing list