21 #define DEBUG_TYPE "convert-to-llvm"
24 #define GEN_PASS_DEF_CONVERTTOLLVMPASS
25 #include "mlir/Conversion/Passes.h.inc"
33 class ConvertToLLVMPassInterface {
37 virtual ~ConvertToLLVMPassInterface() =
default;
45 virtual LogicalResult initialize() = 0;
52 virtual LogicalResult transform(
Operation *op,
60 LogicalResult visitInterfaces(
80 LLVM_DEBUG(llvm::dbgs() <<
"Convert to LLVM extension load\n");
81 for (
Dialect *dialect : dialects) {
82 auto *iface = dyn_cast<ConvertToLLVMPatternInterface>(dialect);
85 LLVM_DEBUG(llvm::dbgs() <<
"Convert to LLVM found dialect interface for "
86 << dialect->getNamespace() <<
"\n");
87 iface->loadDependentDialects(context);
92 std::unique_ptr<DialectExtensionBase>
clone() const final {
93 return std::make_unique<LoadDependentDialectExtension>(*
this);
103 struct StaticConvertToLLVM :
public ConvertToLLVMPassInterface {
105 std::shared_ptr<const FrozenRewritePatternSet>
patterns;
107 std::shared_ptr<const ConversionTarget> target;
109 std::shared_ptr<const LLVMTypeConverter> typeConverter;
110 using ConvertToLLVMPassInterface::ConvertToLLVMPassInterface;
113 LogicalResult initialize() final {
114 auto target = std::make_shared<ConversionTarget>(*context);
115 auto typeConverter = std::make_shared<LLVMTypeConverter>(context);
117 target->addLegalDialect<LLVM::LLVMDialect>();
121 *target, *typeConverter, tempPatterns);
125 std::make_unique<FrozenRewritePatternSet>(std::move(tempPatterns));
126 this->target = target;
127 this->typeConverter = typeConverter;
145 struct DynamicConvertToLLVM :
public ConvertToLLVMPassInterface {
148 std::shared_ptr<const SmallVector<ConvertToLLVMPatternInterface *>>
150 using ConvertToLLVMPassInterface::ConvertToLLVMPassInterface;
153 LogicalResult initialize() final {
155 std::make_shared<SmallVector<ConvertToLLVMPatternInterface *>>();
158 interfaces->push_back(iface);
161 this->interfaces = interfaces;
169 target.addLegalDialect<LLVM::LLVMDialect>();
197 class ConvertToLLVMPass
198 :
public impl::ConvertToLLVMPassBase<ConvertToLLVMPass> {
199 std::shared_ptr<const ConvertToLLVMPassInterface>
impl;
202 using impl::ConvertToLLVMPassBase<ConvertToLLVMPass>::ConvertToLLVMPassBase;
204 ConvertToLLVMPassInterface::getDependentDialects(registry);
207 LogicalResult initialize(
MLIRContext *context)
final {
208 std::shared_ptr<ConvertToLLVMPassInterface>
impl;
211 impl = std::make_shared<DynamicConvertToLLVM>(context, filterDialects);
213 impl = std::make_shared<StaticConvertToLLVM>(context, filterDialects);
214 if (failed(
impl->initialize()))
220 void runOnOperation() final {
221 if (failed(
impl->transform(getOperation(), getAnalysisManager())))
222 return signalPassFailure();
232 ConvertToLLVMPassInterface::ConvertToLLVMPassInterface(
234 : context(context), filterDialects(filterDialects) {}
236 void ConvertToLLVMPassInterface::getDependentDialects(
238 registry.
insert<LLVM::LLVMDialect>();
242 LogicalResult ConvertToLLVMPassInterface::visitInterfaces(
244 if (!filterDialects.empty()) {
248 for (StringRef dialectName : filterDialects) {
249 Dialect *dialect = context->getLoadedDialect(dialectName);
252 <<
"dialect not loaded: " << dialectName <<
"\n";
253 auto *iface = dyn_cast<ConvertToLLVMPatternInterface>(dialect);
256 <<
"dialect does not implement ConvertToLLVMPatternInterface: "
257 << dialectName <<
"\n";
263 for (
Dialect *dialect : context->getLoadedDialects()) {
266 auto *iface = dyn_cast<ConvertToLLVMPatternInterface>(dialect);
285 return std::make_unique<ConvertToLLVMPass>();
#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.
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...
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.