| 1 | //===- MemRefToEmitC.cpp - MemRef to EmitC conversion ---------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements patterns to convert memref ops into emitc ops. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/Conversion/MemRefToEmitC/MemRefToEmitC.h" |
| 14 | |
| 15 | #include "mlir/Conversion/ConvertToEmitC/ToEmitCInterface.h" |
| 16 | #include "mlir/Dialect/EmitC/IR/EmitC.h" |
| 17 | #include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 18 | #include "mlir/IR/Builders.h" |
| 19 | #include "mlir/IR/BuiltinTypes.h" |
| 20 | #include "mlir/IR/Diagnostics.h" |
| 21 | #include "mlir/IR/PatternMatch.h" |
| 22 | #include "mlir/IR/TypeRange.h" |
| 23 | #include "mlir/IR/Value.h" |
| 24 | #include "mlir/Transforms/DialectConversion.h" |
| 25 | #include "llvm/ADT/STLExtras.h" |
| 26 | #include <cstdint> |
| 27 | #include <numeric> |
| 28 | |
| 29 | using namespace mlir; |
| 30 | |
| 31 | static bool isMemRefTypeLegalForEmitC(MemRefType memRefType) { |
| 32 | return memRefType.hasStaticShape() && memRefType.getLayout().isIdentity() && |
| 33 | !llvm::is_contained(Range: memRefType.getShape(), Element: 0); |
| 34 | } |
| 35 | |
| 36 | namespace { |
| 37 | /// Implement the interface to convert MemRef to EmitC. |
| 38 | struct MemRefToEmitCDialectInterface : public ConvertToEmitCPatternInterface { |
| 39 | MemRefToEmitCDialectInterface(Dialect *dialect) |
| 40 | : ConvertToEmitCPatternInterface(dialect) {} |
| 41 | |
| 42 | /// Hook for derived dialect interface to provide conversion patterns |
| 43 | /// and mark dialect legal for the conversion target. |
| 44 | void populateConvertToEmitCConversionPatterns( |
| 45 | ConversionTarget &target, TypeConverter &typeConverter, |
| 46 | RewritePatternSet &patterns, std::optional<bool> lowerToCpp) const final { |
| 47 | populateMemRefToEmitCConversionPatterns(patterns, converter: typeConverter); |
| 48 | } |
| 49 | }; |
| 50 | } // namespace |
| 51 | |
| 52 | void mlir::registerConvertMemRefToEmitCInterface(DialectRegistry ®istry) { |
| 53 | registry.addExtension(extensionFn: +[](MLIRContext *ctx, memref::MemRefDialect *dialect) { |
| 54 | dialect->addInterfaces<MemRefToEmitCDialectInterface>(); |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | // Conversion Patterns |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | |
| 62 | namespace { |
| 63 | struct ConvertAlloca final : public OpConversionPattern<memref::AllocaOp> { |
| 64 | using Base::Base; |
| 65 | |
| 66 | LogicalResult |
| 67 | matchAndRewrite(memref::AllocaOp op, OpAdaptor operands, |
| 68 | ConversionPatternRewriter &rewriter) const override { |
| 69 | |
| 70 | if (!op.getType().hasStaticShape()) { |
| 71 | return rewriter.notifyMatchFailure( |
| 72 | arg: op.getLoc(), msg: "cannot transform alloca with dynamic shape" ); |
| 73 | } |
| 74 | |
| 75 | if (op.getAlignment().value_or(u: 1) > 1) { |
| 76 | // TODO: Allow alignment if it is not more than the natural alignment |
| 77 | // of the C array. |
| 78 | return rewriter.notifyMatchFailure( |
| 79 | arg: op.getLoc(), msg: "cannot transform alloca with alignment requirement" ); |
| 80 | } |
| 81 | |
| 82 | Type resultTy = getTypeConverter()->convertType(t: op.getType()); |
| 83 | if (!resultTy) |
| 84 | return rewriter.notifyMatchFailure(arg: op.getLoc(), msg: "cannot convert type" ); |
| 85 | |
| 86 | auto noInit = emitc::OpaqueAttr::get(context: getContext(), value: "" ); |
| 87 | // Rank-0 path |
| 88 | if (op.getType().getRank() == 0) { |
| 89 | auto pointerTy = dyn_cast<emitc::PointerType>(Val&: resultTy); |
| 90 | assert(pointerTy && "expected rank-0 MemRef to convert to pointer" ); |
| 91 | Type elemTy = pointerTy.getPointee(); |
| 92 | auto var = emitc::VariableOp::create( |
| 93 | builder&: rewriter, location: op.getLoc(), resultType0: emitc::LValueType::get(valueType: elemTy), value: noInit); |
| 94 | |
| 95 | auto ptr = emitc::AddressOfOp::create(builder&: rewriter, location: op.getLoc(), result: resultTy, |
| 96 | reference: var.getResult()); |
| 97 | |
| 98 | rewriter.replaceOp(op, newValues: ptr.getResult()); |
| 99 | return success(); |
| 100 | } |
| 101 | // Rank > 0 path |
| 102 | rewriter.replaceOpWithNewOp<emitc::VariableOp>(op, args&: resultTy, args&: noInit); |
| 103 | return success(); |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | static Value calculateMemrefTotalSizeBytes(Location loc, MemRefType memrefType, |
| 108 | OpBuilder &builder, |
| 109 | Type convertedElementType) { |
| 110 | assert(isMemRefTypeLegalForEmitC(memrefType) && |
| 111 | "incompatible memref type for EmitC conversion" ); |
| 112 | |
| 113 | emitc::CallOpaqueOp elementSize = emitc::CallOpaqueOp::create( |
| 114 | builder, location: loc, resultTypes: emitc::SizeTType::get(ctx: builder.getContext()), |
| 115 | callee: builder.getStringAttr(bytes: "sizeof" ), arg_operands: ValueRange{}, |
| 116 | args: ArrayAttr::get(context: builder.getContext(), |
| 117 | value: {TypeAttr::get(type: convertedElementType)})); |
| 118 | |
| 119 | IndexType indexType = builder.getIndexType(); |
| 120 | int64_t numElements = llvm::product_of(Range: memrefType.getShape()); |
| 121 | emitc::ConstantOp numElementsValue = emitc::ConstantOp::create( |
| 122 | builder, location: loc, resultType0: indexType, value: builder.getIndexAttr(value: numElements)); |
| 123 | |
| 124 | Type sizeTType = emitc::SizeTType::get(ctx: builder.getContext()); |
| 125 | emitc::MulOp totalSizeBytes = emitc::MulOp::create( |
| 126 | builder, location: loc, resultType0: sizeTType, odsArg_0: elementSize.getResult(i: 0), odsArg_1: numElementsValue); |
| 127 | |
| 128 | return totalSizeBytes.getResult(); |
| 129 | } |
| 130 | |
| 131 | static emitc::AddressOfOp |
| 132 | createPointerFromEmitcArray(Location loc, OpBuilder &builder, |
| 133 | TypedValue<emitc::ArrayType> arrayValue) { |
| 134 | |
| 135 | emitc::ConstantOp zeroIndex = emitc::ConstantOp::create( |
| 136 | builder, location: loc, resultType0: builder.getIndexType(), value: builder.getIndexAttr(value: 0)); |
| 137 | |
| 138 | emitc::ArrayType arrayType = arrayValue.getType(); |
| 139 | llvm::SmallVector<mlir::Value> indices(arrayType.getRank(), zeroIndex); |
| 140 | emitc::SubscriptOp subPtr = |
| 141 | emitc::SubscriptOp::create(builder, location: loc, array: arrayValue, indices: ValueRange(indices)); |
| 142 | emitc::AddressOfOp ptr = emitc::AddressOfOp::create( |
| 143 | builder, location: loc, result: emitc::PointerType::get(pointee: arrayType.getElementType()), |
| 144 | reference: subPtr); |
| 145 | |
| 146 | return ptr; |
| 147 | } |
| 148 | |
| 149 | static Value getMemRefPointer(Value v) { |
| 150 | if (isa<emitc::PointerType>(Val: v.getType())) |
| 151 | return v; |
| 152 | |
| 153 | // If `v` is defined through an unrealized cast and the source of that cast |
| 154 | // is `emitc.ptr`, return the pointer. |
| 155 | if (auto cast = v.getDefiningOp<UnrealizedConversionCastOp>()) |
| 156 | if (cast.getNumOperands() == 1 && |
| 157 | isa<emitc::PointerType>(Val: cast.getOperand(i: 0).getType())) |
| 158 | return cast.getOperand(i: 0); |
| 159 | return Value(); |
| 160 | } |
| 161 | |
| 162 | static Value computeRowMajorLinearIndex(ImplicitLocOpBuilder &builder, |
| 163 | MemRefType memrefType, |
| 164 | ValueRange indices) { |
| 165 | ArrayRef<int64_t> shape = memrefType.getShape(); |
| 166 | |
| 167 | Type idxType = |
| 168 | indices.empty() ? builder.getIndexType() : indices[0].getType(); |
| 169 | |
| 170 | Value linearIndex = |
| 171 | indices.empty() |
| 172 | ? emitc::ConstantOp::create(builder, resultType0: idxType, value: builder.getIndexAttr(value: 0)) |
| 173 | : indices[0]; |
| 174 | |
| 175 | if (indices.empty()) |
| 176 | return linearIndex; |
| 177 | |
| 178 | for (auto [dim, idx] : llvm::zip(t: shape.drop_front(), u: indices.drop_front())) { |
| 179 | Value dimSize = |
| 180 | emitc::ConstantOp::create(builder, resultType0: idxType, value: builder.getIndexAttr(value: dim)); |
| 181 | linearIndex = emitc::MulOp::create(builder, resultType0: idxType, odsArg_0: linearIndex, odsArg_1: dimSize); |
| 182 | linearIndex = emitc::AddOp::create(builder, resultType0: idxType, lhs: linearIndex, rhs: idx); |
| 183 | } |
| 184 | return linearIndex; |
| 185 | } |
| 186 | |
| 187 | struct ConvertAlloc final : public OpConversionPattern<memref::AllocOp> { |
| 188 | using Base::Base; |
| 189 | LogicalResult |
| 190 | matchAndRewrite(memref::AllocOp allocOp, OpAdaptor operands, |
| 191 | ConversionPatternRewriter &rewriter) const override { |
| 192 | Location loc = allocOp.getLoc(); |
| 193 | MemRefType memrefType = allocOp.getType(); |
| 194 | if (!isMemRefTypeLegalForEmitC(memRefType: memrefType)) { |
| 195 | return rewriter.notifyMatchFailure( |
| 196 | arg&: loc, msg: "incompatible memref type for EmitC conversion" ); |
| 197 | } |
| 198 | |
| 199 | Type sizeTType = emitc::SizeTType::get(ctx: rewriter.getContext()); |
| 200 | Type elementType = |
| 201 | getTypeConverter()->convertType(t: memrefType.getElementType()); |
| 202 | if (!elementType) { |
| 203 | return rewriter.notifyMatchFailure( |
| 204 | arg&: loc, msg: "failed to convert memref element type" ); |
| 205 | } |
| 206 | IndexType indexType = rewriter.getIndexType(); |
| 207 | Value totalSizeBytes = |
| 208 | calculateMemrefTotalSizeBytes(loc, memrefType, builder&: rewriter, convertedElementType: elementType); |
| 209 | |
| 210 | emitc::CallOpaqueOp allocCall; |
| 211 | StringAttr allocFunctionName; |
| 212 | Value alignmentValue; |
| 213 | SmallVector<Value, 2> argsVec; |
| 214 | if (allocOp.getAlignment()) { |
| 215 | allocFunctionName = rewriter.getStringAttr(bytes: alignedAllocFunctionName); |
| 216 | alignmentValue = emitc::ConstantOp::create( |
| 217 | builder&: rewriter, location: loc, resultType0: sizeTType, |
| 218 | value: rewriter.getIntegerAttr(type: indexType, |
| 219 | value: allocOp.getAlignment().value_or(u: 0))); |
| 220 | argsVec.push_back(Elt: alignmentValue); |
| 221 | } else { |
| 222 | allocFunctionName = rewriter.getStringAttr(bytes: mallocFunctionName); |
| 223 | } |
| 224 | |
| 225 | argsVec.push_back(Elt: totalSizeBytes); |
| 226 | ValueRange args(argsVec); |
| 227 | |
| 228 | allocCall = emitc::CallOpaqueOp::create( |
| 229 | builder&: rewriter, location: loc, |
| 230 | resultTypes: emitc::PointerType::get( |
| 231 | pointee: emitc::OpaqueType::get(context: rewriter.getContext(), value: "void" )), |
| 232 | callee: allocFunctionName, arg_operands: args); |
| 233 | |
| 234 | emitc::PointerType targetPointerType = emitc::PointerType::get(pointee: elementType); |
| 235 | emitc::CastOp castOp = emitc::CastOp::create( |
| 236 | builder&: rewriter, location: loc, dest: targetPointerType, source: allocCall.getResult(i: 0)); |
| 237 | |
| 238 | rewriter.replaceOp(op: allocOp, newOp: castOp); |
| 239 | return success(); |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | struct ConvertDealloc final : public OpConversionPattern<memref::DeallocOp> { |
| 244 | using Base::Base; |
| 245 | |
| 246 | LogicalResult |
| 247 | matchAndRewrite(memref::DeallocOp deallocOp, OpAdaptor operands, |
| 248 | ConversionPatternRewriter &rewriter) const override { |
| 249 | Location loc = deallocOp.getLoc(); |
| 250 | // `free` can only be emitted when the dealloc operand is recoverable as an |
| 251 | // `emitc.ptr<T>`. |
| 252 | Value ptr = getMemRefPointer(v: operands.getMemref()); |
| 253 | if (!ptr) { |
| 254 | return rewriter.notifyMatchFailure( |
| 255 | arg&: loc, msg: "expected pointer-backed memref for EmitC deallocation" ); |
| 256 | } |
| 257 | |
| 258 | // The allocation APIs used by MemRefToEmitC return `void *`, and `free` |
| 259 | // expects that same pointer type. Deallocation therefore only needs the |
| 260 | // recovered base pointer cast back to `void *` before calling `free`. |
| 261 | Type opaqueVoidPtrType = emitc::PointerType::get( |
| 262 | pointee: emitc::OpaqueType::get(context: rewriter.getContext(), value: "void" )); |
| 263 | Value freeArg = |
| 264 | emitc::CastOp::create(builder&: rewriter, location: loc, dest: opaqueVoidPtrType, source: ptr); |
| 265 | emitc::CallOpaqueOp freeCall = emitc::CallOpaqueOp::create( |
| 266 | builder&: rewriter, location: loc, resultTypes: TypeRange{}, callee: rewriter.getStringAttr(bytes: freeFunctionName), |
| 267 | arg_operands: ValueRange{freeArg}); |
| 268 | rewriter.replaceOp(op: deallocOp, newValues: freeCall.getResults()); |
| 269 | return success(); |
| 270 | } |
| 271 | }; |
| 272 | |
| 273 | struct ConvertCopy final : public OpConversionPattern<memref::CopyOp> { |
| 274 | using Base::Base; |
| 275 | |
| 276 | LogicalResult |
| 277 | matchAndRewrite(memref::CopyOp copyOp, OpAdaptor operands, |
| 278 | ConversionPatternRewriter &rewriter) const override { |
| 279 | Location loc = copyOp.getLoc(); |
| 280 | MemRefType srcMemrefType = cast<MemRefType>(Val: copyOp.getSource().getType()); |
| 281 | MemRefType targetMemrefType = |
| 282 | cast<MemRefType>(Val: copyOp.getTarget().getType()); |
| 283 | |
| 284 | if (!isMemRefTypeLegalForEmitC(memRefType: srcMemrefType)) |
| 285 | return rewriter.notifyMatchFailure( |
| 286 | arg&: loc, msg: "incompatible source memref type for EmitC conversion" ); |
| 287 | |
| 288 | if (!isMemRefTypeLegalForEmitC(memRefType: targetMemrefType)) |
| 289 | return rewriter.notifyMatchFailure( |
| 290 | arg&: loc, msg: "incompatible target memref type for EmitC conversion" ); |
| 291 | |
| 292 | if (srcMemrefType.getRank() == 0) { |
| 293 | assert(targetMemrefType.getRank() == 0 && |
| 294 | "target must have same rank as source" ); |
| 295 | Type elementType = |
| 296 | getTypeConverter()->convertType(t: srcMemrefType.getElementType()); |
| 297 | if (!elementType) |
| 298 | return rewriter.notifyMatchFailure(arg&: loc, msg: "cannot convert element type" ); |
| 299 | |
| 300 | Value srcPtr = getMemRefPointer(v: operands.getSource()); |
| 301 | Value targetPtr = getMemRefPointer(v: operands.getTarget()); |
| 302 | if (!srcPtr || !targetPtr) |
| 303 | return rewriter.notifyMatchFailure(arg&: loc, msg: "expected pointer operands" ); |
| 304 | |
| 305 | Value zeroIndex = emitc::ConstantOp::create( |
| 306 | builder&: rewriter, location: loc, resultType0: rewriter.getIndexType(), value: rewriter.getIndexAttr(value: 0)); |
| 307 | Value srcLValue = emitc::SubscriptOp::create( |
| 308 | builder&: rewriter, location: loc, pointer: cast<TypedValue<emitc::PointerType>>(Val&: srcPtr), |
| 309 | index: zeroIndex); |
| 310 | Value value = |
| 311 | emitc::LoadOp::create(builder&: rewriter, location: loc, result: elementType, operand: srcLValue); |
| 312 | |
| 313 | Value targetLValue = emitc::SubscriptOp::create( |
| 314 | builder&: rewriter, location: loc, pointer: cast<TypedValue<emitc::PointerType>>(Val&: targetPtr), |
| 315 | index: zeroIndex); |
| 316 | rewriter.replaceOpWithNewOp<emitc::AssignOp>(op: copyOp, args&: targetLValue, args&: value); |
| 317 | return success(); |
| 318 | } |
| 319 | |
| 320 | auto srcArrayValue = |
| 321 | cast<TypedValue<emitc::ArrayType>>(Val: operands.getSource()); |
| 322 | emitc::AddressOfOp srcPtr = |
| 323 | createPointerFromEmitcArray(loc, builder&: rewriter, arrayValue: srcArrayValue); |
| 324 | |
| 325 | auto targetArrayValue = |
| 326 | cast<TypedValue<emitc::ArrayType>>(Val: operands.getTarget()); |
| 327 | emitc::AddressOfOp targetPtr = |
| 328 | createPointerFromEmitcArray(loc, builder&: rewriter, arrayValue: targetArrayValue); |
| 329 | |
| 330 | Type convertedElementType = |
| 331 | getTypeConverter()->convertType(t: srcMemrefType.getElementType()); |
| 332 | if (!convertedElementType) { |
| 333 | return rewriter.notifyMatchFailure( |
| 334 | arg&: loc, msg: "failed to convert memref element type" ); |
| 335 | } |
| 336 | Value totalSizeInBytes = calculateMemrefTotalSizeBytes( |
| 337 | loc, memrefType: srcMemrefType, builder&: rewriter, convertedElementType); |
| 338 | emitc::CallOpaqueOp memCpyCall = |
| 339 | emitc::CallOpaqueOp::create(builder&: rewriter, location: loc, resultTypes: TypeRange{}, callee: "memcpy" , |
| 340 | arg_operands: ValueRange{ |
| 341 | targetPtr.getResult(), |
| 342 | srcPtr.getResult(), |
| 343 | totalSizeInBytes, |
| 344 | }); |
| 345 | |
| 346 | rewriter.replaceOp(op: copyOp, newValues: memCpyCall.getResults()); |
| 347 | |
| 348 | return success(); |
| 349 | } |
| 350 | }; |
| 351 | |
| 352 | struct ConvertGlobal final : public OpConversionPattern<memref::GlobalOp> { |
| 353 | using Base::Base; |
| 354 | |
| 355 | LogicalResult |
| 356 | matchAndRewrite(memref::GlobalOp op, OpAdaptor operands, |
| 357 | ConversionPatternRewriter &rewriter) const override { |
| 358 | MemRefType opTy = op.getType(); |
| 359 | if (!op.getType().hasStaticShape()) { |
| 360 | return rewriter.notifyMatchFailure( |
| 361 | arg: op.getLoc(), msg: "cannot transform global with dynamic shape" ); |
| 362 | } |
| 363 | |
| 364 | if (op.getAlignment().value_or(u: 1) > 1) { |
| 365 | // TODO: Extend GlobalOp to specify alignment via the `alignas` specifier. |
| 366 | return rewriter.notifyMatchFailure( |
| 367 | arg: op.getLoc(), msg: "global variable with alignment requirement is " |
| 368 | "currently not supported" ); |
| 369 | } |
| 370 | |
| 371 | Type resultTy = getTypeConverter()->convertType(t: opTy); |
| 372 | |
| 373 | if (!resultTy) { |
| 374 | return rewriter.notifyMatchFailure(arg: op.getLoc(), |
| 375 | msg: "cannot convert result type" ); |
| 376 | } |
| 377 | |
| 378 | SymbolTable::Visibility visibility = SymbolTable::getSymbolVisibility(symbol: op); |
| 379 | if (visibility != SymbolTable::Visibility::Public && |
| 380 | visibility != SymbolTable::Visibility::Private) { |
| 381 | return rewriter.notifyMatchFailure( |
| 382 | arg: op.getLoc(), |
| 383 | msg: "only public and private visibility is currently supported" ); |
| 384 | } |
| 385 | // We are explicit in specifying the linkage because the default linkage |
| 386 | // for constants is different in C and C++. |
| 387 | bool staticSpecifier = visibility == SymbolTable::Visibility::Private; |
| 388 | bool externSpecifier = !staticSpecifier; |
| 389 | |
| 390 | Attribute initialValue = operands.getInitialValueAttr(); |
| 391 | if (opTy.getRank() == 0) { |
| 392 | auto pointerTy = dyn_cast<emitc::PointerType>(Val&: resultTy); |
| 393 | assert(pointerTy && "expected rank-0 MemRef to convert to pointer" ); |
| 394 | resultTy = pointerTy.getPointee(); |
| 395 | // special case for `variable : memref<i32> = dense<-1>` |
| 396 | if (std::optional<Attribute> initValueAttr = op.getInitialValue()) { |
| 397 | if (auto elementsAttr = llvm::dyn_cast<ElementsAttr>(Val&: *initValueAttr)) { |
| 398 | initialValue = elementsAttr.getSplatValue<Attribute>(); |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | if (isa_and_present<UnitAttr>(Val: initialValue)) |
| 403 | initialValue = {}; |
| 404 | |
| 405 | rewriter.replaceOpWithNewOp<emitc::GlobalOp>( |
| 406 | op, args: operands.getSymName(), /*sym_visibility=*/args: nullptr, args&: resultTy, |
| 407 | args&: initialValue, args&: externSpecifier, args&: staticSpecifier, args: operands.getConstant()); |
| 408 | return success(); |
| 409 | } |
| 410 | }; |
| 411 | |
| 412 | struct ConvertGetGlobal final |
| 413 | : public OpConversionPattern<memref::GetGlobalOp> { |
| 414 | using Base::Base; |
| 415 | |
| 416 | LogicalResult |
| 417 | matchAndRewrite(memref::GetGlobalOp op, OpAdaptor operands, |
| 418 | ConversionPatternRewriter &rewriter) const override { |
| 419 | |
| 420 | MemRefType opTy = op.getType(); |
| 421 | Type resultTy = getTypeConverter()->convertType(t: opTy); |
| 422 | |
| 423 | if (!resultTy) { |
| 424 | return rewriter.notifyMatchFailure(arg: op.getLoc(), |
| 425 | msg: "cannot convert result type" ); |
| 426 | } |
| 427 | |
| 428 | if (opTy.getRank() == 0) { |
| 429 | auto pointerTy = dyn_cast<emitc::PointerType>(Val&: resultTy); |
| 430 | assert(pointerTy && "expected rank-0 MemRef to convert to pointer" ); |
| 431 | Type elemTy = pointerTy.getPointee(); |
| 432 | emitc::LValueType lvalueType = emitc::LValueType::get(valueType: elemTy); |
| 433 | emitc::GetGlobalOp globalLValue = emitc::GetGlobalOp::create( |
| 434 | builder&: rewriter, location: op.getLoc(), result: lvalueType, name: operands.getNameAttr()); |
| 435 | rewriter.replaceOpWithNewOp<emitc::AddressOfOp>(op, args&: resultTy, |
| 436 | args&: globalLValue); |
| 437 | return success(); |
| 438 | } |
| 439 | rewriter.replaceOpWithNewOp<emitc::GetGlobalOp>(op, args&: resultTy, |
| 440 | args: operands.getNameAttr()); |
| 441 | return success(); |
| 442 | } |
| 443 | }; |
| 444 | |
| 445 | struct ConvertLoad final : public OpConversionPattern<memref::LoadOp> { |
| 446 | using Base::Base; |
| 447 | |
| 448 | LogicalResult |
| 449 | matchAndRewrite(memref::LoadOp op, OpAdaptor operands, |
| 450 | ConversionPatternRewriter &rewriter) const override { |
| 451 | Location loc = op.getLoc(); |
| 452 | auto resultTy = getTypeConverter()->convertType(t: op.getType()); |
| 453 | if (!resultTy) { |
| 454 | return rewriter.notifyMatchFailure(arg&: loc, msg: "cannot convert type" ); |
| 455 | } |
| 456 | |
| 457 | auto arrayValue = |
| 458 | dyn_cast<TypedValue<emitc::ArrayType>>(Val: operands.getMemref()); |
| 459 | Value ptr = getMemRefPointer(v: operands.getMemref()); |
| 460 | if (!ptr && arrayValue) { |
| 461 | auto subscript = emitc::SubscriptOp::create(builder&: rewriter, location: loc, array: arrayValue, |
| 462 | indices: operands.getIndices()); |
| 463 | |
| 464 | rewriter.replaceOpWithNewOp<emitc::LoadOp>(op, args&: resultTy, args&: subscript); |
| 465 | return success(); |
| 466 | } |
| 467 | |
| 468 | if (!ptr) |
| 469 | return rewriter.notifyMatchFailure(arg&: loc, msg: "expected array or pointer type" ); |
| 470 | MemRefType opMemrefType = cast<MemRefType>(Val: op.getMemref().getType()); |
| 471 | ValueRange indices = operands.getIndices(); |
| 472 | |
| 473 | ImplicitLocOpBuilder b(loc, rewriter); |
| 474 | Value linearIndex = computeRowMajorLinearIndex(builder&: b, memrefType: opMemrefType, indices); |
| 475 | auto typedPtr = cast<TypedValue<emitc::PointerType>>(Val&: ptr); |
| 476 | auto subscript = |
| 477 | emitc::SubscriptOp::create(builder&: rewriter, location: loc, pointer: typedPtr, index: linearIndex); |
| 478 | |
| 479 | rewriter.replaceOpWithNewOp<emitc::LoadOp>(op, args&: resultTy, args&: subscript); |
| 480 | return success(); |
| 481 | } |
| 482 | }; |
| 483 | |
| 484 | struct ConvertStore final : public OpConversionPattern<memref::StoreOp> { |
| 485 | using Base::Base; |
| 486 | |
| 487 | LogicalResult |
| 488 | matchAndRewrite(memref::StoreOp op, OpAdaptor operands, |
| 489 | ConversionPatternRewriter &rewriter) const override { |
| 490 | Location loc = op.getLoc(); |
| 491 | auto arrayValue = |
| 492 | dyn_cast<TypedValue<emitc::ArrayType>>(Val: operands.getMemref()); |
| 493 | Value ptr = getMemRefPointer(v: operands.getMemref()); |
| 494 | if (!ptr && arrayValue) { |
| 495 | auto subscript = emitc::SubscriptOp::create(builder&: rewriter, location: loc, array: arrayValue, |
| 496 | indices: operands.getIndices()); |
| 497 | rewriter.replaceOpWithNewOp<emitc::AssignOp>(op, args&: subscript, |
| 498 | args: operands.getValue()); |
| 499 | return success(); |
| 500 | } |
| 501 | |
| 502 | if (!ptr) |
| 503 | return rewriter.notifyMatchFailure(arg&: loc, msg: "expected array or pointer type" ); |
| 504 | MemRefType opMemrefType = cast<MemRefType>(Val: op.getMemref().getType()); |
| 505 | ValueRange indices = operands.getIndices(); |
| 506 | |
| 507 | ImplicitLocOpBuilder b(loc, rewriter); |
| 508 | Value linearIndex = computeRowMajorLinearIndex(builder&: b, memrefType: opMemrefType, indices); |
| 509 | auto typedPtr = cast<TypedValue<emitc::PointerType>>(Val&: ptr); |
| 510 | auto subscript = |
| 511 | emitc::SubscriptOp::create(builder&: rewriter, location: loc, pointer: typedPtr, index: linearIndex); |
| 512 | |
| 513 | rewriter.replaceOpWithNewOp<emitc::AssignOp>(op, args&: subscript, |
| 514 | args: operands.getValue()); |
| 515 | return success(); |
| 516 | } |
| 517 | }; |
| 518 | |
| 519 | } // namespace |
| 520 | |
| 521 | void mlir::populateMemRefToEmitCConversionPatterns( |
| 522 | RewritePatternSet &patterns, const TypeConverter &converter) { |
| 523 | patterns.add<ConvertAlloca, ConvertAlloc, ConvertCopy, ConvertDealloc, |
| 524 | ConvertGlobal, ConvertGetGlobal, ConvertLoad, ConvertStore>( |
| 525 | arg: converter, args: patterns.getContext()); |
| 526 | } |
| 527 | |