|
| 1 | +//===- BufferPlacement.h - Buffer Assignment Utilities ---------*- C++ -*-===// |
| 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 header file defines buffer assignment helper methods to compute correct |
| 10 | +// and valid positions for placing Alloc and Dealloc operations. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#ifndef MLIR_TRANSFORMS_BUFFERPLACEMENT_H |
| 15 | +#define MLIR_TRANSFORMS_BUFFERPLACEMENT_H |
| 16 | + |
| 17 | +#include "mlir/Analysis/Dominance.h" |
| 18 | +#include "mlir/Analysis/Liveness.h" |
| 19 | +#include "mlir/Dialect/StandardOps/IR/Ops.h" |
| 20 | +#include "mlir/IR/Builders.h" |
| 21 | +#include "mlir/IR/Operation.h" |
| 22 | +#include "mlir/Transforms/DialectConversion.h" |
| 23 | + |
| 24 | +namespace mlir { |
| 25 | + |
| 26 | +/// Prepares a buffer placement phase. It can place (user-defined) alloc |
| 27 | +/// nodes. This simplifies the integration of the actual buffer-placement |
| 28 | +/// pass. Sample usage: |
| 29 | +/// BufferAssignmentPlacer baHelper(regionOp); |
| 30 | +/// -> determine alloc positions |
| 31 | +/// auto allocPosition = baHelper.computeAllocPosition(value); |
| 32 | +/// -> place alloc |
| 33 | +/// allocBuilder.setInsertionPoint(positions.getAllocPosition()); |
| 34 | +/// <create alloc> |
| 35 | +/// Note: this class is intended to be used during legalization. In order |
| 36 | +/// to move alloc and dealloc nodes into the right places you can use the |
| 37 | +/// createBufferPlacementPass() function. |
| 38 | +class BufferAssignmentPlacer { |
| 39 | +public: |
| 40 | + /// Creates a new assignment builder. |
| 41 | + explicit BufferAssignmentPlacer(Operation *op); |
| 42 | + |
| 43 | + /// Returns the operation this analysis was constructed from. |
| 44 | + Operation *getOperation() const { return operation; } |
| 45 | + |
| 46 | + /// Computes the actual position to place allocs for the given result. |
| 47 | + OpBuilder::InsertPoint computeAllocPosition(OpResult result); |
| 48 | + |
| 49 | +private: |
| 50 | + /// The operation this analysis was constructed from. |
| 51 | + Operation *operation; |
| 52 | +}; |
| 53 | + |
| 54 | +/// Helper conversion pattern that encapsulates a BufferAssignmentPlacer |
| 55 | +/// instance. Sample usage: |
| 56 | +/// class CustomConversionPattern : public |
| 57 | +/// BufferAssignmentOpConversionPattern<MyOpT> |
| 58 | +/// { |
| 59 | +/// ... matchAndRewrite(...) { |
| 60 | +/// -> Access stored BufferAssignmentPlacer |
| 61 | +/// bufferAssignment->computeAllocPosition(resultOp); |
| 62 | +/// } |
| 63 | +/// }; |
| 64 | +template <typename SourceOp> |
| 65 | +class BufferAssignmentOpConversionPattern |
| 66 | + : public OpConversionPattern<SourceOp> { |
| 67 | +public: |
| 68 | + explicit BufferAssignmentOpConversionPattern( |
| 69 | + MLIRContext *context, BufferAssignmentPlacer *bufferAssignment = nullptr, |
| 70 | + TypeConverter *converter = nullptr, PatternBenefit benefit = 1) |
| 71 | + : OpConversionPattern<SourceOp>(context, benefit), |
| 72 | + bufferAssignment(bufferAssignment), converter(converter) {} |
| 73 | + |
| 74 | +protected: |
| 75 | + BufferAssignmentPlacer *bufferAssignment; |
| 76 | + TypeConverter *converter; |
| 77 | +}; |
| 78 | + |
| 79 | +/// This conversion adds an extra argument for each function result which makes |
| 80 | +/// the converted function a void function. A type converter must be provided |
| 81 | +/// for this conversion to convert a non-shaped type to memref. |
| 82 | +/// BufferAssignmentTypeConverter is an helper TypeConverter for this |
| 83 | +/// purpose. All the non-shaped type of the input function will be converted to |
| 84 | +/// memref. |
| 85 | +class FunctionAndBlockSignatureConverter |
| 86 | + : public BufferAssignmentOpConversionPattern<FuncOp> { |
| 87 | +public: |
| 88 | + using BufferAssignmentOpConversionPattern< |
| 89 | + FuncOp>::BufferAssignmentOpConversionPattern; |
| 90 | + |
| 91 | + /// Performs the actual signature rewriting step. |
| 92 | + LogicalResult |
| 93 | + matchAndRewrite(FuncOp funcOp, ArrayRef<Value> operands, |
| 94 | + ConversionPatternRewriter &rewriter) const final; |
| 95 | +}; |
| 96 | + |
| 97 | +/// This pattern converter transforms a non-void ReturnOpSourceTy into a void |
| 98 | +/// return of type ReturnOpTargetTy. It uses a copy operation of type CopyOpTy |
| 99 | +/// to copy the results to the output buffer. |
| 100 | +template <typename ReturnOpSourceTy, typename ReturnOpTargetTy, |
| 101 | + typename CopyOpTy> |
| 102 | +class NonVoidToVoidReturnOpConverter |
| 103 | + : public BufferAssignmentOpConversionPattern<ReturnOpSourceTy> { |
| 104 | +public: |
| 105 | + using BufferAssignmentOpConversionPattern< |
| 106 | + ReturnOpSourceTy>::BufferAssignmentOpConversionPattern; |
| 107 | + |
| 108 | + /// Performs the actual return-op conversion step. |
| 109 | + LogicalResult |
| 110 | + matchAndRewrite(ReturnOpSourceTy returnOp, ArrayRef<Value> operands, |
| 111 | + ConversionPatternRewriter &rewriter) const final { |
| 112 | + unsigned numReturnValues = returnOp.getNumOperands(); |
| 113 | + Block &entryBlock = returnOp.getParentRegion()->front(); |
| 114 | + unsigned numFuncArgs = entryBlock.getNumArguments(); |
| 115 | + Location loc = returnOp.getLoc(); |
| 116 | + |
| 117 | + // Find the corresponding output buffer for each operand. |
| 118 | + assert(numReturnValues <= numFuncArgs && |
| 119 | + "The number of operands of return operation is more than the " |
| 120 | + "number of function argument."); |
| 121 | + unsigned firstReturnParameter = numFuncArgs - numReturnValues; |
| 122 | + for (auto operand : llvm::enumerate(operands)) { |
| 123 | + unsigned returnArgNumber = firstReturnParameter + operand.index(); |
| 124 | + BlockArgument dstBuffer = entryBlock.getArgument(returnArgNumber); |
| 125 | + if (dstBuffer == operand.value()) |
| 126 | + continue; |
| 127 | + |
| 128 | + // Insert the copy operation to copy before the return. |
| 129 | + rewriter.setInsertionPoint(returnOp); |
| 130 | + rewriter.create<CopyOpTy>(loc, operand.value(), |
| 131 | + entryBlock.getArgument(returnArgNumber)); |
| 132 | + } |
| 133 | + // Insert the new target return operation. |
| 134 | + rewriter.replaceOpWithNewOp<ReturnOpTargetTy>(returnOp); |
| 135 | + return success(); |
| 136 | + } |
| 137 | +}; |
| 138 | + |
| 139 | +/// A helper type converter class for using inside Buffer Assignment operation |
| 140 | +/// conversion patterns. The default constructor keeps all the types intact |
| 141 | +/// except for the ranked-tensor types which is converted to memref types. |
| 142 | +class BufferAssignmentTypeConverter : public TypeConverter { |
| 143 | +public: |
| 144 | + BufferAssignmentTypeConverter(); |
| 145 | +}; |
| 146 | + |
| 147 | +} // end namespace mlir |
| 148 | + |
| 149 | +#endif // MLIR_TRANSFORMS_BUFFERPLACEMENT_H |
0 commit comments