brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.5 KiB · 9199dcc Raw
86 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "mlir/Dialect/Arith/Transforms/BufferDeallocationOpInterfaceImpl.h"10#include "mlir/Dialect/Arith/IR/Arith.h"11#include "mlir/Dialect/Bufferization/IR/BufferDeallocationOpInterface.h"12#include "mlir/Dialect/MemRef/IR/MemRef.h"13#include "mlir/IR/Dialect.h"14#include "mlir/IR/Operation.h"15 16using namespace mlir;17using namespace mlir::bufferization;18 19namespace {20/// Provides custom logic to materialize ownership indicator values for the21/// result value of 'arith.select'. Instead of cloning or runtime alias22/// checking, this implementation inserts another `arith.select` to choose the23/// ownership indicator of the operand in the same way the original24/// `arith.select` chooses the MemRef operand. If at least one of the operand's25/// ownerships is 'Unknown', fall back to the default implementation.26///27/// Example:28/// ```mlir29/// // let ownership(%m0) := %o030/// // let ownership(%m1) := %o131/// %res = arith.select %cond, %m0, %m132/// ```33/// The default implementation would insert a clone and replace all uses of the34/// result of `arith.select` with that clone:35/// ```mlir36/// %res = arith.select %cond, %m0, %m137/// %clone = bufferization.clone %res38/// // let ownership(%res) := 'Unknown'39/// // let ownership(%clone) := %true40/// // replace all uses of %res with %clone41/// ```42/// This implementation, on the other hand, materializes the following:43/// ```mlir44/// %res = arith.select %cond, %m0, %m145/// %res_ownership = arith.select %cond, %o0, %o146/// // let ownership(%res) := %res_ownership47/// ```48struct SelectOpInterface49    : public BufferDeallocationOpInterface::ExternalModel<SelectOpInterface,50                                                          arith::SelectOp> {51  FailureOr<Operation *> process(Operation *op, DeallocationState &state,52                                 const DeallocationOptions &options) const {53    return op; // nothing to do54  }55 56  std::pair<Value, Value>57  materializeUniqueOwnershipForMemref(Operation *op, DeallocationState &state,58                                      const DeallocationOptions &options,59                                      OpBuilder &builder, Value value) const {60    auto selectOp = cast<arith::SelectOp>(op);61    assert(value == selectOp.getResult() &&62           "Value not defined by this operation");63 64    Block *block = value.getParentBlock();65    if (!state.getOwnership(selectOp.getTrueValue(), block).isUnique() ||66        !state.getOwnership(selectOp.getFalseValue(), block).isUnique())67      return state.getMemrefWithUniqueOwnership(builder, value,68                                                value.getParentBlock());69 70    Value ownership = arith::SelectOp::create(71        builder, op->getLoc(), selectOp.getCondition(),72        state.getOwnership(selectOp.getTrueValue(), block).getIndicator(),73        state.getOwnership(selectOp.getFalseValue(), block).getIndicator());74    return {selectOp.getResult(), ownership};75  }76};77 78} // namespace79 80void mlir::arith::registerBufferDeallocationOpInterfaceExternalModels(81    DialectRegistry &registry) {82  registry.addExtension(+[](MLIRContext *ctx, ArithDialect *dialect) {83    SelectOp::attachInterface<SelectOpInterface>(*ctx);84  });85}86