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

Ingo Müller llvmlistbot at llvm.org
Tue Jul 9 06:31:14 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:

On first sight, this looks good! If I remember/reconstructed things correctly, the main breakage resulted from having an interface argument instead of `Operation*`.

I have just started to CI jobs in the hope to confirm that your patch fixes my original problem:

1. [This](https://github.com/iree-org/iree-llvm-sandbox/actions/runs/9857834962/job/27217780796?pr=850) job is for a commit that reverts my work-around by changing the typeswitch argument type back to the interface type and should thus *fail*.
2. [This](https://github.com/iree-org/iree-llvm-sandbox/actions/runs/9857967690/job/27218209768?pr=850) job has an additional commit that applies the diff from this PR as a patch on the MLIR source code and should thus *succeed* in the hope that your patch fixes the previously introduced breakage.

I haven't worked on that project for several weeks so the CI caches are flushed and CI will likely take 1-2 hours. I'll report back later when I'll be able to check the results but in case I don't manage to do that today, I am sharing the links now.

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


More information about the Mlir-commits mailing list