brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · f0ddb50 Raw
74 lines · cpp
1//===- TensorCopyInsertion.cpp - Resolve Bufferization Conflicts w/ Copies ===//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/Bufferization/Transforms/Passes.h"10 11#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"12#include "mlir/Dialect/Bufferization/Transforms/Bufferize.h"13#include "mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h"14#include "mlir/Dialect/Bufferization/Transforms/OneShotModuleBufferize.h"15#include "mlir/Dialect/Bufferization/Transforms/Transforms.h"16 17using namespace mlir;18using namespace mlir::bufferization;19 20LogicalResult mlir::bufferization::insertTensorCopies(21    Operation *op, const OneShotBufferizationOptions &options,22    const BufferizationState &bufferizationState,23    BufferizationStatistics *statistics) {24  OneShotAnalysisState analysisState(op, options);25  // Run normal One-Shot Bufferize analysis or One-Shot Module Bufferize26  // analysis depending on whether function boundary bufferization is enabled or27  // not.28  if (options.bufferizeFunctionBoundaries) {29    if (failed(analyzeModuleOp(op, analysisState, statistics)))30      return failure();31  } else {32    if (failed(analyzeOp(op, analysisState, statistics)))33      return failure();34  }35 36  if (options.testAnalysisOnly)37    return success();38 39  return insertTensorCopies(op, analysisState, bufferizationState);40}41 42LogicalResult mlir::bufferization::insertTensorCopies(43    Operation *op, const AnalysisState &analysisState,44    const BufferizationState &bufferizationState) {45  IRRewriter rewriter(op->getContext());46 47  // It may be more efficient to walk in pre-order here, but the current48  // implementation visits regions of ops even if they are not allowed or49  // bufferizable, and existing tests rely on this behavior.50  // For now, only exclude nested operations if they are in a different symbol51  // table scope.52  WalkResult result = op->walk([&](Operation *nestedOp) {53    if (op->hasTrait<OpTrait::SymbolTable>() &&54        nestedOp->getParentWithTrait<OpTrait::SymbolTable>() != op)55      return WalkResult::skip();56 57    auto bufferizableOp =58        analysisState.getOptions().dynCastBufferizableOp(nestedOp);59    if (!bufferizableOp)60      return WalkResult::skip();61 62    // Find inplacability conflicts and resolve them. (Typically with explicit63    // tensor copies in the form of AllocTensorOps.)64    rewriter.setInsertionPoint(nestedOp);65    if (failed(bufferizableOp.resolveConflicts(rewriter, analysisState,66                                               bufferizationState)))67      return WalkResult::interrupt();68 69    return WalkResult::advance();70  });71 72  return failure(result.wasInterrupted());73}74