Skip to content

[mlir] Support null interface to base conversion #65988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2023

Conversation

zero9178
Copy link
Member

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.

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.
@zero9178 zero9178 requested a review from a team as a code owner September 11, 2023 18:10
@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Sep 11, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 11, 2023

@llvm/pr-subscribers-mlir-core

Changes

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.

Full diff: https://github.com/llvm/llvm-project/pull/65988.diff

2 Files Affected:

  • (modified) mlir/tools/mlir-tblgen/OpInterfacesGen.cpp (+1)
  • (modified) mlir/unittests/IR/InterfaceTest.cpp (+13)
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(*op));
   op.erase();
 }
+
+TEST(InterfaceTest, TestImplicitConversion) {
+  MLIRContext context;
+  context.loadDialect();
+
+  TestBaseTypeInterfacePrintTypeB typeB;
+  TestBaseTypeInterfacePrintTypeA typeA = typeB;
+  EXPECT_EQ(typeA, nullptr);
+
+  typeB = TestType::get(&context);
+  typeA = typeB;
+  EXPECT_EQ(typeA, typeB);
+}

@llvmbot
Copy link
Member

llvmbot commented Sep 11, 2023

@llvm/pr-subscribers-mlir

Changes

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.

Full diff: https://github.com/llvm/llvm-project/pull/65988.diff

2 Files Affected:

  • (modified) mlir/tools/mlir-tblgen/OpInterfacesGen.cpp (+1)
  • (modified) mlir/unittests/IR/InterfaceTest.cpp (+13)
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(*op));
   op.erase();
 }
+
+TEST(InterfaceTest, TestImplicitConversion) {
+  MLIRContext context;
+  context.loadDialect();
+
+  TestBaseTypeInterfacePrintTypeB typeB;
+  TestBaseTypeInterfacePrintTypeA typeA = typeB;
+  EXPECT_EQ(typeA, nullptr);
+
+  typeB = TestType::get(&context);
+  typeA = typeB;
+  EXPECT_EQ(typeA, typeB);
+}

@zero9178 zero9178 merged commit cedeb31 into llvm:main Sep 12, 2023
@zero9178 zero9178 deleted the null-interface-base-conversion branch September 12, 2023 06:42
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants