20 #define DEBUG_TYPE "convert-to-llvm"
23 #define GEN_PASS_DEF_CONVERTTOLLVMPASS
24 #include "mlir/Conversion/Passes.h.inc"
44 LLVM_DEBUG(llvm::dbgs() <<
"Convert to LLVM extension load\n");
45 for (
Dialect *dialect : dialects) {
46 auto *iface = dyn_cast<ConvertToLLVMPatternInterface>(dialect);
49 LLVM_DEBUG(llvm::dbgs() <<
"Convert to LLVM found dialect interface for "
50 << dialect->getNamespace() <<
"\n");
51 iface->loadDependentDialects(context);
56 std::unique_ptr<DialectExtensionBase>
clone()
const final {
57 return std::make_unique<LoadDependentDialectExtension>(*
this);
64 class ConvertToLLVMPass
65 :
public impl::ConvertToLLVMPassBase<ConvertToLLVMPass> {
66 std::shared_ptr<const FrozenRewritePatternSet> patterns;
67 std::shared_ptr<const ConversionTarget> target;
68 std::shared_ptr<const LLVMTypeConverter> typeConverter;
71 using impl::ConvertToLLVMPassBase<ConvertToLLVMPass>::ConvertToLLVMPassBase;
73 registry.insert<LLVM::LLVMDialect>();
74 registry.addExtensions<LoadDependentDialectExtension>();
77 LogicalResult initialize(
MLIRContext *context)
final {
79 auto target = std::make_shared<ConversionTarget>(*context);
80 target->addLegalDialect<LLVM::LLVMDialect>();
81 auto typeConverter = std::make_shared<LLVMTypeConverter>(context);
83 if (!filterDialects.empty()) {
87 for (std::string &dialectName : filterDialects) {
88 Dialect *dialect = context->getLoadedDialect(dialectName);
91 <<
"dialect not loaded: " << dialectName <<
"\n";
92 auto *iface = dyn_cast<ConvertToLLVMPatternInterface>(dialect);
95 <<
"dialect does not implement ConvertToLLVMPatternInterface: "
96 << dialectName <<
"\n";
97 iface->populateConvertToLLVMConversionPatterns(*target, *typeConverter,
103 for (
Dialect *dialect : context->getLoadedDialects()) {
106 auto *iface = dyn_cast<ConvertToLLVMPatternInterface>(dialect);
109 iface->populateConvertToLLVMConversionPatterns(*target, *typeConverter,
115 std::make_unique<FrozenRewritePatternSet>(std::move(tempPatterns));
116 this->target = target;
117 this->typeConverter = typeConverter;
121 void runOnOperation() final {
135 return std::make_unique<ConvertToLLVMPass>();
#define MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(CLASS_NAME)
This class represents an opaque dialect extension.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
void addExtensions()
Add the given extensions to the registry.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
MLIRContext is the top-level object for a collection of MLIR operations.
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
std::unique_ptr< Pass > createConvertToLLVMPass()
Create a pass that performs dialect conversion to LLVM for all dialects implementing ConvertToLLVMPat...
void registerConvertToLLVMDependentDialectLoading(DialectRegistry ®istry)
Register the extension that will load dependent dialects for LLVM conversion.
Operation * clone(OpBuilder &b, Operation *op, TypeRange newResultTypes, ValueRange newOperands)
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.