[Mlir-commits] [mlir] cedeb31 - [mlir] Support null interface to base conversion (#65988)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Sep 11 23:42:52 PDT 2023


Author: Markus Böck
Date: 2023-09-12T08:42:47+02:00
New Revision: cedeb31e6d08b1e1e134478b36dd7ed1ef6221a0

URL: https://github.com/llvm/llvm-project/commit/cedeb31e6d08b1e1e134478b36dd7ed1ef6221a0
DIFF: https://github.com/llvm/llvm-project/commit/cedeb31e6d08b1e1e134478b36dd7ed1ef6221a0.diff

LOG: [mlir] Support null interface to base conversion (#65988)

The current implicit conversion operator from an interface to a "base
interface" of the interface unconditionally calls `this->getImpl()`
which leads to accessing a null pointer if the interface instance is a
null instance.

This PR changes the ODS generated interface instance to explicitly check
and then return a null interface instance if the `this` instance is a
null instance.

Added: 
    

Modified: 
    mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
    mlir/unittests/IR/InterfaceTest.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp b/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
index 65c1e6392b1316e..9672a02cc08f68c 100644
--- a/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
@@ -571,6 +571,7 @@ void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
 
     // Allow implicit conversion to the base interface.
     os << "  operator " << baseQualName << " () const {\n"
+       << "    if (!*this) return nullptr;\n"
        << "    return " << baseQualName << "(*this, getImpl()->impl"
        << base.getName() << ");\n"
        << "  }\n\n";

diff  --git a/mlir/unittests/IR/InterfaceTest.cpp b/mlir/unittests/IR/InterfaceTest.cpp
index 621a1c5fa18f3f8..5ab4d9a106231a1 100644
--- a/mlir/unittests/IR/InterfaceTest.cpp
+++ b/mlir/unittests/IR/InterfaceTest.cpp
@@ -70,3 +70,16 @@ TEST(InterfaceTest, TestCustomClassOf) {
   EXPECT_FALSE(isa<TestOptionallyImplementedOpInterface>(*op));
   op.erase();
 }
+
+TEST(InterfaceTest, TestImplicitConversion) {
+  MLIRContext context;
+  context.loadDialect<test::TestDialect>();
+
+  TestBaseTypeInterfacePrintTypeB typeB;
+  TestBaseTypeInterfacePrintTypeA typeA = typeB;
+  EXPECT_EQ(typeA, nullptr);
+
+  typeB = TestType::get(&context);
+  typeA = typeB;
+  EXPECT_EQ(typeA, typeB);
+}


        


More information about the Mlir-commits mailing list