15 #include "llvm/Support/Debug.h"
19 #define DEBUG_TYPE "convert-to-emitc"
22 #define GEN_PASS_DEF_CONVERTTOEMITC
23 #include "mlir/Conversion/Passes.h.inc"
31 class ConvertToEmitCPassInterface {
35 virtual ~ConvertToEmitCPassInterface() =
default;
43 virtual LogicalResult initialize() = 0;
50 virtual LogicalResult transform(
Operation *op,
58 LogicalResult visitInterfaces(
79 LLVM_DEBUG(llvm::dbgs() <<
"Convert to EmitC extension load\n");
80 for (
Dialect *dialect : dialects) {
81 auto *iface = dyn_cast<ConvertToEmitCPatternInterface>(dialect);
84 LLVM_DEBUG(llvm::dbgs() <<
"Convert to EmitC found dialect interface for "
85 << dialect->getNamespace() <<
"\n");
90 std::unique_ptr<DialectExtensionBase>
clone() const final {
91 return std::make_unique<LoadDependentDialectExtension>(*
this);
101 struct StaticConvertToEmitC :
public ConvertToEmitCPassInterface {
103 std::shared_ptr<const FrozenRewritePatternSet>
patterns;
105 std::shared_ptr<const ConversionTarget> target;
107 std::shared_ptr<const TypeConverter> typeConverter;
108 using ConvertToEmitCPassInterface::ConvertToEmitCPassInterface;
111 LogicalResult initialize() final {
112 auto target = std::make_shared<ConversionTarget>(*context);
113 auto typeConverter = std::make_shared<TypeConverter>();
116 typeConverter->addConversion([](
Type type) -> std::optional<Type> {
123 target->addLegalDialect<emitc::EmitCDialect>();
127 *target, *typeConverter, tempPatterns);
131 std::make_unique<FrozenRewritePatternSet>(std::move(tempPatterns));
132 this->target = target;
133 this->typeConverter = typeConverter;
152 class ConvertToEmitC :
public impl::ConvertToEmitCBase<ConvertToEmitC> {
153 std::shared_ptr<const ConvertToEmitCPassInterface>
impl;
156 using impl::ConvertToEmitCBase<ConvertToEmitC>::ConvertToEmitCBase;
158 ConvertToEmitCPassInterface::getDependentDialects(registry);
161 LogicalResult initialize(
MLIRContext *context)
final {
162 std::shared_ptr<ConvertToEmitCPassInterface>
impl;
163 impl = std::make_shared<StaticConvertToEmitC>(context, filterDialects);
170 void runOnOperation() final {
171 if (
failed(
impl->transform(getOperation(), getAnalysisManager())))
172 return signalPassFailure();
182 ConvertToEmitCPassInterface::ConvertToEmitCPassInterface(
184 : context(context), filterDialects(filterDialects) {}
186 void ConvertToEmitCPassInterface::getDependentDialects(
188 registry.
insert<emitc::EmitCDialect>();
192 LogicalResult ConvertToEmitCPassInterface::visitInterfaces(
194 if (!filterDialects.empty()) {
198 for (StringRef dialectName : filterDialects) {
199 Dialect *dialect = context->getLoadedDialect(dialectName);
202 <<
"dialect not loaded: " << dialectName <<
"\n";
203 auto *iface = dyn_cast<ConvertToEmitCPatternInterface>(dialect);
206 <<
"dialect does not implement ConvertToEmitCPatternInterface: "
207 << dialectName <<
"\n";
213 for (
Dialect *dialect : context->getLoadedDialects()) {
214 auto *iface = dyn_cast<ConvertToEmitCPatternInterface>(dialect);
#define MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(CLASS_NAME)
This class represents an analysis manager for a particular operation instance.
virtual void populateConvertToEmitCConversionPatterns(ConversionTarget &target, TypeConverter &typeConverter, RewritePatternSet &patterns) const =0
Hook for derived dialect interface to provide conversion patterns and mark dialect legal for the conv...
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.
Operation is the basic unit of execution within MLIR.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
bool isSupportedEmitCType(mlir::Type type)
Determines whether type is valid in EmitC.
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
const FrozenRewritePatternSet & patterns
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.