| 1 | //===- TestTransformsOps.cpp - Test Transforms ----------------------------===// |
| 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-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines transform dialect operations for testing MLIR |
| 10 | // transformations |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Dialect/Affine/IR/AffineOps.h" |
| 15 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 16 | #include "mlir/Dialect/Transform/IR/TransformDialect.h" |
| 17 | #include "mlir/Dialect/Transform/Interfaces/TransformInterfaces.h" |
| 18 | #include "mlir/Dialect/Utils/StaticValueUtils.h" |
| 19 | #include "mlir/IR/OpDefinition.h" |
| 20 | #include "mlir/Transforms/RegionUtils.h" |
| 21 | |
| 22 | #define GET_OP_CLASSES |
| 23 | #include "TestTransformsOps.h.inc" |
| 24 | |
| 25 | using namespace mlir; |
| 26 | using namespace mlir::transform; |
| 27 | |
| 28 | #define GET_OP_CLASSES |
| 29 | #include "TestTransformsOps.cpp.inc" |
| 30 | |
| 31 | DiagnosedSilenceableFailure |
| 32 | transform::TestMoveOperandDeps::apply(TransformRewriter &rewriter, |
| 33 | TransformResults &TransformResults, |
| 34 | TransformState &state) { |
| 35 | Operation *op = *state.getPayloadOps(getOp()).begin(); |
| 36 | Operation *moveBefore = *state.getPayloadOps(getInsertionPoint()).begin(); |
| 37 | if (failed(moveOperationDependencies(rewriter, op, moveBefore))) { |
| 38 | auto *listener = |
| 39 | cast<ErrorCheckingTrackingListener>(rewriter.getListener()); |
| 40 | std::string errorMsg = listener->getLatestMatchFailureMessage(); |
| 41 | (void)emitRemark(errorMsg); |
| 42 | } |
| 43 | return DiagnosedSilenceableFailure::success(); |
| 44 | } |
| 45 | |
| 46 | DiagnosedSilenceableFailure |
| 47 | transform::TestMoveValueDefns::apply(TransformRewriter &rewriter, |
| 48 | TransformResults &TransformResults, |
| 49 | TransformState &state) { |
| 50 | SmallVector<Value> values; |
| 51 | for (auto tdValue : getValues()) { |
| 52 | values.push_back(*state.getPayloadValues(tdValue).begin()); |
| 53 | } |
| 54 | Operation *moveBefore = *state.getPayloadOps(getInsertionPoint()).begin(); |
| 55 | if (failed(moveValueDefinitions(rewriter, values, moveBefore))) { |
| 56 | auto *listener = |
| 57 | cast<ErrorCheckingTrackingListener>(rewriter.getListener()); |
| 58 | std::string errorMsg = listener->getLatestMatchFailureMessage(); |
| 59 | (void)emitRemark(errorMsg); |
| 60 | } |
| 61 | return DiagnosedSilenceableFailure::success(); |
| 62 | } |
| 63 | |
| 64 | //===----------------------------------------------------------------------===// |
| 65 | // Test affine functionality. |
| 66 | //===----------------------------------------------------------------------===// |
| 67 | DiagnosedSilenceableFailure |
| 68 | transform::TestMakeComposedFoldedAffineApply::applyToOne( |
| 69 | TransformRewriter &rewriter, affine::AffineApplyOp affineApplyOp, |
| 70 | ApplyToEachResultList &results, TransformState &state) { |
| 71 | Location loc = affineApplyOp.getLoc(); |
| 72 | OpFoldResult ofr = affine::makeComposedFoldedAffineApply( |
| 73 | rewriter, loc, affineApplyOp.getAffineMap(), |
| 74 | getAsOpFoldResult(affineApplyOp.getOperands()), |
| 75 | /*composeAffineMin=*/true); |
| 76 | Value result; |
| 77 | if (auto v = dyn_cast<Value>(ofr)) { |
| 78 | result = v; |
| 79 | } else { |
| 80 | result = arith::ConstantIndexOp::create(rewriter, loc, |
| 81 | getConstantIntValue(ofr).value()); |
| 82 | } |
| 83 | results.push_back(result.getDefiningOp()); |
| 84 | rewriter.replaceOp(affineApplyOp, result); |
| 85 | return DiagnosedSilenceableFailure::success(); |
| 86 | } |
| 87 | |
| 88 | //===----------------------------------------------------------------------===// |
| 89 | // Extension |
| 90 | //===----------------------------------------------------------------------===// |
| 91 | namespace { |
| 92 | |
| 93 | class TestTransformsDialectExtension |
| 94 | : public transform::TransformDialectExtension< |
| 95 | TestTransformsDialectExtension> { |
| 96 | public: |
| 97 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestTransformsDialectExtension) |
| 98 | |
| 99 | using Base::Base; |
| 100 | |
| 101 | void init() { |
| 102 | registerTransformOps< |
| 103 | #define GET_OP_LIST |
| 104 | #include "TestTransformsOps.cpp.inc" |
| 105 | >(); |
| 106 | } |
| 107 | }; |
| 108 | } // namespace |
| 109 | |
| 110 | namespace test { |
| 111 | void registerTestTransformsTransformDialectExtension( |
| 112 | DialectRegistry ®istry) { |
| 113 | registry.addExtensions<TestTransformsDialectExtension>(); |
| 114 | } |
| 115 | } // namespace test |
| 116 | |