17 #include "llvm/Support/DebugLog.h"
20 #define DEBUG_TYPE "convert-to-llvm"
23 #define GEN_PASS_DEF_CONVERTTOLLVMPASS
24 #include "mlir/Conversion/Passes.h.inc"
32 class ConvertToLLVMPassInterface {
36 bool allowPatternRollback =
true);
37 virtual ~ConvertToLLVMPassInterface() =
default;
45 virtual LogicalResult initialize() = 0;
52 virtual LogicalResult transform(
Operation *op,
60 LogicalResult visitInterfaces(
67 bool allowPatternRollback;
83 LDBG() <<
"Convert to LLVM extension load";
84 for (
Dialect *dialect : dialects) {
85 auto *iface = dyn_cast<ConvertToLLVMPatternInterface>(dialect);
88 LDBG() <<
"Convert to LLVM found dialect interface for "
89 << dialect->getNamespace();
90 iface->loadDependentDialects(context);
95 std::unique_ptr<DialectExtensionBase>
clone() const final {
96 return std::make_unique<LoadDependentDialectExtension>(*
this);
106 struct StaticConvertToLLVM :
public ConvertToLLVMPassInterface {
108 std::shared_ptr<const FrozenRewritePatternSet>
patterns;
110 std::shared_ptr<const ConversionTarget> target;
112 std::shared_ptr<const LLVMTypeConverter> typeConverter;
113 using ConvertToLLVMPassInterface::ConvertToLLVMPassInterface;
116 LogicalResult initialize() final {
117 auto target = std::make_shared<ConversionTarget>(*context);
118 auto typeConverter = std::make_shared<LLVMTypeConverter>(context);
120 target->addLegalDialect<LLVM::LLVMDialect>();
124 *target, *typeConverter, tempPatterns);
128 std::make_unique<FrozenRewritePatternSet>(std::move(tempPatterns));
129 this->target = target;
130 this->typeConverter = typeConverter;
137 config.allowPatternRollback = allowPatternRollback;
150 struct DynamicConvertToLLVM :
public ConvertToLLVMPassInterface {
153 std::shared_ptr<const SmallVector<ConvertToLLVMPatternInterface *>>
155 using ConvertToLLVMPassInterface::ConvertToLLVMPassInterface;
158 LogicalResult initialize() final {
160 std::make_shared<SmallVector<ConvertToLLVMPatternInterface *>>();
163 interfaces->push_back(iface);
166 this->interfaces = interfaces;
174 target.addLegalDialect<LLVM::LLVMDialect>();
190 config.allowPatternRollback = allowPatternRollback;
204 class ConvertToLLVMPass
205 :
public impl::ConvertToLLVMPassBase<ConvertToLLVMPass> {
206 std::shared_ptr<const ConvertToLLVMPassInterface>
impl;
209 using impl::ConvertToLLVMPassBase<ConvertToLLVMPass>::ConvertToLLVMPassBase;
211 ConvertToLLVMPassInterface::getDependentDialects(registry);
214 LogicalResult initialize(
MLIRContext *context)
final {
215 std::shared_ptr<ConvertToLLVMPassInterface>
impl;
218 impl = std::make_shared<DynamicConvertToLLVM>(context, filterDialects,
219 allowPatternRollback);
221 impl = std::make_shared<StaticConvertToLLVM>(context, filterDialects,
222 allowPatternRollback);
229 void runOnOperation() final {
230 if (
failed(
impl->transform(getOperation(), getAnalysisManager())))
231 return signalPassFailure();
241 ConvertToLLVMPassInterface::ConvertToLLVMPassInterface(
243 bool allowPatternRollback)
244 : context(context), filterDialects(filterDialects),
245 allowPatternRollback(allowPatternRollback) {}
247 void ConvertToLLVMPassInterface::getDependentDialects(
249 registry.
insert<LLVM::LLVMDialect>();
253 LogicalResult ConvertToLLVMPassInterface::visitInterfaces(
255 if (!filterDialects.empty()) {
259 for (StringRef dialectName : filterDialects) {
260 Dialect *dialect = context->getLoadedDialect(dialectName);
263 <<
"dialect not loaded: " << dialectName <<
"\n";
264 auto *iface = dyn_cast<ConvertToLLVMPatternInterface>(dialect);
267 <<
"dialect does not implement ConvertToLLVMPatternInterface: "
268 << dialectName <<
"\n";
274 for (
Dialect *dialect : context->getLoadedDialects()) {
277 auto *iface = dyn_cast<ConvertToLLVMPatternInterface>(dialect);
#define MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(CLASS_NAME)
This class represents an analysis manager for a particular operation instance.
This class describes a specific conversion target.
Base class for dialect interfaces providing translation to LLVM IR.
virtual void populateConvertToLLVMConversionPatterns(ConversionTarget &target, LLVMTypeConverter &typeConverter, RewritePatternSet &patterns) const =0
Hook for derived dialect interface to provide conversion patterns and mark dialect legal for the conv...
Stores data layout objects for each operation that specifies the data layout above and below the give...
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...
Conversion from types to the LLVM IR dialect.
MLIRContext is the top-level object for a collection of MLIR operations.
Operation is the basic unit of execution within MLIR.
Include the generated interface declarations.
const FrozenRewritePatternSet GreedyRewriteConfig config
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
const FrozenRewritePatternSet & patterns
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...
void populateOpConvertToLLVMConversionPatterns(Operation *op, ConversionTarget &target, LLVMTypeConverter &typeConverter, RewritePatternSet &patterns)
Helper function for populating LLVM conversion patterns.
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.
Dialect conversion configuration.