1 //===- Bufferize.cpp - Bufferization of linalg ops ------------------===// 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/Transforms/Bufferize.h" 10 #include "PassDetail.h" 11 #include "mlir/Dialect/Linalg/IR/LinalgOps.h" 12 #include "mlir/Dialect/Linalg/Passes.h" 13 #include "mlir/Dialect/Linalg/Transforms/Transforms.h" 14 #include "mlir/Dialect/Linalg/Utils/Utils.h" 15 #include "mlir/Dialect/Math/IR/Math.h" 16 #include "mlir/Dialect/StandardOps/Transforms/Passes.h" 17 #include "mlir/Dialect/StandardOps/Utils/Utils.h" 18 #include "mlir/Dialect/Vector/VectorOps.h" 19 #include "mlir/IR/BuiltinDialect.h" 20 #include "mlir/IR/Operation.h" 21 #include "mlir/Pass/Pass.h" 22 23 using namespace ::mlir; 24 using namespace ::mlir::linalg; 25 26 static Value cloneMemref(Location loc, Value memref, OpBuilder &b) { 27 auto memrefType = memref.getType().cast<MemRefType>(); 28 auto alloc = b.create<memref::AllocOp>(loc, memrefType, 29 getDynOperands(loc, memref, b)); 30 b.create<linalg::CopyOp>(loc, memref, alloc); 31 return alloc; 32 } 33 34 static LogicalResult 35 allocateBuffersForResults(Location loc, LinalgOp linalgOp, ValueRange outputs, 36 SmallVectorImpl<Value> &resultBuffers, OpBuilder &b) { 37 // Lazily compute loopRanges. 38 SmallVector<Range, 4> loopRanges; 39 40 // Allocate a buffer for every tensor result. 41 assert(linalgOp.getNumOutputs() == linalgOp->getNumResults()); 42 for (auto en : llvm::enumerate(linalgOp->getResultTypes())) { 43 size_t resultIndex = en.index(); 44 Type resultType = en.value(); 45 46 auto tensorType = resultType.dyn_cast<RankedTensorType>(); 47 if (tensorType == nullptr) { 48 linalgOp.emitOpError() 49 << "tensor to buffer conversion expects ranked tensor results"; 50 return failure(); 51 } 52 auto tensorShape = tensorType.getShape(); 53 auto memrefType = MemRefType::get(tensorShape, tensorType.getElementType()); 54 Value resultTensor = outputs[resultIndex]; 55 56 // Clone output buffers whose value is actually used. 57 if (linalgOp.payloadUsesValueFromOutputOperandIndex(resultIndex)) { 58 resultBuffers.push_back(cloneMemref(loc, resultTensor, b)); 59 continue; 60 } 61 62 if (auto alloc = resultTensor.getDefiningOp<memref::AllocOp>()) { 63 resultBuffers.push_back(resultTensor); 64 continue; 65 } 66 // Allocate buffers for statically-shaped results. 67 if (memrefType.hasStaticShape()) { 68 resultBuffers.push_back(b.create<memref::AllocOp>(loc, memrefType)); 69 continue; 70 } 71 72 resultBuffers.push_back(b.create<memref::AllocOp>( 73 loc, memrefType, getDynOperands(loc, resultTensor, b))); 74 } 75 return success(); 76 } 77 78 /// Specialization for `linalg::GenericOp` and `linalg::IndexedGenericOp`. 79 /// A pattern to convert Generic Linalg operations which work on tensors to 80 /// use buffers. BufferPlacement pass should be later used to move 81 /// Alloc operations to the correct positions and insert the missing Dealloc 82 /// operations in the correct places. 83 template <typename GenericOpTy> 84 static void 85 finalizeBufferAllocationForGenericOp(ConversionPatternRewriter &rewriter, 86 GenericOpTy genericOp, ValueRange inputs, 87 ValueRange outputs) { 88 // Generate a new linalg operation that works on buffers. 89 auto newGenericOp = rewriter.create<GenericOpTy>( 90 genericOp.getLoc(), 91 /*resultTensorTypes=*/llvm::None, 92 /*inputs=*/inputs, 93 /*outputs=*/outputs, genericOp.indexing_maps(), 94 genericOp.iterator_types(), genericOp.docAttr(), 95 genericOp.library_callAttr(), genericOp.sparseAttr()); 96 97 // Create a new block in the region of the new Generic Op. 98 Block *oldBlock = genericOp.getBody(); 99 Region &newRegion = newGenericOp.region(); 100 Block *newBlock = rewriter.createBlock(&newRegion, newRegion.begin(), 101 oldBlock->getArgumentTypes()); 102 103 // Clone the body of the old block to the new block. 104 BlockAndValueMapping mapping; 105 mapping.map(oldBlock->getArguments(), newBlock->getArguments()); 106 107 OpBuilder::InsertionGuard guard(rewriter); 108 rewriter.setInsertionPointToEnd(newBlock); 109 for (auto &op : oldBlock->getOperations()) { 110 Operation *clonedOp = rewriter.clone(op, mapping); 111 mapping.map(op.getResults(), clonedOp->getResults()); 112 } 113 114 // Replace the results of the old op with the new output buffers. 115 rewriter.replaceOp(genericOp, outputs); 116 } 117 118 /// Specialization for all other `linalg::LinalgOp`. 119 static void finalizeBufferAllocation(ConversionPatternRewriter &rewriter, 120 linalg::LinalgOp linalgOp, 121 ValueRange inputs, ValueRange outputs) { 122 assert(!isa<linalg::GenericOp>(linalgOp.getOperation())); 123 assert(!isa<linalg::IndexedGenericOp>(linalgOp.getOperation())); 124 SmallVector<Value, 8> newOperands = inputs; 125 newOperands.append(outputs.begin(), outputs.end()); 126 auto otherOperands = linalgOp.getAssumedNonShapedOperands(); 127 newOperands.append(otherOperands.begin(), otherOperands.end()); 128 linalgOp.clone(rewriter, linalgOp.getLoc(), 129 /*resultTypes=*/ArrayRef<Type>{}, newOperands); 130 // Replace the results of the old op with the new output buffers. 131 rewriter.replaceOp(linalgOp, outputs); 132 } 133 134 //===----------------------------------------------------------------------===// 135 // Bufferization patterns. 136 //===----------------------------------------------------------------------===// 137 138 namespace { 139 140 /// Conversion pattern that replaces `linalg.init_tensor` with allocation. 141 class BufferizeInitTensorOp : public OpConversionPattern<InitTensorOp> { 142 public: 143 using OpConversionPattern<InitTensorOp>::OpConversionPattern; 144 145 LogicalResult 146 matchAndRewrite(InitTensorOp op, ArrayRef<Value> operands, 147 ConversionPatternRewriter &rewriter) const final { 148 linalg::InitTensorOpAdaptor adaptor(operands, op->getAttrDictionary()); 149 rewriter.replaceOpWithNewOp<memref::AllocOp>( 150 op, getTypeConverter()->convertType(op.getType()).cast<MemRefType>(), 151 adaptor.sizes()); 152 return success(); 153 } 154 }; 155 156 /// Conversion pattern that bufferizes `linalg.fill` operation. 157 class BufferizeFillOp : public OpConversionPattern<FillOp> { 158 public: 159 using OpConversionPattern<FillOp>::OpConversionPattern; 160 161 LogicalResult 162 matchAndRewrite(FillOp op, ArrayRef<Value> operands, 163 ConversionPatternRewriter &rewriter) const final { 164 linalg::FillOpAdaptor adaptor(operands, op->getAttrDictionary()); 165 if (!op.output().getType().isa<TensorType>()) 166 return rewriter.notifyMatchFailure(op, 167 "operand must be of a tensor type"); 168 169 rewriter.create<FillOp>(op.getLoc(), adaptor.output(), adaptor.value()); 170 rewriter.replaceOp(op, adaptor.output()); 171 172 return success(); 173 } 174 }; 175 176 /// Generic conversion pattern that matches any LinalgOp. This avoids template 177 /// instantiating one pattern for each LinalgOp. 178 class BufferizeAnyLinalgOp : public OpInterfaceConversionPattern<LinalgOp> { 179 public: 180 using OpInterfaceConversionPattern<LinalgOp>::OpInterfaceConversionPattern; 181 182 LogicalResult 183 matchAndRewrite(LinalgOp op, ArrayRef<Value> operands, 184 ConversionPatternRewriter &rewriter) const final { 185 // GenericOpAdaptor below expects an `operand_segment_sizes` attribute. 186 if (!op->hasAttr("operand_segment_sizes")) 187 return failure(); 188 189 // We abuse the GenericOpAdaptor here. 190 // TODO: Manually create an Adaptor that captures inputs and outputs for all 191 // linalg::LinalgOp interface ops. 192 linalg::GenericOpAdaptor adaptor(operands, op->getAttrDictionary()); 193 194 Location loc = op.getLoc(); 195 SmallVector<Value, 2> newOutputBuffers; 196 197 if (failed(allocateBuffersForResults(loc, op, adaptor.outputs(), 198 newOutputBuffers, rewriter))) { 199 return op.emitOpError() 200 << "Failed to allocate buffers for tensor results."; 201 } 202 203 // Delegate to the linalg generic pattern. 204 if (auto genericOp = dyn_cast<linalg::GenericOp>(*op)) { 205 finalizeBufferAllocationForGenericOp<GenericOp>( 206 rewriter, genericOp, adaptor.inputs(), newOutputBuffers); 207 return success(); 208 } 209 210 // Delegate to the linalg indexed generic pattern. 211 if (auto genericOp = dyn_cast<linalg::IndexedGenericOp>(*op)) { 212 finalizeBufferAllocationForGenericOp<IndexedGenericOp>( 213 rewriter, genericOp, adaptor.inputs(), newOutputBuffers); 214 return success(); 215 } 216 217 finalizeBufferAllocation(rewriter, op, adaptor.inputs(), newOutputBuffers); 218 return success(); 219 } 220 }; 221 222 /// Convert `subtensor %t [offsets][sizes][strides] -> %st` to an alloc + copy 223 /// pattern. 224 /// ``` 225 /// %a = alloc(sizes) 226 /// %sv = subview %source [offsets][sizes][strides] 227 /// linalg_copy(%sv, %a) 228 /// ``` 229 /// 230 /// This pattern is arguable a std pattern once linalg::CopyOp becomes 231 /// std::CopyOp. 232 class SubTensorOpConverter : public OpConversionPattern<SubTensorOp> { 233 public: 234 using OpConversionPattern<SubTensorOp>::OpConversionPattern; 235 236 LogicalResult 237 matchAndRewrite(SubTensorOp op, ArrayRef<Value> operands, 238 ConversionPatternRewriter &rewriter) const final { 239 SubTensorOpAdaptor adaptor(operands, op->getAttrDictionary()); 240 Value sourceMemref = adaptor.source(); 241 assert(sourceMemref.getType().isa<MemRefType>()); 242 243 MemRefType subviewMemRefType = 244 getTypeConverter()->convertType(op.getType()).cast<MemRefType>(); 245 // op.sizes() capture exactly the dynamic alloc operands matching the 246 // subviewMemRefType thanks to subview/subtensor canonicalization and 247 // verification. 248 Value alloc = rewriter.create<memref::AllocOp>( 249 op.getLoc(), subviewMemRefType, op.sizes()); 250 Value subView = rewriter.create<memref::SubViewOp>( 251 op.getLoc(), sourceMemref, op.getMixedOffsets(), op.getMixedSizes(), 252 op.getMixedStrides()); 253 rewriter.create<linalg::CopyOp>(op.getLoc(), subView, alloc); 254 rewriter.replaceOp(op, alloc); 255 return success(); 256 } 257 }; 258 259 /// Convert `subtensor_insert %source into %dest [offsets][sizes][strides] -> 260 /// %t` to an buffer_cast + subview + copy + tensor_load pattern. 261 /// buffer_cast and tensor_load are inserted automatically by the 262 /// conversion infra: 263 /// ``` 264 /// %sv = subview %dest [offsets][sizes][strides] 265 /// linalg_copy(%source, %sv) 266 /// // replace with %dest 267 /// ``` 268 /// 269 /// This pattern is arguable a std pattern once linalg::CopyOp becomes 270 /// std::CopyOp. 271 class SubTensorInsertOpConverter 272 : public OpConversionPattern<SubTensorInsertOp> { 273 public: 274 using OpConversionPattern<SubTensorInsertOp>::OpConversionPattern; 275 276 LogicalResult 277 matchAndRewrite(SubTensorInsertOp op, ArrayRef<Value> operands, 278 ConversionPatternRewriter &rewriter) const final { 279 SubTensorInsertOpAdaptor adaptor(operands, op->getAttrDictionary()); 280 Value sourceMemRef = adaptor.source(); 281 assert(sourceMemRef.getType().isa<MemRefType>()); 282 283 // For now, be conservative and copy the converted input memref. 284 // In general, the converted input memref here could be aliased or could 285 // point into constant memory, so mutating it would lead to miscompilations. 286 Value destMemRef = cloneMemref(op.getLoc(), adaptor.dest(), rewriter); 287 assert(destMemRef.getType().isa<MemRefType>()); 288 289 // Take a subview to copy the small memref. 290 Value subview = rewriter.create<memref::SubViewOp>( 291 op.getLoc(), destMemRef, op.getMixedOffsets(), op.getMixedSizes(), 292 op.getMixedStrides()); 293 // Copy the small memref. 294 rewriter.create<linalg::CopyOp>(op.getLoc(), sourceMemRef, subview); 295 rewriter.replaceOp(op, destMemRef); 296 return success(); 297 } 298 }; 299 } // namespace 300 301 namespace { 302 /// Converts Linalg operations that work on tensor-type operands or results to 303 /// work on buffers. 304 struct LinalgBufferizePass : public LinalgBufferizeBase<LinalgBufferizePass> { 305 void runOnOperation() override { 306 MLIRContext &context = getContext(); 307 ConversionTarget target(context); 308 BufferizeTypeConverter typeConverter; 309 310 // Mark all Standard operations legal. 311 target.addLegalDialect<AffineDialect, math::MathDialect, 312 memref::MemRefDialect, StandardOpsDialect>(); 313 target.addIllegalOp<InitTensorOp, SubTensorOp, SubTensorInsertOp>(); 314 315 // Mark all Linalg operations illegal as long as they work on tensors. 316 auto isLegalOperation = [&](Operation *op) { 317 return typeConverter.isLegal(op); 318 }; 319 target.addDynamicallyLegalDialect<linalg::LinalgDialect>(isLegalOperation); 320 target.addDynamicallyLegalOp<ConstantOp>(isLegalOperation); 321 322 RewritePatternSet patterns(&context); 323 populateLinalgBufferizePatterns(typeConverter, patterns); 324 if (failed(applyPartialConversion(getOperation(), target, 325 std::move(patterns)))) 326 signalPassFailure(); 327 } 328 }; 329 } // end anonymous namespace 330 331 std::unique_ptr<OperationPass<FuncOp>> mlir::createLinalgBufferizePass() { 332 return std::make_unique<LinalgBufferizePass>(); 333 } 334 335 void mlir::linalg::populateLinalgBufferizePatterns( 336 BufferizeTypeConverter &typeConverter, RewritePatternSet &patterns) { 337 // TODO: Drop this once tensor constants work in standard. 338 // clang-format off 339 patterns.add< 340 BufferizeAnyLinalgOp, 341 BufferizeFillOp, 342 BufferizeInitTensorOp, 343 SubTensorOpConverter, 344 SubTensorInsertOpConverter 345 >(typeConverter, patterns.getContext()); 346 // clang-format on 347 } 348