#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
#include "mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h"
#include "mlir/Dialect/Bufferization/Transforms/Transforms.h"
#include "mlir/Pass/Pass.h"
using namespace mlir;
namespace {
struct TestTensorCopyInsertionPass
: public PassWrapper<TestTensorCopyInsertionPass, OperationPass<ModuleOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestTensorCopyInsertionPass)
TestTensorCopyInsertionPass() = default;
TestTensorCopyInsertionPass(const TestTensorCopyInsertionPass &pass)
: PassWrapper(pass) {}
void getDependentDialects(DialectRegistry ®istry) const override {
registry.insert<bufferization::BufferizationDialect>();
}
StringRef getArgument() const final { return "test-tensor-copy-insertion"; }
StringRef getDescription() const final {
return "Module pass to test Tensor Copy Insertion";
}
void runOnOperation() override {
bufferization::OneShotBufferizationOptions options;
options.allowReturnAllocsFromLoops = allowReturnAllocsFromLoops;
options.bufferizeFunctionBoundaries = bufferizeFunctionBoundaries;
if (mustInferMemorySpace) {
options.defaultMemorySpaceFn =
[](TensorType t) -> std::optional<Attribute> { return std::nullopt; };
}
if (failed(bufferization::insertTensorCopies(getOperation(), options)))
signalPassFailure();
}
Option<bool> allowReturnAllocsFromLoops{
*this, "allow-return-allocs-from-loops",
llvm::cl::desc("Allows returning/yielding new allocations from a loop."),
llvm::cl::init(false)};
Option<bool> bufferizeFunctionBoundaries{
*this, "bufferize-function-boundaries",
llvm::cl::desc("Bufferize function boundaries."), llvm::cl::init(false)};
Option<bool> mustInferMemorySpace{
*this, "must-infer-memory-space",
llvm::cl::desc(
"The memory space of an memref types must always be inferred. If "
"unset, a default memory space of 0 is used otherwise."),
llvm::cl::init(false)};
};
}
namespace mlir::test {
void registerTestTensorCopyInsertionPass() {
PassRegistration<TestTensorCopyInsertionPass>();
}
}