diff --git a/mlir/include/mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h b/mlir/include/mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h --- a/mlir/include/mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h +++ b/mlir/include/mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h @@ -40,13 +40,15 @@ /// Hook for derived dialect interface to provide conversion patterns /// and mark dialect legal for the conversion target. virtual void populateConvertToLLVMConversionPatterns( - ConversionTarget &target, RewritePatternSet &patterns) const = 0; + ConversionTarget &target, LLVMTypeConverter &typeConverter, + RewritePatternSet &patterns) const = 0; }; /// Recursively walk the IR and collect all dialects implementing the interface, /// and populate the conversion patterns. void populateConversionTargetFromOperation(Operation *op, ConversionTarget &target, + LLVMTypeConverter &typeConverter, RewritePatternSet &patterns); } // namespace mlir diff --git a/mlir/include/mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h b/mlir/include/mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h --- a/mlir/include/mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h +++ b/mlir/include/mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h @@ -12,6 +12,7 @@ #include namespace mlir { +class DialectRegistry; class Pass; class LLVMTypeConverter; class RewritePatternSet; @@ -23,6 +24,9 @@ /// MemRef dialect to the LLVM dialect. void populateFinalizeMemRefToLLVMConversionPatterns( LLVMTypeConverter &converter, RewritePatternSet &patterns); + +void registerConvertMemRefToLLVMInterface(DialectRegistry ®istry); + } // namespace mlir #endif // MLIR_CONVERSION_MEMREFTOLLVM_MEMREFTOLLVM_H diff --git a/mlir/include/mlir/InitAllExtensions.h b/mlir/include/mlir/InitAllExtensions.h --- a/mlir/include/mlir/InitAllExtensions.h +++ b/mlir/include/mlir/InitAllExtensions.h @@ -14,6 +14,7 @@ #ifndef MLIR_INITALLEXTENSIONS_H_ #define MLIR_INITALLEXTENSIONS_H_ +#include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h" #include "mlir/Conversion/NVVMToLLVM/NVVMToLLVM.h" #include "mlir/Dialect/Func/Extensions/AllExtensions.h" @@ -28,6 +29,7 @@ /// pipelines and transformations you are using. inline void registerAllExtensions(DialectRegistry ®istry) { func::registerAllExtensions(registry); + registerConvertMemRefToLLVMInterface(registry); registerConvertNVVMToLLVMInterface(registry); } diff --git a/mlir/lib/Conversion/ConvertToLLVM/CMakeLists.txt b/mlir/lib/Conversion/ConvertToLLVM/CMakeLists.txt --- a/mlir/lib/Conversion/ConvertToLLVM/CMakeLists.txt +++ b/mlir/lib/Conversion/ConvertToLLVM/CMakeLists.txt @@ -22,6 +22,7 @@ LINK_LIBS PUBLIC MLIRConvertToLLVMInterface MLIRIR + MLIRLLVMCommonConversion MLIRLLVMDialect MLIRPass MLIRRewrite diff --git a/mlir/lib/Conversion/ConvertToLLVM/ConvertToLLVMPass.cpp b/mlir/lib/Conversion/ConvertToLLVM/ConvertToLLVMPass.cpp --- a/mlir/lib/Conversion/ConvertToLLVM/ConvertToLLVMPass.cpp +++ b/mlir/lib/Conversion/ConvertToLLVM/ConvertToLLVMPass.cpp @@ -9,6 +9,7 @@ #include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h" #include "mlir/Conversion/ConvertToLLVM/ToLLVMPass.h" #include "mlir/Conversion/LLVMCommon/ConversionTarget.h" +#include "mlir/Conversion/LLVMCommon/TypeConverter.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" #include "mlir/IR/PatternMatch.h" #include "mlir/Pass/Pass.h" @@ -62,6 +63,7 @@ : public impl::ConvertToLLVMPassBase { std::shared_ptr patterns; std::shared_ptr target; + std::shared_ptr typeConverter; public: using impl::ConvertToLLVMPassBase::ConvertToLLVMPassBase; @@ -72,23 +74,26 @@ ConvertToLLVMPass(const ConvertToLLVMPass &other) : ConvertToLLVMPassBase(other), patterns(other.patterns), - target(other.target) {} + target(other.target), typeConverter(other.typeConverter) {} LogicalResult initialize(MLIRContext *context) final { RewritePatternSet tempPatterns(context); auto target = std::make_shared(*context); target->addLegalDialect(); + auto typeConverter = std::make_shared(context); for (Dialect *dialect : context->getLoadedDialects()) { // First time we encounter this dialect: if it implements the interface, // let's populate patterns ! auto iface = dyn_cast(dialect); if (!iface) continue; - iface->populateConvertToLLVMConversionPatterns(*target, tempPatterns); + iface->populateConvertToLLVMConversionPatterns(*target, *typeConverter, + tempPatterns); } patterns = std::make_unique(std::move(tempPatterns)); this->target = target; + this->typeConverter = typeConverter; return success(); } diff --git a/mlir/lib/Conversion/ConvertToLLVM/ToLLVMInterface.cpp b/mlir/lib/Conversion/ConvertToLLVM/ToLLVMInterface.cpp --- a/mlir/lib/Conversion/ConvertToLLVM/ToLLVMInterface.cpp +++ b/mlir/lib/Conversion/ConvertToLLVM/ToLLVMInterface.cpp @@ -13,9 +13,9 @@ using namespace mlir; -void mlir::populateConversionTargetFromOperation(Operation *root, - ConversionTarget &target, - RewritePatternSet &patterns) { +void mlir::populateConversionTargetFromOperation( + Operation *root, ConversionTarget &target, LLVMTypeConverter &typeConverter, + RewritePatternSet &patterns) { DenseSet dialects; root->walk([&](Operation *op) { Dialect *dialect = op->getDialect(); @@ -26,6 +26,7 @@ auto iface = dyn_cast(dialect); if (!iface) return; - iface->populateConvertToLLVMConversionPatterns(target, patterns); + iface->populateConvertToLLVMConversionPatterns(target, typeConverter, + patterns); }); } diff --git a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp --- a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp +++ b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp @@ -9,6 +9,7 @@ #include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h" #include "mlir/Analysis/DataLayoutAnalysis.h" +#include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h" #include "mlir/Conversion/LLVMCommon/ConversionTarget.h" #include "mlir/Conversion/LLVMCommon/Pattern.h" #include "mlir/Conversion/LLVMCommon/TypeConverter.h" @@ -1935,4 +1936,27 @@ signalPassFailure(); } }; + +/// Implement the interface to convert MemRef to LLVM. +struct MemRefToLLVMDialectInterface : public ConvertToLLVMPatternInterface { + using ConvertToLLVMPatternInterface::ConvertToLLVMPatternInterface; + void loadDependentDialects(MLIRContext *context) const final { + context->loadDialect(); + } + + /// Hook for derived dialect interface to provide conversion patterns + /// and mark dialect legal for the conversion target. + void populateConvertToLLVMConversionPatterns( + ConversionTarget &target, LLVMTypeConverter &typeConverter, + RewritePatternSet &patterns) const final { + populateFinalizeMemRefToLLVMConversionPatterns(typeConverter, patterns); + } +}; + } // namespace + +void mlir::registerConvertMemRefToLLVMInterface(DialectRegistry ®istry) { + registry.addExtension(+[](MLIRContext *ctx, memref::MemRefDialect *dialect) { + dialect->addInterfaces(); + }); +} diff --git a/mlir/lib/Conversion/NVVMToLLVM/NVVMToLLVM.cpp b/mlir/lib/Conversion/NVVMToLLVM/NVVMToLLVM.cpp --- a/mlir/lib/Conversion/NVVMToLLVM/NVVMToLLVM.cpp +++ b/mlir/lib/Conversion/NVVMToLLVM/NVVMToLLVM.cpp @@ -201,7 +201,8 @@ /// Hook for derived dialect interface to provide conversion patterns /// and mark dialect legal for the conversion target. void populateConvertToLLVMConversionPatterns( - ConversionTarget &target, RewritePatternSet &patterns) const final { + ConversionTarget &target, LLVMTypeConverter &typeConverter, + RewritePatternSet &patterns) const final { populateNVVMToLLVMConversionPatterns(patterns); } };