[Mlir-commits] [mlir] 87a89e0 - [mlir] Remove llvm::LLVMContext and llvm::Module from mlir::LLVMDialectImpl

Alex Zinenko llvmlistbot at llvm.org
Fri Aug 7 05:30:38 PDT 2020


Author: Alex Zinenko
Date: 2020-08-07T14:30:31+02:00
New Revision: 87a89e0f7753711ef5f2741a1494e7a44da99d21

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

LOG: [mlir] Remove llvm::LLVMContext and llvm::Module from mlir::LLVMDialectImpl

Original modeling of LLVM IR types in the MLIR LLVM dialect had been wrapping
LLVM IR types and therefore required the LLVMContext in which they were created
to outlive them, which was solved by placing the LLVMContext inside the dialect
and thus having the lifetime of MLIRContext. This has led to numerous issues
caused by the lack of thread-safety of LLVMContext and the need to re-create
LLVM IR modules, obtained by translating from MLIR, in different LLVM contexts
to enable parallel compilation. Similarly, llvm::Module had been introduced to
keep track of identified structure types that could not be modeled properly.

A recent series of commits changed the modeling of LLVM IR types in the MLIR
LLVM dialect so that it no longer wraps LLVM IR types and has no dependence on
LLVMContext and changed the ownership model of the translated LLVM IR modules.
Remove LLVMContext and LLVM modules from the implementation of MLIR LLVM
dialect and clean up the remaining uses.

The only part of LLVM IR that remains necessary for the LLVM dialect is the
data layout. It should be moved from the dialect level to the module level and
replaced with an MLIR-based representation to remove the dependency of the
LLVMDialect on LLVM IR library.

Reviewed By: rriddle

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

Added: 
    

Modified: 
    mlir/include/mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h
    mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
    mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
    mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
    mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h b/mlir/include/mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h
index 1bf46b91b7a6..25bff67d4a6e 100644
--- a/mlir/include/mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h
+++ b/mlir/include/mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h
@@ -81,8 +81,6 @@ class LLVMTypeConverter : public TypeConverter {
   /// Returns the MLIR context.
   MLIRContext &getContext();
 
-  /// Returns the LLVM context.
-  llvm::LLVMContext &getLLVMContext();
 
   /// Returns the LLVM dialect.
   LLVM::LLVMDialect *getDialect() { return llvmDialect; }
@@ -396,9 +394,6 @@ class ConvertToLLVMPattern : public ConversionPattern {
   /// Returns the LLVM dialect.
   LLVM::LLVMDialect &getDialect() const;
 
-  /// Returns the LLVM IR context.
-  llvm::LLVMContext &getContext() const;
-
   /// Gets the MLIR type wrapping the LLVM integer type whose bit width is
   /// defined by the used type converter.
   LLVM::LLVMType getIndexType() const;

diff  --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
index fb4001f1bc9b..9b708fea9037 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
@@ -22,9 +22,6 @@ def LLVM_Dialect : Dialect {
   let hasRegionArgAttrVerify = 1;
   let extraClassDeclaration = [{
     ~LLVMDialect();
-    llvm::LLVMContext &getLLVMContext();
-    llvm::Module &getLLVMModule();
-    llvm::sys::SmartMutex<true> &getLLVMContextMutex();
     const llvm::DataLayout &getDataLayout();
 
   private:

diff  --git a/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp b/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
index b57a1607bc97..5f8a38743400 100644
--- a/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
+++ b/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
@@ -193,11 +193,6 @@ MLIRContext &LLVMTypeConverter::getContext() {
   return *getDialect()->getContext();
 }
 
-/// Get the LLVM context.
-llvm::LLVMContext &LLVMTypeConverter::getLLVMContext() {
-  return llvmDialect->getLLVMContext();
-}
-
 LLVM::LLVMType LLVMTypeConverter::getIndexType() {
   return LLVM::LLVMType::getIntNTy(&getContext(), getIndexTypeBitwidth());
 }
@@ -844,10 +839,6 @@ LLVM::LLVMDialect &ConvertToLLVMPattern::getDialect() const {
   return *typeConverter.getDialect();
 }
 
-llvm::LLVMContext &ConvertToLLVMPattern::getContext() const {
-  return typeConverter.getLLVMContext();
-}
-
 LLVM::LLVMType ConvertToLLVMPattern::getIndexType() const {
   return typeConverter.getIndexType();
 }

diff  --git a/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp b/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
index f5d66f28f0ce..12d3f5042bcd 100644
--- a/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
+++ b/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
@@ -129,7 +129,8 @@ LogicalResult getMemRefAlignment(LLVMTypeConverter &typeConverter, T op,
   // TODO: this should use the MLIR data layout when it becomes available and
   // stop depending on translation.
   LLVM::LLVMDialect *dialect = typeConverter.getDialect();
-  align = LLVM::TypeToLLVMIRTranslator(dialect->getLLVMContext())
+  llvm::LLVMContext llvmContext;
+  align = LLVM::TypeToLLVMIRTranslator(llvmContext)
               .getPreferredAlignment(elementTy.cast<LLVM::LLVMType>(),
                                      dialect->getDataLayout());
   return success();

diff  --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index 96f2ecd049bb..e03d0256eea4 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -1672,14 +1672,12 @@ namespace mlir {
 namespace LLVM {
 namespace detail {
 struct LLVMDialectImpl {
-  LLVMDialectImpl() : module("LLVMDialectModule", llvmContext) {}
+  LLVMDialectImpl() : layout("") {}
 
-  llvm::LLVMContext llvmContext;
-  llvm::Module module;
-
-  /// A smart mutex to lock access to the llvm context. Unlike MLIR, LLVM is not
-  /// multi-threaded and requires locked access to prevent race conditions.
-  llvm::sys::SmartMutex<true> mutex;
+  /// Default data layout to use.
+  // TODO: this should be moved to some Op equivalent to LLVM module and
+  // eventually replaced with a proper MLIR data layout.
+  llvm::DataLayout layout;
 };
 } // end namespace detail
 } // end namespace LLVM
@@ -1723,14 +1721,7 @@ LLVMDialect::~LLVMDialect() {}
 #define GET_OP_CLASSES
 #include "mlir/Dialect/LLVMIR/LLVMOps.cpp.inc"
 
-llvm::LLVMContext &LLVMDialect::getLLVMContext() { return impl->llvmContext; }
-llvm::Module &LLVMDialect::getLLVMModule() { return impl->module; }
-llvm::sys::SmartMutex<true> &LLVMDialect::getLLVMContextMutex() {
-  return impl->mutex;
-}
-const llvm::DataLayout &LLVMDialect::getDataLayout() {
-  return impl->module.getDataLayout();
-}
+const llvm::DataLayout &LLVMDialect::getDataLayout() { return impl->layout; }
 
 /// Parse a type registered to this dialect.
 Type LLVMDialect::parseType(DialectAsmParser &parser) const {


        


More information about the Mlir-commits mailing list