20 #define DEBUG_TYPE "convert-to-llvm"
23 #define GEN_PASS_DEF_CONVERTTOLLVMPASS
24 #include "mlir/Conversion/Passes.h.inc"
42 LLVM_DEBUG(llvm::dbgs() <<
"Convert to LLVM extension load\n");
43 for (
Dialect *dialect : dialects) {
44 auto *iface = dyn_cast<ConvertToLLVMPatternInterface>(dialect);
47 LLVM_DEBUG(llvm::dbgs() <<
"Convert to LLVM found dialect interface for "
48 << dialect->getNamespace() <<
"\n");
49 iface->loadDependentDialects(context);
54 std::unique_ptr<DialectExtensionBase>
clone()
const final {
55 return std::make_unique<LoadDependentDialectExtension>(*
this);
62 class ConvertToLLVMPass
63 :
public impl::ConvertToLLVMPassBase<ConvertToLLVMPass> {
64 std::shared_ptr<const FrozenRewritePatternSet> patterns;
65 std::shared_ptr<const ConversionTarget> target;
66 std::shared_ptr<const LLVMTypeConverter> typeConverter;
69 using impl::ConvertToLLVMPassBase<ConvertToLLVMPass>::ConvertToLLVMPassBase;
71 registry.insert<LLVM::LLVMDialect>();
72 registry.addExtensions<LoadDependentDialectExtension>();
77 auto target = std::make_shared<ConversionTarget>(*context);
78 target->addLegalDialect<LLVM::LLVMDialect>();
79 auto typeConverter = std::make_shared<LLVMTypeConverter>(context);
81 if (!filterDialects.empty()) {
85 for (std::string &dialectName : filterDialects) {
86 Dialect *dialect = context->getLoadedDialect(dialectName);
89 <<
"dialect not loaded: " << dialectName <<
"\n";
90 auto *iface = dyn_cast<ConvertToLLVMPatternInterface>(dialect);
93 <<
"dialect does not implement ConvertToLLVMPatternInterface: "
94 << dialectName <<
"\n";
95 iface->populateConvertToLLVMConversionPatterns(*target, *typeConverter,
101 for (
Dialect *dialect : context->getLoadedDialects()) {
104 auto *iface = dyn_cast<ConvertToLLVMPatternInterface>(dialect);
107 iface->populateConvertToLLVMConversionPatterns(*target, *typeConverter,
113 std::make_unique<FrozenRewritePatternSet>(std::move(tempPatterns));
114 this->target = target;
115 this->typeConverter = typeConverter;
119 void runOnOperation() final {
133 return std::make_unique<ConvertToLLVMPass>();
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...
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
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.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
This class represents an efficient way to signal success or failure.