[Mlir-commits] [mlir] [mlir] Guard expensive string-based asserts behind EXPENSIVE_CHECKS (PR #93111)

Jeff Niu llvmlistbot at llvm.org
Wed May 22 16:47:58 PDT 2024


https://github.com/Mogball created https://github.com/llvm/llvm-project/pull/93111

These string based asserts are important but they are extremely expensive. In compilation workloads with lots of interface checking, this can slow down as much as 3x.

>From 2ad42a93d0f054311d3cde9cd4dfaef8d6c671c6 Mon Sep 17 00:00:00 2001
From: Mogball <jeff at modular.com>
Date: Wed, 22 May 2024 16:45:06 -0700
Subject: [PATCH] [mlir] Guard expensive string-based asserts behind
 EXPENSIVE_CHECKS

These string based asserts are important but they are extremely
expensive. In compilation workloads with lots of interface checking,
this can slow down as much as 3x.
---
 mlir/include/mlir/IR/Attributes.h   | 19 ++++++++-----------
 mlir/include/mlir/IR/Dialect.h      |  4 ++--
 mlir/include/mlir/IR/OpDefinition.h |  4 ++--
 mlir/include/mlir/IR/Types.h        | 19 ++++++++-----------
 4 files changed, 20 insertions(+), 26 deletions(-)

diff --git a/mlir/include/mlir/IR/Attributes.h b/mlir/include/mlir/IR/Attributes.h
index 8a077865b51b5..a8a9c6ada33e9 100644
--- a/mlir/include/mlir/IR/Attributes.h
+++ b/mlir/include/mlir/IR/Attributes.h
@@ -50,20 +50,17 @@ class Attribute {
   /// Casting utility functions. These are deprecated and will be removed,
   /// please prefer using the `llvm` namespace variants instead.
   template <typename... Tys>
-  [[deprecated("Use mlir::isa<U>() instead")]]
-  bool isa() const;
+  [[deprecated("Use mlir::isa<U>() instead")]] bool isa() const;
   template <typename... Tys>
-  [[deprecated("Use mlir::isa_and_nonnull<U>() instead")]]
-  bool isa_and_nonnull() const;
+  [[deprecated("Use mlir::isa_and_nonnull<U>() instead")]] bool
+  isa_and_nonnull() const;
   template <typename U>
-  [[deprecated("Use mlir::dyn_cast<U>() instead")]]
-  U dyn_cast() const;
+  [[deprecated("Use mlir::dyn_cast<U>() instead")]] U dyn_cast() const;
   template <typename U>
-  [[deprecated("Use mlir::dyn_cast_or_null<U>() instead")]]
-  U dyn_cast_or_null() const;
+  [[deprecated("Use mlir::dyn_cast_or_null<U>() instead")]] U
+  dyn_cast_or_null() const;
   template <typename U>
-  [[deprecated("Use mlir::cast<U>() instead")]]
-  U cast() const;
+  [[deprecated("Use mlir::cast<U>() instead")]] U cast() const;
 
   /// Return a unique identifier for the concrete attribute type. This is used
   /// to support dynamic type casting.
@@ -303,7 +300,7 @@ class AttributeInterface
 protected:
   /// Returns the impl interface instance for the given type.
   static typename InterfaceBase::Concept *getInterfaceFor(Attribute attr) {
-#ifndef NDEBUG
+#ifdef EXPENSIVE_CHECKS
     // Check that the current interface isn't an unresolved promise for the
     // given attribute.
     dialect_extension_detail::handleUseOfUndefinedPromisedInterface(
diff --git a/mlir/include/mlir/IR/Dialect.h b/mlir/include/mlir/IR/Dialect.h
index f7c1f4df16fc4..22517cd2e07fb 100644
--- a/mlir/include/mlir/IR/Dialect.h
+++ b/mlir/include/mlir/IR/Dialect.h
@@ -159,7 +159,7 @@ class Dialect {
   /// Lookup an interface for the given ID if one is registered, otherwise
   /// nullptr.
   DialectInterface *getRegisteredInterface(TypeID interfaceID) {
-#ifndef NDEBUG
+#ifdef EXPENSIVE_CHECKS
     handleUseOfUndefinedPromisedInterface(getTypeID(), interfaceID);
 #endif
 
@@ -168,7 +168,7 @@ class Dialect {
   }
   template <typename InterfaceT>
   InterfaceT *getRegisteredInterface() {
-#ifndef NDEBUG
+#ifdef EXPENSIVE_CHECKS
     handleUseOfUndefinedPromisedInterface(getTypeID(),
                                           InterfaceT::getInterfaceID(),
                                           llvm::getTypeName<InterfaceT>());
diff --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h
index 59f094d669099..15f67b9ae5b49 100644
--- a/mlir/include/mlir/IR/OpDefinition.h
+++ b/mlir/include/mlir/IR/OpDefinition.h
@@ -1689,7 +1689,7 @@ class Op : public OpState, public Traits<ConcreteType>... {
   static bool classof(Operation *op) {
     if (auto info = op->getRegisteredInfo())
       return TypeID::get<ConcreteType>() == info->getTypeID();
-#ifndef NDEBUG
+#ifdef EXPENSIVE_CHECKS
     if (op->getName().getStringRef() == ConcreteType::getOperationName())
       llvm::report_fatal_error(
           "classof on '" + ConcreteType::getOperationName() +
@@ -2090,7 +2090,7 @@ class OpInterface
   static typename InterfaceBase::Concept *getInterfaceFor(Operation *op) {
     OperationName name = op->getName();
 
-#ifndef NDEBUG
+#ifdef EXPENSIVE_CHECKS
     // Check that the current interface isn't an unresolved promise for the
     // given operation.
     if (Dialect *dialect = name.getDialect()) {
diff --git a/mlir/include/mlir/IR/Types.h b/mlir/include/mlir/IR/Types.h
index 65824531fdc90..c013886164098 100644
--- a/mlir/include/mlir/IR/Types.h
+++ b/mlir/include/mlir/IR/Types.h
@@ -97,20 +97,17 @@ class Type {
   bool operator!() const { return impl == nullptr; }
 
   template <typename... Tys>
-  [[deprecated("Use mlir::isa<U>() instead")]]
-  bool isa() const;
+  [[deprecated("Use mlir::isa<U>() instead")]] bool isa() const;
   template <typename... Tys>
-  [[deprecated("Use mlir::isa_and_nonnull<U>() instead")]]
-  bool isa_and_nonnull() const;
+  [[deprecated("Use mlir::isa_and_nonnull<U>() instead")]] bool
+  isa_and_nonnull() const;
   template <typename U>
-  [[deprecated("Use mlir::dyn_cast<U>() instead")]]
-  U dyn_cast() const;
+  [[deprecated("Use mlir::dyn_cast<U>() instead")]] U dyn_cast() const;
   template <typename U>
-  [[deprecated("Use mlir::dyn_cast_or_null<U>() instead")]]
-  U dyn_cast_or_null() const;
+  [[deprecated("Use mlir::dyn_cast_or_null<U>() instead")]] U
+  dyn_cast_or_null() const;
   template <typename U>
-  [[deprecated("Use mlir::cast<U>() instead")]]
-  U cast() const;
+  [[deprecated("Use mlir::cast<U>() instead")]] U cast() const;
 
   /// Return a unique identifier for the concrete type. This is used to support
   /// dynamic type casting.
@@ -285,7 +282,7 @@ class TypeInterface : public detail::Interface<ConcreteType, Type, Traits, Type,
 protected:
   /// Returns the impl interface instance for the given type.
   static typename InterfaceBase::Concept *getInterfaceFor(Type type) {
-#ifndef NDEBUG
+#ifdef EXPENSIVE_CHECKS
     // Check that the current interface isn't an unresolved promise for the
     // given type.
     dialect_extension_detail::handleUseOfUndefinedPromisedInterface(



More information about the Mlir-commits mailing list