#include "mlir/Dialect/Bufferization/Transforms/Passes.h"
#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
#include "mlir/Dialect/Bufferization/Transforms/Bufferize.h"
#include "mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h"
#include "mlir/Dialect/Bufferization/Transforms/OneShotModuleBufferize.h"
#include "mlir/Dialect/Bufferization/Transforms/Transforms.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/Operation.h"
#include "mlir/Interfaces/ControlFlowInterfaces.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Transforms/Passes.h"
#include <optional>
namespace mlir {
namespace bufferization {
#define GEN_PASS_DEF_FINALIZINGBUFFERIZE
#define GEN_PASS_DEF_BUFFERIZATIONBUFFERIZE
#define GEN_PASS_DEF_ONESHOTBUFFERIZE
#include "mlir/Dialect/Bufferization/Transforms/Passes.h.inc"
}
}
#define DEBUG_TYPE "bufferize"
using namespace mlir;
using namespace mlir::bufferization;
static Value materializeToTensor(OpBuilder &builder, TensorType type,
ValueRange inputs, Location loc) {
assert(inputs.size() == 1);
assert(isa<BaseMemRefType>(inputs[0].getType()));
return builder.create<bufferization::ToTensorOp>(loc, type, inputs[0]);
}
BufferizeTypeConverter::BufferizeTypeConverter() {
addConversion([](Type type) { return type; });
addConversion([](RankedTensorType type) -> Type {
return MemRefType::get(type.getShape(), type.getElementType());
});
addConversion([](UnrankedTensorType type) -> Type {
return UnrankedMemRefType::get(type.getElementType(), 0);
});
addArgumentMaterialization(materializeToTensor);
addSourceMaterialization(materializeToTensor);
addTargetMaterialization([](OpBuilder &builder, BaseMemRefType type,
ValueRange inputs, Location loc) -> Value {
assert(inputs.size() == 1 && "expected exactly one input");
if (auto inputType = dyn_cast<MemRefType>(inputs[0].getType())) {
assert(inputType != type && "expected different types");
auto rankedDestType = dyn_cast<MemRefType>(type);
if (!rankedDestType)
return nullptr;
BufferizationOptions options;
options.bufferAlignment = 0;
FailureOr<Value> replacement =
castOrReallocMemRefValue(builder, inputs[0], rankedDestType, options);
if (failed(replacement))
return nullptr;
return *replacement;
}
if (isa<TensorType>(inputs[0].getType())) {
return builder.create<bufferization::ToMemrefOp>(loc, type, inputs[0]);
}
llvm_unreachable("only tensor/memref input types supported");
});
}
void mlir::bufferization::populateBufferizeMaterializationLegality(
ConversionTarget &target) {
target.addLegalOp<bufferization::ToTensorOp, bufferization::ToMemrefOp>();
}
namespace {
class BufferizeToTensorOp
: public OpConversionPattern<bufferization::ToTensorOp> {
public:
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(bufferization::ToTensorOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
rewriter.replaceOp(op, adaptor.getMemref());
return success();
}
};
}
namespace {
class BufferizeToMemrefOp
: public OpConversionPattern<bufferization::ToMemrefOp> {
public:
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(bufferization::ToMemrefOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
rewriter.replaceOp(op, adaptor.getTensor());
return success();
}
};
}
void mlir::bufferization::populateEliminateBufferizeMaterializationsPatterns(
BufferizeTypeConverter &typeConverter, RewritePatternSet &patterns) {
patterns.add<BufferizeToTensorOp, BufferizeToMemrefOp>(typeConverter,
patterns.getContext());
}
namespace {
struct FinalizingBufferizePass
: public bufferization::impl::FinalizingBufferizeBase<
FinalizingBufferizePass> {
using FinalizingBufferizeBase<
FinalizingBufferizePass>::FinalizingBufferizeBase;
void runOnOperation() override {
auto func = getOperation();
auto *context = &getContext();
BufferizeTypeConverter typeConverter;
RewritePatternSet patterns(context);
ConversionTarget target(*context);
populateEliminateBufferizeMaterializationsPatterns(typeConverter, patterns);
target.markUnknownOpDynamicallyLegal(
[&](Operation *op) { return typeConverter.isLegal(op); });
if (failed(applyFullConversion(func, target, std::move(patterns))))
signalPassFailure();
}
};
static LayoutMapOption parseLayoutMapOption(const std::string &s) {
if (s == "fully-dynamic-layout-map")
return LayoutMapOption::FullyDynamicLayoutMap;
if (s == "identity-layout-map")
return LayoutMapOption::IdentityLayoutMap;
if (s == "infer-layout-map")
return LayoutMapOption::InferLayoutMap;
llvm_unreachable("invalid layout map option");
}
static OneShotBufferizationOptions::AnalysisHeuristic
parseHeuristicOption(const std::string &s) {
if (s == "bottom-up")
return OneShotBufferizationOptions::AnalysisHeuristic::BottomUp;
if (s == "top-down")
return OneShotBufferizationOptions::AnalysisHeuristic::TopDown;
if (s == "bottom-up-from-terminators")
return OneShotBufferizationOptions::AnalysisHeuristic::
BottomUpFromTerminators;
if (s == "fuzzer")
return OneShotBufferizationOptions::AnalysisHeuristic::Fuzzer;
llvm_unreachable("invalid analysisheuristic option");
}
struct OneShotBufferizePass
: public bufferization::impl::OneShotBufferizeBase<OneShotBufferizePass> {
OneShotBufferizePass() = default;
explicit OneShotBufferizePass(const OneShotBufferizationOptions &options)
: options(options) {}
void getDependentDialects(DialectRegistry ®istry) const override {
registry
.insert<bufferization::BufferizationDialect, memref::MemRefDialect>();
}
void runOnOperation() override {
OneShotBufferizationOptions opt;
if (!options) {
opt.allowReturnAllocsFromLoops = allowReturnAllocsFromLoops;
opt.allowUnknownOps = allowUnknownOps;
opt.analysisFuzzerSeed = analysisFuzzerSeed;
opt.analysisHeuristic = parseHeuristicOption(analysisHeuristic);
opt.copyBeforeWrite = copyBeforeWrite;
opt.dumpAliasSets = dumpAliasSets;
opt.setFunctionBoundaryTypeConversion(
parseLayoutMapOption(functionBoundaryTypeConversion));
if (mustInferMemorySpace) {
opt.defaultMemorySpaceFn =
[](TensorType t) -> std::optional<Attribute> {
return std::nullopt;
};
}
opt.printConflicts = printConflicts;
opt.testAnalysisOnly = testAnalysisOnly;
opt.bufferizeFunctionBoundaries = bufferizeFunctionBoundaries;
opt.checkParallelRegions = checkParallelRegions;
opt.noAnalysisFuncFilter = noAnalysisFuncFilter;
LayoutMapOption unknownTypeConversionOption =
parseLayoutMapOption(unknownTypeConversion);
if (unknownTypeConversionOption == LayoutMapOption::InferLayoutMap) {
emitError(UnknownLoc::get(&getContext()),
"Invalid option: 'infer-layout-map' is not a valid value for "
"'unknown-type-conversion'");
return signalPassFailure();
}
opt.unknownTypeConverterFn = [=](Value value, Attribute memorySpace,
const BufferizationOptions &options) {
auto tensorType = cast<TensorType>(value.getType());
if (unknownTypeConversionOption == LayoutMapOption::IdentityLayoutMap)
return bufferization::getMemRefTypeWithStaticIdentityLayout(
tensorType, memorySpace);
assert(unknownTypeConversionOption ==
LayoutMapOption::FullyDynamicLayoutMap &&
"invalid layout map option");
return bufferization::getMemRefTypeWithFullyDynamicLayout(tensorType,
memorySpace);
};
OpFilter::Entry::FilterFn filterFn = [&](Operation *op) {
if (this->dialectFilter.hasValue())
return llvm::is_contained(this->dialectFilter,
op->getDialect()->getNamespace());
return true;
};
opt.opFilter.allowOperation(filterFn);
} else {
opt = *options;
}
if (opt.copyBeforeWrite && opt.testAnalysisOnly) {
emitError(UnknownLoc::get(&getContext()),
"Invalid option: 'copy-before-write' cannot be used with "
"'test-analysis-only'");
return signalPassFailure();
}
if (opt.printConflicts && !opt.testAnalysisOnly) {
emitError(
UnknownLoc::get(&getContext()),
"Invalid option: 'print-conflicts' requires 'test-analysis-only'");
return signalPassFailure();
}
if (opt.dumpAliasSets && !opt.testAnalysisOnly) {
emitError(
UnknownLoc::get(&getContext()),
"Invalid option: 'dump-alias-sets' requires 'test-analysis-only'");
return signalPassFailure();
}
BufferizationStatistics statistics;
ModuleOp moduleOp = getOperation();
if (opt.bufferizeFunctionBoundaries) {
if (failed(runOneShotModuleBufferize(moduleOp, opt, &statistics))) {
signalPassFailure();
return;
}
} else {
if (!opt.noAnalysisFuncFilter.empty()) {
emitError(UnknownLoc::get(&getContext()),
"Invalid option: 'no-analysis-func-filter' requires "
"'bufferize-function-boundaries'");
return signalPassFailure();
}
if (failed(runOneShotBufferize(moduleOp, opt, &statistics))) {
signalPassFailure();
return;
}
}
this->numBufferAlloc = statistics.numBufferAlloc;
this->numTensorInPlace = statistics.numTensorInPlace;
this->numTensorOutOfPlace = statistics.numTensorOutOfPlace;
}
private:
std::optional<OneShotBufferizationOptions> options;
};
}
std::unique_ptr<Pass> mlir::bufferization::createOneShotBufferizePass() {
return std::make_unique<OneShotBufferizePass>();
}
std::unique_ptr<Pass> mlir::bufferization::createOneShotBufferizePass(
const OneShotBufferizationOptions &options) {
return std::make_unique<OneShotBufferizePass>(options);
}
std::unique_ptr<OperationPass<func::FuncOp>>
mlir::bufferization::createFinalizingBufferizePass() {
return std::make_unique<FinalizingBufferizePass>();
}
namespace {
class BufferizationRewriter : public IRRewriter, public RewriterBase::Listener {
public:
BufferizationRewriter(MLIRContext *ctx, DenseSet<Operation *> &erasedOps,
DenseSet<Operation *> &toMemrefOps,
SmallVector<Operation *> &worklist,
const BufferizationOptions &options,
BufferizationStatistics *statistics)
: IRRewriter(ctx), erasedOps(erasedOps), toMemrefOps(toMemrefOps),
worklist(worklist), analysisState(options), statistics(statistics) {
setListener(this);
}
protected:
void notifyOperationErased(Operation *op) override {
erasedOps.insert(op);
toMemrefOps.erase(op);
}
void notifyOperationInserted(Operation *op, InsertPoint previous) override {
if (previous.isSet())
return;
erasedOps.erase(op);
if (statistics) {
if (auto sideEffectingOp = dyn_cast<MemoryEffectOpInterface>(op))
statistics->numBufferAlloc += static_cast<int64_t>(
sideEffectingOp.hasEffect<MemoryEffects::Allocate>());
}
if (isa<ToMemrefOp>(op)) {
toMemrefOps.insert(op);
return;
}
if (isa<ToTensorOp>(op))
return;
if (!hasTensorSemantics(op))
return;
auto const &options = analysisState.getOptions();
if (!options.isOpAllowed(op))
return;
worklist.push_back(op);
}
private:
DenseSet<Operation *> &erasedOps;
DenseSet<Operation *> &toMemrefOps;
SmallVector<Operation *> &worklist;
const AnalysisState analysisState;
BufferizationStatistics *statistics;
};
}
LogicalResult bufferization::bufferizeOp(Operation *op,
const BufferizationOptions &options,
BufferizationStatistics *statistics) {
if (options.copyBeforeWrite) {
AnalysisState state(options);
if (failed(insertTensorCopies(op, state)))
return failure();
}
DenseSet<Operation *> toMemrefOps;
op->walk([&](ToMemrefOp toMemrefOp) { toMemrefOps.insert(toMemrefOp); });
SmallVector<Operation *> worklist;
op->walk<WalkOrder::PostOrder>([&](Operation *op) {
if (options.isOpAllowed(op) && hasTensorSemantics(op))
worklist.push_back(op);
});
DenseSet<Operation *> erasedOps;
BufferizationRewriter rewriter(op->getContext(), erasedOps, toMemrefOps,
worklist, options, statistics);
for (unsigned i = 0; i < worklist.size(); ++i) {
Operation *nextOp = worklist[i];
if (erasedOps.contains(nextOp))
continue;
auto bufferizableOp = options.dynCastBufferizableOp(nextOp);
if (!bufferizableOp)
continue;
if (!hasTensorSemantics(nextOp))
continue;
if (!bufferizableOp.supportsUnstructuredControlFlow())
for (Region &r : nextOp->getRegions())
if (r.getBlocks().size() > 1)
return nextOp->emitOpError(
"op or BufferizableOpInterface implementation does not support "
"unstructured control flow, but at least one region has multiple "
"blocks");
LLVM_DEBUG(llvm::dbgs()
<< "//===-------------------------------------------===//\n"
<< "IR after bufferizing: " << nextOp->getName() << "\n");
rewriter.setInsertionPoint(nextOp);
if (failed(bufferizableOp.bufferize(rewriter, options))) {
LLVM_DEBUG(llvm::dbgs()
<< "failed to bufferize\n"
<< "//===-------------------------------------------===//\n");
return nextOp->emitError("failed to bufferize op");
}
LLVM_DEBUG(llvm::dbgs()
<< *op
<< "\n//===-------------------------------------------===//\n");
}
if (erasedOps.contains(op))
return success();
for (Operation *op : toMemrefOps) {
rewriter.setInsertionPoint(op);
(void)bufferization::foldToMemrefToTensorPair(
rewriter, cast<ToMemrefOp>(op), options);
}
op->walk<WalkOrder::PostOrder>([&](ToTensorOp toTensorOp) {
if (toTensorOp->getUses().empty()) {
rewriter.eraseOp(toTensorOp);
return WalkResult::skip();
}
return WalkResult::advance();
});
if (options.allowUnknownOps)
return success();
for (Operation *op : worklist) {
if (erasedOps.contains(op))
continue;
if (!hasTensorSemantics(op))
continue;
if (!options.isOpAllowed(op))
continue;
if (op->getUses().empty() && isMemoryEffectFree(op))
continue;
if (isa<ToTensorOp, ToMemrefOp>(op))
continue;
return op->emitError("op was not bufferized");
}
return success();
}
LogicalResult
bufferization::bufferizeBlockSignature(Block *block, RewriterBase &rewriter,
const BufferizationOptions &options) {
OpBuilder::InsertionGuard g(rewriter);
auto bufferizableOp = options.dynCastBufferizableOp(block->getParentOp());
if (!bufferizableOp)
return failure();
SmallVector<Type> newTypes;
for (BlockArgument &bbArg : block->getArguments()) {
auto tensorType = dyn_cast<TensorType>(bbArg.getType());
if (!tensorType) {
newTypes.push_back(bbArg.getType());
continue;
}
FailureOr<BaseMemRefType> memrefType =
bufferization::getBufferType(bbArg, options);
if (failed(memrefType))
return failure();
newTypes.push_back(*memrefType);
}
for (auto [bbArg, type] : llvm::zip(block->getArguments(), newTypes)) {
if (bbArg.getType() == type)
continue;
SmallVector<OpOperand *> bbArgUses;
for (OpOperand &use : bbArg.getUses())
bbArgUses.push_back(&use);
bbArg.setType(type);
rewriter.setInsertionPointToStart(block);
if (!bbArgUses.empty()) {
Value toTensorOp =
rewriter.create<bufferization::ToTensorOp>(bbArg.getLoc(), bbArg);
for (OpOperand *use : bbArgUses)
use->set(toTensorOp);
}
}
for (Operation *op : block->getUsers()) {
auto branchOp = dyn_cast<BranchOpInterface>(op);
if (!branchOp)
return op->emitOpError("cannot bufferize ops with block references that "
"do not implement BranchOpInterface");
auto it = llvm::find(op->getSuccessors(), block);
assert(it != op->getSuccessors().end() && "could find successor");
int64_t successorIdx = std::distance(op->getSuccessors().begin(), it);
SuccessorOperands operands = branchOp.getSuccessorOperands(successorIdx);
SmallVector<Value> newOperands;
for (auto [operand, type] :
llvm::zip(operands.getForwardedOperands(), newTypes)) {
if (operand.getType() == type) {
newOperands.push_back(operand);
continue;
}
FailureOr<BaseMemRefType> operandBufferType =
bufferization::getBufferType(operand, options);
if (failed(operandBufferType))
return failure();
rewriter.setInsertionPointAfterValue(operand);
Value bufferizedOperand = rewriter.create<bufferization::ToMemrefOp>(
operand.getLoc(), *operandBufferType, operand);
if (type != *operandBufferType)
bufferizedOperand = rewriter.create<memref::CastOp>(
operand.getLoc(), type, bufferizedOperand);
newOperands.push_back(bufferizedOperand);
}
operands.getMutableForwardedOperands().assign(newOperands);
}
return success();
}
BufferizationOptions bufferization::getPartialBufferizationOptions() {
BufferizationOptions options;
options.allowUnknownOps = true;
options.copyBeforeWrite = true;
options.enforceAliasingInvariants = false;
options.unknownTypeConverterFn = [](Value value, Attribute memorySpace,
const BufferizationOptions &options) {
return getMemRefTypeWithStaticIdentityLayout(
cast<TensorType>(value.getType()), memorySpace);
};
options.opFilter.allowDialect<BufferizationDialect>();
return options;
}