|
| 1 | +//===- Bufferize.cpp - MLProgram bufferize pass ---------------------------===// |
| 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 a bufferization pass for the MLProgram dialect |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "mlir/Dialect/MLProgram/Transforms/Passes.h" |
| 14 | + |
| 15 | +#include "mlir/Dialect/Bufferization/IR/Bufferization.h" |
| 16 | +#include "mlir/Dialect/MLProgram/IR/MLProgram.h" |
| 17 | +#include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 18 | +#include "mlir/IR/BuiltinTypes.h" |
| 19 | + |
| 20 | +namespace mlir { |
| 21 | +namespace ml_program { |
| 22 | +#define GEN_PASS_DEF_MLPROGRAMBUFFERIZE |
| 23 | +#include "mlir/Dialect/MLProgram/Transforms/Passes.h.inc" |
| 24 | + |
| 25 | +static LogicalResult bufferizeMLProgramGlobalOp(GlobalOp globalOp, |
| 26 | + OpBuilder &builder) { |
| 27 | + if (!globalOp.getValue().has_value()) |
| 28 | + return globalOp.emitError("global op must have a value"); |
| 29 | + |
| 30 | + auto tensorType = cast<RankedTensorType>(globalOp.getType()); |
| 31 | + auto memrefType = |
| 32 | + MemRefType::get(tensorType.getShape(), tensorType.getElementType()); |
| 33 | + |
| 34 | + builder.setInsertionPointToStart( |
| 35 | + globalOp->getParentOfType<ModuleOp>().getBody()); |
| 36 | + builder.create<memref::GlobalOp>( |
| 37 | + globalOp.getLoc(), globalOp.getSymName(), |
| 38 | + /*sym_visibility=*/globalOp.getSymVisibilityAttr(), |
| 39 | + /*type=*/memrefType, |
| 40 | + /*initial_value=*/globalOp.getValue().value(), |
| 41 | + /*constant=*/!globalOp.getIsMutable(), |
| 42 | + /*alignment=*/nullptr); |
| 43 | + return success(); |
| 44 | +} |
| 45 | + |
| 46 | +static LogicalResult bufferizeMLProgramGlobalLoadOp(GlobalLoadOp globalLoadOp, |
| 47 | + OpBuilder &builder) { |
| 48 | + auto loc = globalLoadOp.getLoc(); |
| 49 | + auto tensorType = cast<RankedTensorType>(globalLoadOp.getType()); |
| 50 | + auto memrefType = |
| 51 | + MemRefType::get(tensorType.getShape(), tensorType.getElementType()); |
| 52 | + |
| 53 | + builder.setInsertionPoint(globalLoadOp); |
| 54 | + Value globalVal = builder.create<memref::GetGlobalOp>( |
| 55 | + loc, memrefType, globalLoadOp.getGlobalAttr().getLeafReference()); |
| 56 | + |
| 57 | + // We need a copy to guarantee that the produced tensor does not alias with |
| 58 | + // any other buffer. |
| 59 | + Value alloc = builder.create<memref::AllocOp>(loc, memrefType, ValueRange{}); |
| 60 | + builder.create<memref::CopyOp>(globalLoadOp->getLoc(), globalVal, alloc); |
| 61 | + |
| 62 | + globalVal = builder.create<bufferization::ToTensorOp>(loc, tensorType, alloc, |
| 63 | + /*restrict=*/true); |
| 64 | + globalLoadOp->getResult(0).replaceAllUsesWith(globalVal); |
| 65 | + return success(); |
| 66 | +} |
| 67 | + |
| 68 | +static LogicalResult |
| 69 | +bufferizeMLProgramGlobalStoreOp(GlobalStoreOp globalStoreOp, |
| 70 | + OpBuilder &builder) { |
| 71 | + auto loc = globalStoreOp.getLoc(); |
| 72 | + auto tensorType = cast<RankedTensorType>(globalStoreOp.getValue().getType()); |
| 73 | + auto memrefType = |
| 74 | + MemRefType::get(tensorType.getShape(), tensorType.getElementType()); |
| 75 | + |
| 76 | + builder.setInsertionPoint(globalStoreOp); |
| 77 | + Value memref = builder.create<memref::GetGlobalOp>( |
| 78 | + loc, memrefType, globalStoreOp.getGlobalAttr().getLeafReference()); |
| 79 | + Value copyValue = builder.create<bufferization::ToMemrefOp>( |
| 80 | + loc, memrefType, globalStoreOp.getValue()); |
| 81 | + builder.create<memref::CopyOp>(loc, copyValue, memref); |
| 82 | + return success(); |
| 83 | +} |
| 84 | + |
| 85 | +namespace { |
| 86 | +/// Converts MLProgram operations that work on tensor-type operands or results |
| 87 | +/// to work on buffers. |
| 88 | +class MLProgramBufferize |
| 89 | + : public impl::MLProgramBufferizeBase<MLProgramBufferize> { |
| 90 | + void runOnOperation() override { |
| 91 | + auto module = getOperation(); |
| 92 | + OpBuilder builder(module.getBodyRegion()); |
| 93 | + SmallVector<Operation *> toErase; |
| 94 | + |
| 95 | + auto walkResult = module.walk([&](GlobalOp op) { |
| 96 | + if (auto type = dyn_cast<RankedTensorType>(op.getType())) { |
| 97 | + if (!type.hasStaticShape()) { |
| 98 | + // If the ml_program.global has dynamically shaped tensor. |
| 99 | + op.emitError( |
| 100 | + "unimplemented: global op bufferization with dynamic shape"); |
| 101 | + return WalkResult::interrupt(); |
| 102 | + } |
| 103 | + } else { |
| 104 | + // If the ml_program.global is of non-tensor type. |
| 105 | + op.emitError("unsupported global op type"); |
| 106 | + return WalkResult::interrupt(); |
| 107 | + } |
| 108 | + |
| 109 | + if (failed(bufferizeMLProgramGlobalOp(op, builder))) { |
| 110 | + op.emitError("bufferization for this op failed"); |
| 111 | + return WalkResult::interrupt(); |
| 112 | + } |
| 113 | + toErase.push_back(op); |
| 114 | + return WalkResult::advance(); |
| 115 | + }); |
| 116 | + |
| 117 | + if (walkResult.wasInterrupted()) |
| 118 | + return signalPassFailure(); |
| 119 | + |
| 120 | + module.walk([&](GlobalLoadOp op) { |
| 121 | + if (failed(bufferizeMLProgramGlobalLoadOp(op, builder))) { |
| 122 | + op.emitError("bufferization for this op failed"); |
| 123 | + return; |
| 124 | + } |
| 125 | + toErase.push_back(op); |
| 126 | + }); |
| 127 | + |
| 128 | + module.walk([&](GlobalStoreOp op) { |
| 129 | + if (failed(bufferizeMLProgramGlobalStoreOp(op, builder))) { |
| 130 | + op.emitError("bufferization for this op failed"); |
| 131 | + return; |
| 132 | + } |
| 133 | + toErase.push_back(op); |
| 134 | + }); |
| 135 | + |
| 136 | + for (auto *op : llvm::reverse(toErase)) |
| 137 | + op->erase(); |
| 138 | + } |
| 139 | +}; |
| 140 | +} // namespace |
| 141 | +} // namespace ml_program |
| 142 | +} // namespace mlir |
0 commit comments