From 87a89e0f7753711ef5f2741a1494e7a44da99d21 Mon Sep 17 00:00:00 2001 From: Alex Zinenko Date: Thu, 6 Aug 2020 18:16:14 +0200 Subject: [PATCH] [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 --- .../StandardToLLVM/ConvertStandardToLLVM.h | 5 ----- mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td | 3 --- .../Conversion/StandardToLLVM/StandardToLLVM.cpp | 9 --------- .../Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp | 3 ++- mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp | 21 ++++++--------------- 5 files changed, 8 insertions(+), 33 deletions(-) diff --git a/mlir/include/mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h b/mlir/include/mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h index 1bf46b9..25bff67 100644 --- a/mlir/include/mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h +++ b/mlir/include/mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h @@ -81,8 +81,6 @@ public: /// 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 @@ public: /// 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 fb4001f..9b708fe 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 &getLLVMContextMutex(); const llvm::DataLayout &getDataLayout(); private: diff --git a/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp b/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp index b57a160..5f8a387 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 f5d66f2..12d3f50 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(), dialect->getDataLayout()); return success(); diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp index 96f2ecd..e03d025 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 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 &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 { -- 2.7.4