Skip to content

Commit cedeb31

Browse files
authored
[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.
1 parent cd4b906 commit cedeb31

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

mlir/tools/mlir-tblgen/OpInterfacesGen.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
571571

572572
// Allow implicit conversion to the base interface.
573573
os << " operator " << baseQualName << " () const {\n"
574+
<< " if (!*this) return nullptr;\n"
574575
<< " return " << baseQualName << "(*this, getImpl()->impl"
575576
<< base.getName() << ");\n"
576577
<< " }\n\n";

mlir/unittests/IR/InterfaceTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,16 @@ TEST(InterfaceTest, TestCustomClassOf) {
7070
EXPECT_FALSE(isa<TestOptionallyImplementedOpInterface>(*op));
7171
op.erase();
7272
}
73+
74+
TEST(InterfaceTest, TestImplicitConversion) {
75+
MLIRContext context;
76+
context.loadDialect<test::TestDialect>();
77+
78+
TestBaseTypeInterfacePrintTypeB typeB;
79+
TestBaseTypeInterfacePrintTypeA typeA = typeB;
80+
EXPECT_EQ(typeA, nullptr);
81+
82+
typeB = TestType::get(&context);
83+
typeA = typeB;
84+
EXPECT_EQ(typeA, typeB);
85+
}

0 commit comments

Comments
 (0)