|
| 1 | +//===- BufferDeallocationOpInterfaceImpl.cpp ------------------------------===// |
| 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/SCF/Transforms/BufferDeallocationOpInterfaceImpl.h" |
| 10 | +#include "mlir/Dialect/Bufferization/IR/BufferDeallocationOpInterface.h" |
| 11 | +#include "mlir/Dialect/Bufferization/IR/Bufferization.h" |
| 12 | +#include "mlir/Dialect/SCF/IR/SCF.h" |
| 13 | + |
| 14 | +using namespace mlir; |
| 15 | +using namespace mlir::bufferization; |
| 16 | + |
| 17 | +namespace { |
| 18 | +/// The `scf.forall.in_parallel` terminator is special in a few ways: |
| 19 | +/// * It does not implement the BranchOpInterface or |
| 20 | +/// RegionBranchTerminatorOpInterface, but the ParallelCombiningOpInterface |
| 21 | +/// which is not supported by BufferDeallocation. |
| 22 | +/// * It has a graph-like region which only allows one specific tensor op |
| 23 | +/// * After bufferization the nested region is always empty |
| 24 | +/// For these reasons we provide custom deallocation logic via this external |
| 25 | +/// model. |
| 26 | +/// |
| 27 | +/// Example: |
| 28 | +/// ```mlir |
| 29 | +/// scf.forall (%arg1) in (%arg0) { |
| 30 | +/// %alloc = memref.alloc() : memref<2xf32> |
| 31 | +/// ... |
| 32 | +/// <implicit in_parallel terminator here> |
| 33 | +/// } |
| 34 | +/// ``` |
| 35 | +/// gets transformed to |
| 36 | +/// ```mlir |
| 37 | +/// scf.forall (%arg1) in (%arg0) { |
| 38 | +/// %alloc = memref.alloc() : memref<2xf32> |
| 39 | +/// ... |
| 40 | +/// bufferization.dealloc (%alloc : memref<2xf32>) if (%true) |
| 41 | +/// <implicit in_parallel terminator here> |
| 42 | +/// } |
| 43 | +/// ``` |
| 44 | +struct InParallelOpInterface |
| 45 | + : public BufferDeallocationOpInterface::ExternalModel<InParallelOpInterface, |
| 46 | + scf::InParallelOp> { |
| 47 | + FailureOr<Operation *> process(Operation *op, DeallocationState &state, |
| 48 | + const DeallocationOptions &options) const { |
| 49 | + auto inParallelOp = cast<scf::InParallelOp>(op); |
| 50 | + OpBuilder builder(op); |
| 51 | + if (!inParallelOp.getBody()->empty()) |
| 52 | + return op->emitError("only supported when nested region is empty"); |
| 53 | + |
| 54 | + // Collect the values to deallocate and retain and use them to create the |
| 55 | + // dealloc operation. |
| 56 | + Block *block = op->getBlock(); |
| 57 | + SmallVector<Value> memrefs, conditions, toRetain; |
| 58 | + if (failed(state.getMemrefsAndConditionsToDeallocate( |
| 59 | + builder, op->getLoc(), block, memrefs, conditions))) |
| 60 | + return failure(); |
| 61 | + |
| 62 | + state.getMemrefsToRetain(block, /*toBlock=*/nullptr, {}, toRetain); |
| 63 | + if (memrefs.empty() && toRetain.empty()) |
| 64 | + return op; |
| 65 | + |
| 66 | + auto deallocOp = builder.create<bufferization::DeallocOp>( |
| 67 | + op->getLoc(), memrefs, conditions, toRetain); |
| 68 | + |
| 69 | + // We want to replace the current ownership of the retained values with the |
| 70 | + // result values of the dealloc operation as they are always unique. |
| 71 | + state.resetOwnerships(deallocOp.getRetained(), block); |
| 72 | + for (auto [retained, ownership] : |
| 73 | + llvm::zip(deallocOp.getRetained(), deallocOp.getUpdatedConditions())) |
| 74 | + state.updateOwnership(retained, ownership, block); |
| 75 | + |
| 76 | + return op; |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +} // namespace |
| 81 | + |
| 82 | +void mlir::scf::registerBufferDeallocationOpInterfaceExternalModels( |
| 83 | + DialectRegistry ®istry) { |
| 84 | + registry.addExtension(+[](MLIRContext *ctx, SCFDialect *dialect) { |
| 85 | + InParallelOp::attachInterface<InParallelOpInterface>(*ctx); |
| 86 | + }); |
| 87 | +} |
0 commit comments