31 void populateConvertToEmitCConversionPatterns(
42 dialect->addInterfaces<MemRefToEmitCDialectInterface>();
55 matchAndRewrite(memref::AllocaOp op, OpAdaptor operands,
58 if (!op.getType().hasStaticShape()) {
60 op.getLoc(),
"cannot transform alloca with dynamic shape");
63 if (op.getAlignment().value_or(1) > 1) {
67 op.getLoc(),
"cannot transform alloca with alignment requirement");
70 auto resultTy = getTypeConverter()->convertType(op.getType());
84 matchAndRewrite(memref::GlobalOp op, OpAdaptor operands,
87 if (!op.getType().hasStaticShape()) {
89 op.getLoc(),
"cannot transform global with dynamic shape");
92 if (op.getAlignment().value_or(1) > 1) {
95 op.getLoc(),
"global variable with alignment requirement is "
96 "currently not supported");
98 auto resultTy = getTypeConverter()->convertType(op.getType());
101 "cannot convert result type");
109 "only public and private visibility is currently supported");
114 bool externSpecifier = !staticSpecifier;
116 Attribute initialValue = operands.getInitialValueAttr();
117 if (isa_and_present<UnitAttr>(initialValue))
121 op, operands.getSymName(), resultTy, initialValue, externSpecifier,
122 staticSpecifier, operands.getConstant());
127 struct ConvertGetGlobal final
132 matchAndRewrite(memref::GetGlobalOp op, OpAdaptor operands,
135 auto resultTy = getTypeConverter()->convertType(op.getType());
138 "cannot convert result type");
141 operands.getNameAttr());
150 matchAndRewrite(memref::LoadOp op, OpAdaptor operands,
153 auto resultTy = getTypeConverter()->convertType(op.getType());
159 dyn_cast<TypedValue<emitc::ArrayType>>(operands.getMemref());
164 auto subscript = rewriter.
create<emitc::SubscriptOp>(
165 op.getLoc(), arrayValue, operands.getIndices());
176 matchAndRewrite(memref::StoreOp op, OpAdaptor operands,
179 dyn_cast<TypedValue<emitc::ArrayType>>(operands.getMemref());
184 auto subscript = rewriter.
create<emitc::SubscriptOp>(
185 op.getLoc(), arrayValue, operands.getIndices());
187 operands.getValue());
195 [&](MemRefType memRefType) -> std::optional<Type> {
196 if (!memRefType.hasStaticShape() ||
197 !memRefType.getLayout().isIdentity() || memRefType.getRank() == 0 ||
198 llvm::any_of(memRefType.getShape(),
199 [](int64_t dim) { return dim == 0; })) {
202 Type convertedElementType =
203 typeConverter.
convertType(memRefType.getElementType());
204 if (!convertedElementType)
207 convertedElementType);
210 auto materializeAsUnrealizedCast = [](
OpBuilder &builder,
Type resultType,
213 if (inputs.size() != 1)
216 return builder.
create<UnrealizedConversionCastOp>(loc, resultType, inputs)
220 typeConverter.addSourceMaterialization(materializeAsUnrealizedCast);
221 typeConverter.addTargetMaterialization(materializeAsUnrealizedCast);
static MLIRContext * getContext(OpFoldResult val)
Attributes are known-constant values of operations.
This class implements a pattern rewriter for use with ConversionPatterns.
This class describes a specific conversion target.
ConvertToEmitCPatternInterface(Dialect *dialect)
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
This class helps build Operations.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
OpConversionPattern is a wrapper around ConversionPattern that allows for matching and rewriting agai...
OpConversionPattern(MLIRContext *context, PatternBenefit benefit=1)
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
static Visibility getSymbolVisibility(Operation *symbol)
Returns the visibility of the given symbol operation.
Visibility
An enumeration detailing the different visibility types that a symbol may have.
@ Public
The symbol is public and may be referenced anywhere internal or external to the visible references in...
@ Private
The symbol is private and may only be referenced by SymbolRefAttrs local to the operations within the...
void addConversion(FnT &&callback)
Register a conversion function.
LogicalResult convertType(Type t, SmallVectorImpl< Type > &results) const
Convert the given type.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Include the generated interface declarations.
void populateMemRefToEmitCTypeConversion(TypeConverter &typeConverter)
const FrozenRewritePatternSet & patterns
void populateMemRefToEmitCConversionPatterns(RewritePatternSet &patterns, const TypeConverter &converter)
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
void registerConvertMemRefToEmitCInterface(DialectRegistry ®istry)