[Mlir-commits] [mlir] 12ccc59 - [mlir] Change the order of members in MLIRContext to ensure dialects are destroyed first

Laszlo Kindrat llvmlistbot at llvm.org
Thu May 25 09:18:45 PDT 2023


Author: Laszlo Kindrat
Date: 2023-05-25T12:18:32-04:00
New Revision: 12ccc59594916caf8a833f749cc820dde961d83d

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

LOG: [mlir] Change the order of members in MLIRContext to ensure dialects are destroyed first

Currently, the dialects precede the registered operations in the context object, which means that the latter is destroyed first. At the same time, Operation::~Operation dereferences the registered operation when destroying properties, which can cause use-after-free (e.g. if a dialect owns an op). This patch fixes that by changing the order of the members so that dialects come after registered operations.

Differential Revision: https://reviews.llvm.org/D151440

Added: 
    

Modified: 
    mlir/lib/IR/MLIRContext.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/IR/MLIRContext.cpp b/mlir/lib/IR/MLIRContext.cpp
index e47221f79f1b..def85a52c688 100644
--- a/mlir/lib/IR/MLIRContext.cpp
+++ b/mlir/lib/IR/MLIRContext.cpp
@@ -175,11 +175,6 @@ class MLIRContextImpl {
   /// destruction with the context.
   std::unique_ptr<llvm::ThreadPool> ownedThreadPool;
 
-  /// This is a list of dialects that are created referring to this context.
-  /// The MLIRContext owns the objects.
-  DenseMap<StringRef, std::unique_ptr<Dialect>> loadedDialects;
-  DialectRegistry dialectsRegistry;
-
   /// An allocator used for AbstractAttribute and AbstractType objects.
   llvm::BumpPtrAllocator abstractDialectSymbolAllocator;
 
@@ -193,6 +188,12 @@ class MLIRContextImpl {
   /// and efficient `getRegisteredOperations` implementation.
   SmallVector<RegisteredOperationName, 0> sortedRegisteredOperations;
 
+  /// This is a list of dialects that are created referring to this context.
+  /// The MLIRContext owns the objects. These need to be declared after the
+  /// registered operations to ensure correct destruction order.
+  DenseMap<StringRef, std::unique_ptr<Dialect>> loadedDialects;
+  DialectRegistry dialectsRegistry;
+
   /// A mutex used when accessing operation information.
   llvm::sys::SmartRWMutex<true> operationInfoMutex;
 


        


More information about the Mlir-commits mailing list