|
| 1 | +//===- BufferizableOpInterfaceImpl.cpp - Impl. of BufferizableOpInterface -===// |
| 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 | +#include "mlir/Dialect/Tensor/Transforms/BufferizableOpInterfaceImpl.h" |
| 10 | + |
| 11 | +#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h" |
| 12 | +#include "mlir/Dialect/MLProgram/IR/MLProgram.h" |
| 13 | +#include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 14 | + |
| 15 | +using namespace mlir; |
| 16 | +using namespace mlir::bufferization; |
| 17 | +using namespace mlir::ml_program; |
| 18 | + |
| 19 | +namespace mlir { |
| 20 | +namespace ml_program { |
| 21 | + |
| 22 | +template <typename Interface, typename Op> |
| 23 | +struct ExternalModelBase |
| 24 | + : public BufferizableOpInterface::ExternalModel<Interface, Op> { |
| 25 | + |
| 26 | + AliasingValueList getAliasingValues(Operation *, OpOperand &, |
| 27 | + const AnalysisState &) const { |
| 28 | + return {}; |
| 29 | + } |
| 30 | + |
| 31 | + BufferRelation bufferRelation(Operation *, OpResult, |
| 32 | + const AnalysisState &) const { |
| 33 | + return BufferRelation::Unknown; |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +/// Bufferization of ml_program.global into a memref.global |
| 38 | +struct GlobalOpInterface |
| 39 | + : public ExternalModelBase<GlobalOpInterface, GlobalOp> { |
| 40 | + |
| 41 | + bool bufferizesToMemoryRead(Operation *, OpOperand &, |
| 42 | + const AnalysisState &) const { |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + bool bufferizesToMemoryWrite(Operation *, OpOperand &, |
| 47 | + const AnalysisState &) const { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + bool hasTensorSemantics(Operation *) const { return true; } |
| 52 | + |
| 53 | + LogicalResult bufferize(Operation *op, RewriterBase &rewriter, |
| 54 | + const BufferizationOptions &) const { |
| 55 | + auto globalOp = cast<GlobalOp>(op); |
| 56 | + if (!globalOp.getValue().has_value()) |
| 57 | + return globalOp.emitError("global op must have a value"); |
| 58 | + |
| 59 | + auto tensorType = cast<TensorType>(globalOp.getType()); |
| 60 | + auto memrefType = getMemRefTypeWithStaticIdentityLayout(tensorType); |
| 61 | + |
| 62 | + replaceOpWithNewBufferizedOp<memref::GlobalOp>( |
| 63 | + rewriter, globalOp, globalOp.getSymName(), |
| 64 | + /*sym_visibility=*/globalOp.getSymVisibilityAttr(), |
| 65 | + /*type=*/cast<MemRefType>(memrefType), |
| 66 | + /*initial_value=*/globalOp.getValue().value(), |
| 67 | + /*constant=*/!globalOp.getIsMutable(), |
| 68 | + /*alignment=*/nullptr); |
| 69 | + return success(); |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +/// Bufferization of ml_program.global_load into a memref.get_global |
| 74 | +struct GlobalLoadOpInterface |
| 75 | + : public ExternalModelBase<GlobalLoadOpInterface, GlobalLoadOp> { |
| 76 | + |
| 77 | + bool bufferizesToMemoryRead(Operation *, OpOperand &, |
| 78 | + const AnalysisState &) const { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + bool bufferizesToMemoryWrite(Operation *, OpOperand &, |
| 83 | + const AnalysisState &) const { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + LogicalResult bufferize(Operation *op, RewriterBase &rewriter, |
| 88 | + const BufferizationOptions &) const { |
| 89 | + auto globalLoadOp = cast<GlobalLoadOp>(op); |
| 90 | + |
| 91 | + auto tensorType = cast<TensorType>(globalLoadOp.getType()); |
| 92 | + auto memrefType = getMemRefTypeWithStaticIdentityLayout(tensorType); |
| 93 | + |
| 94 | + replaceOpWithNewBufferizedOp<memref::GetGlobalOp>( |
| 95 | + rewriter, globalLoadOp, memrefType, |
| 96 | + globalLoadOp.getGlobalAttr().getLeafReference()); |
| 97 | + |
| 98 | + return success(); |
| 99 | + } |
| 100 | +}; |
| 101 | + |
| 102 | +/// Bufferization of ml_program.global_store into a memref.get_global and |
| 103 | +/// memcpy |
| 104 | +struct GlobalStoreOpInterface |
| 105 | + : public ExternalModelBase<GlobalStoreOpInterface, GlobalStoreOp> { |
| 106 | + |
| 107 | + bool bufferizesToMemoryRead(Operation *, OpOperand &, |
| 108 | + const AnalysisState &) const { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + bool bufferizesToMemoryWrite(Operation *, OpOperand &, |
| 113 | + const AnalysisState &) const { |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + LogicalResult bufferize(Operation *op, RewriterBase &rewriter, |
| 118 | + const BufferizationOptions &options) const { |
| 119 | + auto globalStoreOp = cast<GlobalStoreOp>(op); |
| 120 | + |
| 121 | + auto tensorType = cast<TensorType>(globalStoreOp.getValue().getType()); |
| 122 | + auto memrefType = getMemRefTypeWithStaticIdentityLayout(tensorType); |
| 123 | + |
| 124 | + auto loc = globalStoreOp.getLoc(); |
| 125 | + auto targetMemref = rewriter.create<memref::GetGlobalOp>( |
| 126 | + loc, memrefType, globalStoreOp.getGlobalAttr().getLeafReference()); |
| 127 | + |
| 128 | + auto sourceMemref = getBuffer(rewriter, globalStoreOp.getValue(), options); |
| 129 | + if (failed(sourceMemref)) { |
| 130 | + return failure(); |
| 131 | + } |
| 132 | + |
| 133 | + auto memcpy = |
| 134 | + options.createMemCpy(rewriter, loc, sourceMemref.value(), targetMemref); |
| 135 | + if (failed(memcpy)) { |
| 136 | + return failure(); |
| 137 | + } |
| 138 | + rewriter.eraseOp(globalStoreOp); |
| 139 | + |
| 140 | + return success(); |
| 141 | + } |
| 142 | +}; |
| 143 | + |
| 144 | +void registerBufferizableOpInterfaceExternalModels(DialectRegistry ®istry) { |
| 145 | + registry.addExtension(+[](MLIRContext *ctx, MLProgramDialect *) { |
| 146 | + GlobalOp::attachInterface<GlobalOpInterface>(*ctx); |
| 147 | + GlobalLoadOp::attachInterface<GlobalLoadOpInterface>(*ctx); |
| 148 | + GlobalStoreOp::attachInterface<GlobalStoreOpInterface>(*ctx); |
| 149 | + }); |
| 150 | +} |
| 151 | + |
| 152 | +} // namespace ml_program |
| 153 | +} // namespace mlir |
0 commit comments