| 1 | //===- TestOneShotModuleBufferzation.cpp - Bufferization Test -----*- c++ |
| 2 | //-*-===// |
| 3 | // |
| 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "mlir/Dialect/Bufferization/IR/Bufferization.h" |
| 11 | #include "mlir/Dialect/Bufferization/Transforms/Bufferize.h" |
| 12 | #include "mlir/Dialect/Bufferization/Transforms/OneShotModuleBufferize.h" |
| 13 | #include "mlir/Dialect/Bufferization/Transforms/Transforms.h" |
| 14 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 15 | #include "mlir/Pass/Pass.h" |
| 16 | |
| 17 | #include "TestAttributes.h" // TestTensorEncodingAttr, TestMemRefLayoutAttr |
| 18 | #include "TestOpsInterfaces.h" |
| 19 | |
| 20 | #include "llvm/ADT/TypeSwitch.h" |
| 21 | |
| 22 | using namespace mlir; |
| 23 | |
| 24 | namespace { |
| 25 | MemRefLayoutAttrInterface |
| 26 | getMemRefLayoutForTensorEncoding(RankedTensorType tensorType) { |
| 27 | if (auto encoding = dyn_cast_if_present<test::TestTensorEncodingAttr>( |
| 28 | tensorType.getEncoding())) { |
| 29 | return cast<MemRefLayoutAttrInterface>(test::TestMemRefLayoutAttr::get( |
| 30 | tensorType.getContext(), encoding.getDummy())); |
| 31 | } |
| 32 | return {}; |
| 33 | } |
| 34 | |
| 35 | test::TestMemRefLayoutAttr |
| 36 | getLayoutFromBuffer(bufferization::BufferLikeType buffer) { |
| 37 | auto layout = |
| 38 | llvm::TypeSwitch<bufferization::BufferLikeType, |
| 39 | MemRefLayoutAttrInterface>(buffer) |
| 40 | .Case(caseFn: [&](MemRefType memref) { return memref.getLayout(); }) |
| 41 | .Case(caseFn: [&](test::TestMemrefType testMemref) { |
| 42 | return cast<MemRefLayoutAttrInterface>(testMemref.getLayout()); |
| 43 | }) |
| 44 | .Default(defaultFn: [](bufferization::BufferLikeType) { |
| 45 | return MemRefLayoutAttrInterface(); |
| 46 | }); |
| 47 | return dyn_cast_or_null<test::TestMemRefLayoutAttr>(layout); |
| 48 | } |
| 49 | |
| 50 | struct TestOneShotModuleBufferizePass |
| 51 | : public PassWrapper<TestOneShotModuleBufferizePass, OperationPass<>> { |
| 52 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestOneShotModuleBufferizePass) |
| 53 | |
| 54 | TestOneShotModuleBufferizePass() = default; |
| 55 | TestOneShotModuleBufferizePass(const TestOneShotModuleBufferizePass &pass) |
| 56 | : PassWrapper(pass) {} |
| 57 | |
| 58 | void getDependentDialects(DialectRegistry ®istry) const override { |
| 59 | registry.insert<test::TestDialect>(); |
| 60 | registry.insert<bufferization::BufferizationDialect>(); |
| 61 | } |
| 62 | StringRef getArgument() const final { |
| 63 | return "test-one-shot-module-bufferize" ; |
| 64 | } |
| 65 | StringRef getDescription() const final { |
| 66 | return "Pass to test One Shot Module Bufferization" ; |
| 67 | } |
| 68 | |
| 69 | Option<bool> allowReturnAllocsFromLoops{ |
| 70 | *this, "allow-return-allocs-from-loops" , |
| 71 | llvm::cl::desc("Allows returning/yielding new allocations from a loop." ), |
| 72 | llvm::cl::init(Val: false)}; |
| 73 | Option<bool> testTensorToMemRef{ |
| 74 | *this, "test-tensor-to-memref" , |
| 75 | llvm::cl::desc("Convert test tensors to builtin memrefs." ), |
| 76 | llvm::cl::init(Val: false)}; |
| 77 | Option<bool> testTensorToDynamicMemRef{ |
| 78 | *this, "test-tensor-to-dynamic-memref" , |
| 79 | llvm::cl::desc("Convert test tensors to builtin memrefs with dynamic " |
| 80 | "extents." ), |
| 81 | llvm::cl::init(Val: false)}; |
| 82 | |
| 83 | void runOnOperation() override { |
| 84 | |
| 85 | llvm::errs() << "Running TestOneShotModuleBufferize on: " |
| 86 | << getOperation()->getName() << "\n" ; |
| 87 | bufferization::OneShotBufferizationOptions opt; |
| 88 | |
| 89 | opt.bufferizeFunctionBoundaries = true; |
| 90 | opt.allowReturnAllocsFromLoops = allowReturnAllocsFromLoops; |
| 91 | opt.functionArgTypeConverterFn = |
| 92 | [&](bufferization::TensorLikeType tensor, Attribute memSpace, |
| 93 | func::FuncOp, const bufferization::BufferizationOptions &options) { |
| 94 | return options.unknownTypeConverterFn(tensor, memSpace, options); |
| 95 | }; |
| 96 | opt.unknownTypeConverterFn = |
| 97 | [&](bufferization::TensorLikeType tensor, Attribute memSpace, |
| 98 | const bufferization::BufferizationOptions &options) { |
| 99 | return llvm::TypeSwitch<bufferization::TensorLikeType, |
| 100 | bufferization::BufferLikeType>(tensor) |
| 101 | .Case(caseFn: [&](UnrankedTensorType unrankedTensorType) { |
| 102 | return cast<bufferization::BufferLikeType>( |
| 103 | Val: UnrankedMemRefType::get(elementType: unrankedTensorType.getElementType(), |
| 104 | memorySpace: memSpace)); |
| 105 | }) |
| 106 | .Case(caseFn: [&](RankedTensorType rankedTensorType) { |
| 107 | // Note: builtin ranked tensor with custom encoding to layout |
| 108 | // conversion. |
| 109 | auto layout = |
| 110 | getMemRefLayoutForTensorEncoding(tensorType: rankedTensorType); |
| 111 | return cast<bufferization::BufferLikeType>(Val: MemRefType::get( |
| 112 | shape: rankedTensorType.getShape(), |
| 113 | elementType: rankedTensorType.getElementType(), layout, memorySpace: memSpace)); |
| 114 | }) |
| 115 | .Case(caseFn: [&](test::TestTensorType testTensorType) |
| 116 | -> bufferization::BufferLikeType { |
| 117 | if (testTensorToDynamicMemRef) |
| 118 | return cast<bufferization::BufferLikeType>(MemRefType::get( |
| 119 | SmallVector<int64_t>(testTensorType.getRank(), |
| 120 | ShapedType::kDynamic), |
| 121 | testTensorType.getElementType(), |
| 122 | MemRefLayoutAttrInterface{}, memSpace)); |
| 123 | if (testTensorToMemRef) |
| 124 | return cast<bufferization::BufferLikeType>( |
| 125 | MemRefType::get(testTensorType.getShape(), |
| 126 | testTensorType.getElementType(), |
| 127 | MemRefLayoutAttrInterface{}, memSpace)); |
| 128 | assert(memSpace == nullptr && |
| 129 | "memory space is not supported for test types" ); |
| 130 | // Note: unknown type conversion cannot create layout because |
| 131 | // test tensor does not have an encoding, this is probably |
| 132 | // enough for a test. |
| 133 | return test::TestMemrefType::get( |
| 134 | testTensorType.getContext(), testTensorType.getShape(), |
| 135 | testTensorType.getElementType(), /*layout=*/nullptr); |
| 136 | }) |
| 137 | .Case(caseFn: [&](test::TestTensorLikeWrapperType wrapper) { |
| 138 | return options.unknownTypeConverterFn( |
| 139 | cast<bufferization::TensorLikeType>(wrapper.getInner()), |
| 140 | memSpace, options); |
| 141 | }) |
| 142 | .Default(defaultFn: [&](bufferization::TensorLikeType tensor) { |
| 143 | llvm_unreachable("unexpected tensor type" ); |
| 144 | return bufferization::BufferLikeType{}; |
| 145 | }); |
| 146 | }; |
| 147 | // A simple yet distinct (from upstream) policy: compare layouts and return |
| 148 | // "smaller" one. |
| 149 | opt.reconcileBufferTypeMismatchFn = |
| 150 | [](bufferization::BufferLikeType x, bufferization::BufferLikeType y, |
| 151 | const bufferization::BufferizationOptions &) |
| 152 | -> FailureOr<bufferization::BufferLikeType> { |
| 153 | auto lhsLayout = getLayoutFromBuffer(x); |
| 154 | auto rhsLayout = getLayoutFromBuffer(y); |
| 155 | if (lhsLayout && rhsLayout) { |
| 156 | return lhsLayout.getDummy().getValue() <= |
| 157 | rhsLayout.getDummy().getValue() |
| 158 | ? x |
| 159 | : y; |
| 160 | } |
| 161 | return rhsLayout ? y : x; |
| 162 | }; |
| 163 | opt.castFn = [&](OpBuilder &b, Location loc, Type dest, Value value) { |
| 164 | return test::TestDummyCastOp::create(b, loc, dest, value).getResult(); |
| 165 | }; |
| 166 | // Function signature update only works with memref.cast. Disable it to |
| 167 | // align behaviour for upstream and user casts. |
| 168 | opt.inferFunctionResultLayout = false; |
| 169 | |
| 170 | bufferization::BufferizationState bufferizationState; |
| 171 | |
| 172 | if (failed(Result: bufferization::runOneShotModuleBufferize(moduleOp: getOperation(), options: opt, |
| 173 | state&: bufferizationState))) |
| 174 | signalPassFailure(); |
| 175 | } |
| 176 | }; |
| 177 | } // namespace |
| 178 | |
| 179 | namespace mlir::test { |
| 180 | void registerTestOneShotModuleBufferizePass() { |
| 181 | PassRegistration<TestOneShotModuleBufferizePass>(); |
| 182 | } |
| 183 | } // namespace mlir::test |
| 184 | |