[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
This commit is contained in:
Alex Zinenko 2020-08-06 18:16:14 +02:00
parent 16b0225377
commit 87a89e0f77
5 changed files with 8 additions and 33 deletions

View File

@ -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;

View File

@ -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:

View File

@ -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();
}

View File

@ -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();

View File

@ -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 {