#include "mlir/Dialect/Bufferization/Transforms/FuncBufferizableOpInterfaceImpl.h"
#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
#include "mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/Operation.h"
namespace mlir {
namespace bufferization {
namespace func_ext {
void FuncAnalysisState::startFunctionAnalysis(FuncOp funcOp) {
analyzedFuncOps[funcOp] = FuncOpAnalysisState::InProgress;
auto createdEquiv = equivalentFuncArgs.try_emplace(funcOp, IndexMapping());
auto createdAliasingOperands =
aliasingFuncArgs.try_emplace(funcOp, IndexToIndexListMapping());
auto createdAliasingResults =
aliasingReturnVals.try_emplace(funcOp, IndexToIndexListMapping());
auto createdRead = readBbArgs.try_emplace(funcOp, BbArgIndexSet());
auto createdWritten = writtenBbArgs.try_emplace(funcOp, BbArgIndexSet());
(void)createdEquiv;
(void)createdAliasingOperands;
(void)createdAliasingResults;
(void)createdRead;
(void)createdWritten;
#ifndef NDEBUG
assert(createdEquiv.second && "equivalence info exists already");
assert(createdAliasingOperands.second && "aliasing info exists already");
assert(createdAliasingResults.second && "aliasing info exists already");
assert(createdRead.second && "bbarg access info exists already");
assert(createdWritten.second && "bbarg access info exists already");
#endif
}
static func::ReturnOp getAssumedUniqueReturnOp(FuncOp funcOp) {
func::ReturnOp returnOp;
for (Block &b : funcOp.getBody()) {
if (auto candidateOp = dyn_cast<func::ReturnOp>(b.getTerminator())) {
if (returnOp)
return nullptr;
returnOp = candidateOp;
}
}
return returnOp;
}
static BaseMemRefType
getBufferizedFunctionArgType(FuncOp funcOp, int64_t index,
const BufferizationOptions &options) {
auto tensorType =
funcOp.getFunctionType().getInput(index).dyn_cast<TensorType>();
assert(tensorType && "expected TensorType");
BaseMemRefType memrefType;
if (options.functionBoundaryTypeConversion ==
BufferizationOptions::LayoutMapOption::IdentityLayoutMap) {
memrefType = getMemRefTypeWithStaticIdentityLayout(tensorType);
} else {
memrefType = getMemRefTypeWithFullyDynamicLayout(tensorType);
}
auto layoutAttr = funcOp.getArgAttrOfType<AffineMapAttr>(
index, BufferizationDialect::kBufferLayoutAttrName);
if (!layoutAttr)
return memrefType;
auto rankedMemrefType = memrefType.dyn_cast<MemRefType>();
assert(rankedMemrefType && "buffer layout not supported on unranked tensors");
return MemRefType::get(
rankedMemrefType.getShape(), rankedMemrefType.getElementType(),
layoutAttr.getValue(), rankedMemrefType.getMemorySpaceAsInt());
}
static FuncOp getCalledFunction(CallOpInterface callOp) {
SymbolRefAttr sym = callOp.getCallableForCallee().dyn_cast<SymbolRefAttr>();
if (!sym)
return nullptr;
return dyn_cast_or_null<FuncOp>(
SymbolTable::lookupNearestSymbolFrom(callOp, sym));
}
static const FuncAnalysisState &
getFuncAnalysisState(const AnalysisState &state) {
Optional<const FuncAnalysisState *> maybeState =
state.getDialectState<FuncAnalysisState>(
func::FuncDialect::getDialectNamespace());
assert(maybeState && "FuncAnalysisState does not exist");
return **maybeState;
}
static FuncOpAnalysisState getFuncOpAnalysisState(const AnalysisState &state,
FuncOp funcOp) {
Optional<const FuncAnalysisState *> maybeState =
state.getDialectState<FuncAnalysisState>(
func::FuncDialect::getDialectNamespace());
if (!maybeState.has_value())
return FuncOpAnalysisState::NotAnalyzed;
const auto &analyzedFuncOps = maybeState.value()->analyzedFuncOps;
auto it = analyzedFuncOps.find(funcOp);
if (it == analyzedFuncOps.end())
return FuncOpAnalysisState::NotAnalyzed;
return it->second;
}
static Optional<int64_t> getEquivalentFuncArgIdx(FuncOp funcOp,
const FuncAnalysisState &state,
int64_t returnValIdx) {
auto funcOpIt = state.equivalentFuncArgs.find(funcOp);
if (funcOpIt == state.equivalentFuncArgs.end())
return None;
auto retValIt = funcOpIt->getSecond().find(returnValIdx);
if (retValIt == funcOpIt->getSecond().end())
return None;
return retValIt->getSecond();
}
struct CallOpInterface
: public BufferizableOpInterface::ExternalModel<CallOpInterface,
func::CallOp> {
bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
const AnalysisState &state) const {
func::CallOp callOp = cast<func::CallOp>(op);
FuncOp funcOp = getCalledFunction(callOp);
assert(funcOp && "expected CallOp to a FuncOp");
if (getFuncOpAnalysisState(state, funcOp) != FuncOpAnalysisState::Analyzed)
return true;
const FuncAnalysisState &funcState = getFuncAnalysisState(state);
return funcState.readBbArgs.lookup(funcOp).contains(
opOperand.getOperandNumber());
}
bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
const AnalysisState &state) const {
func::CallOp callOp = cast<func::CallOp>(op);
FuncOp funcOp = getCalledFunction(callOp);
assert(funcOp && "expected CallOp to a FuncOp");
if (getFuncOpAnalysisState(state, funcOp) != FuncOpAnalysisState::Analyzed)
return true;
const FuncAnalysisState &funcState = getFuncAnalysisState(state);
return funcState.writtenBbArgs.lookup(funcOp).contains(
opOperand.getOperandNumber());
}
SmallVector<OpResult> getAliasingOpResult(Operation *op, OpOperand &opOperand,
const AnalysisState &state) const {
func::CallOp callOp = cast<func::CallOp>(op);
FuncOp funcOp = getCalledFunction(callOp);
assert(funcOp && "expected CallOp to a FuncOp");
if (getFuncOpAnalysisState(state, funcOp) !=
FuncOpAnalysisState::Analyzed) {
SmallVector<OpResult> result;
for (OpResult opResult : op->getOpResults())
if (opResult.getType().isa<TensorType>())
result.push_back(opResult);
return result;
}
const FuncAnalysisState &funcState = getFuncAnalysisState(state);
auto aliasingReturnVals =
funcState.aliasingReturnVals.lookup(funcOp).lookup(
opOperand.getOperandNumber());
SmallVector<OpResult> result;
for (int64_t resultIdx : aliasingReturnVals)
result.push_back(callOp->getOpResult(resultIdx));
return result;
}
SmallVector<OpOperand *>
getAliasingOpOperand(Operation *op, OpResult opResult,
const AnalysisState &state) const {
func::CallOp callOp = cast<func::CallOp>(op);
FuncOp funcOp = getCalledFunction(callOp);
assert(funcOp && "expected CallOp to a FuncOp");
if (getFuncOpAnalysisState(state, funcOp) !=
FuncOpAnalysisState::Analyzed) {
SmallVector<OpOperand *> result;
for (OpOperand &opOperand : op->getOpOperands())
if (opOperand.get().getType().isa<TensorType>())
result.push_back(&opOperand);
return result;
}
const FuncAnalysisState &funcState = getFuncAnalysisState(state);
auto aliasingFuncArgs = funcState.aliasingFuncArgs.lookup(funcOp).lookup(
opResult.getResultNumber());
SmallVector<OpOperand *> result;
for (int64_t bbArgIdx : aliasingFuncArgs)
result.push_back(&callOp->getOpOperand(bbArgIdx));
return result;
}
BufferRelation bufferRelation(Operation *op, OpResult opResult,
const AnalysisState &state) const {
func::CallOp callOp = cast<func::CallOp>(op);
FuncOp funcOp = getCalledFunction(callOp);
assert(funcOp && "expected CallOp to a FuncOp");
if (getFuncOpAnalysisState(state, funcOp) !=
FuncOpAnalysisState::Analyzed) {
return BufferRelation::None;
}
const FuncAnalysisState &funcState = getFuncAnalysisState(state);
Optional<int64_t> maybeEquiv =
getEquivalentFuncArgIdx(funcOp, funcState, opResult.getResultNumber());
if (maybeEquiv) {
#ifndef NDEBUG
SmallVector<OpOperand *> aliasingOpOperands =
getAliasingOpOperand(op, opResult, state);
assert(aliasingOpOperands.size() == 1 &&
"expected exactly 1 aliasing OpOperand");
assert(aliasingOpOperands.front()->getOperandNumber() == *maybeEquiv &&
"inconsistent analysis state");
#endif
return BufferRelation::Equivalent;
}
return BufferRelation::None;
}
LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
const BufferizationOptions &options) const {
func::CallOp callOp = cast<func::CallOp>(op);
unsigned numResults = callOp.getNumResults();
unsigned numOperands = callOp->getNumOperands();
FuncOp funcOp = getCalledFunction(callOp);
assert(funcOp && "expected CallOp to a FuncOp");
FunctionType funcType = funcOp.getFunctionType();
SmallVector<Type> resultTypes;
SmallVector<Value> replacementValues(numResults, Value());
SmallVector<Optional<unsigned>> retValMapping(numResults, None);
SmallVector<Value> newOperands(numOperands, Value());
for (const auto &it : llvm::enumerate(callOp.getResultTypes())) {
unsigned returnValIdx = it.index();
Type returnType = it.value();
if (!returnType.isa<TensorType>()) {
retValMapping[returnValIdx] = resultTypes.size();
resultTypes.push_back(returnType);
continue;
}
retValMapping[returnValIdx] = resultTypes.size();
resultTypes.push_back(funcType.getResult(resultTypes.size()));
}
for (OpOperand &opOperand : callOp->getOpOperands()) {
unsigned idx = opOperand.getOperandNumber();
Value tensorOperand = opOperand.get();
if (!tensorOperand.getType().isa<TensorType>()) {
newOperands[idx] = tensorOperand;
continue;
}
Value buffer = newOperands[idx];
if (!buffer) {
FailureOr<Value> maybeBuffer =
getBuffer(rewriter, opOperand.get(), options);
if (failed(maybeBuffer))
return failure();
buffer = *maybeBuffer;
}
auto memRefType = funcType.getInput(idx);
if (buffer.getType() != memRefType) {
assert(
memref::CastOp::areCastCompatible(buffer.getType(), memRefType) &&
"CallOp::bufferize: cast incompatible");
Value castBuffer = rewriter.create<memref::CastOp>(callOp.getLoc(),
memRefType, buffer);
buffer = castBuffer;
}
newOperands[idx] = buffer;
}
Operation *newCallOp = rewriter.create<func::CallOp>(
callOp.getLoc(), funcOp.getSymName(), resultTypes, newOperands);
newCallOp->setAttrs(callOp->getAttrs());
for (unsigned i = 0; i < replacementValues.size(); ++i) {
if (replacementValues[i])
continue;
replacementValues[i] = newCallOp->getResult(*retValMapping[i]);
}
replaceOpWithBufferizedValues(rewriter, callOp, replacementValues);
return success();
}
};
struct ReturnOpInterface
: public BufferizableOpInterface::ExternalModel<ReturnOpInterface,
func::ReturnOp> {
bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
const AnalysisState &state) const {
return true;
}
bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
const AnalysisState &state) const {
return false;
}
SmallVector<OpResult> getAliasingOpResult(Operation *op, OpOperand &opOperand,
const AnalysisState &state) const {
return {};
}
LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
const BufferizationOptions &options) const {
#ifndef NDEBUG
auto returnOp = cast<func::ReturnOp>(op);
assert(isa<FuncOp>(returnOp->getParentOp()) &&
"only support FuncOp parent for ReturnOp");
#endif
return success();
}
};
struct FuncOpInterface
: public BufferizableOpInterface::ExternalModel<FuncOpInterface, FuncOp> {
LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
const BufferizationOptions &options) const {
auto funcOp = cast<FuncOp>(op);
FunctionType funcType = funcOp.getFunctionType();
SmallVector<Type> argTypes;
for (const auto &it : llvm::enumerate(funcType.getInputs())) {
Type argType = it.value();
if (auto tensorType = argType.dyn_cast<TensorType>()) {
argTypes.push_back(
getBufferizedFunctionArgType(funcOp, it.index(), options));
continue;
}
argTypes.push_back(argType);
}
if (funcOp.getBody().empty()) {
SmallVector<Type> retTypes;
for (Type resultType : funcType.getResults()) {
if (resultType.isa<TensorType>())
return funcOp->emitError() << "cannot bufferize bodiless function "
<< "that returns a tensor";
retTypes.push_back(resultType);
}
funcOp.setType(FunctionType::get(op->getContext(), argTypes, retTypes));
return success();
}
func::ReturnOp returnOp = getAssumedUniqueReturnOp(funcOp);
assert(returnOp && "expected func with single return op");
Location loc = returnOp.getLoc();
Block &frontBlock = funcOp.getBody().front();
for (BlockArgument &bbArg : frontBlock.getArguments()) {
auto tensorType = bbArg.getType().dyn_cast<TensorType>();
if (!tensorType)
continue;
SmallVector<OpOperand *> bbArgUses;
for (OpOperand &use : bbArg.getUses())
bbArgUses.push_back(&use);
Type memrefType =
getBufferizedFunctionArgType(funcOp, bbArg.getArgNumber(), options);
bbArg.setType(memrefType);
rewriter.setInsertionPointToStart(&frontBlock);
if (!bbArgUses.empty()) {
Value toTensorOp =
rewriter.create<bufferization::ToTensorOp>(funcOp.getLoc(), bbArg);
for (OpOperand *use : bbArgUses)
use->set(toTensorOp);
}
}
SmallVector<Value> returnValues;
for (OpOperand &returnOperand : returnOp->getOpOperands()) {
Value returnVal = returnOperand.get();
auto tensorType = returnVal.getType().dyn_cast<TensorType>();
rewriter.setInsertionPoint(returnOp);
if (!tensorType) {
returnValues.push_back(returnVal);
continue;
}
BaseMemRefType resultType;
if (options.functionBoundaryTypeConversion ==
BufferizationOptions::LayoutMapOption::IdentityLayoutMap) {
resultType = getMemRefTypeWithStaticIdentityLayout(tensorType);
} else {
resultType = getMemRefTypeWithFullyDynamicLayout(tensorType);
}
Value toMemrefOp = rewriter.create<bufferization::ToMemrefOp>(
loc, resultType, returnVal);
returnValues.push_back(toMemrefOp);
}
returnOp.operandsMutable().assign(returnValues);
funcOp.setType(FunctionType::get(op->getContext(), argTypes,
ValueRange(returnValues).getTypes()));
return success();
}
bool isWritable(Operation *op, Value value,
const AnalysisState &state) const {
auto funcOp = cast<FuncOp>(op);
BlockArgument bbArg = value.dyn_cast<BlockArgument>();
assert(bbArg && "expected BlockArgument");
if (BoolAttr writable = funcOp.getArgAttrOfType<BoolAttr>(
bbArg.getArgNumber(), BufferizationDialect::kWritableAttrName))
return writable.getValue();
return true;
}
};
}
}
}
void mlir::bufferization::func_ext::
registerBufferizableOpInterfaceExternalModels(DialectRegistry ®istry) {
registry.addExtension(+[](MLIRContext *ctx, func::FuncDialect *dialect) {
func::CallOp::attachInterface<func_ext::CallOpInterface>(*ctx);
func::FuncOp::attachInterface<func_ext::FuncOpInterface>(*ctx);
func::ReturnOp::attachInterface<func_ext::ReturnOpInterface>(*ctx);
});
}