| 1 | //===- TestStridedMetadataRangeAnalysis.cpp - Test strided md analysis ----===// |
| 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 | #include "mlir/Analysis/DataFlow/ConstantPropagationAnalysis.h" |
| 10 | #include "mlir/Analysis/DataFlow/DeadCodeAnalysis.h" |
| 11 | #include "mlir/Analysis/DataFlow/IntegerRangeAnalysis.h" |
| 12 | #include "mlir/Analysis/DataFlow/StridedMetadataRangeAnalysis.h" |
| 13 | #include "mlir/Analysis/DataFlowFramework.h" |
| 14 | #include "mlir/IR/BuiltinAttributes.h" |
| 15 | #include "mlir/IR/Operation.h" |
| 16 | #include "mlir/Pass/Pass.h" |
| 17 | #include "mlir/Pass/PassRegistry.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | |
| 21 | using namespace mlir; |
| 22 | using namespace mlir::dataflow; |
| 23 | |
| 24 | static void printAnalysisResults(DataFlowSolver &solver, Operation *op, |
| 25 | raw_ostream &os) { |
| 26 | // Collect the strided metadata of the op results. |
| 27 | SmallVector<std::pair<unsigned, const StridedMetadataRangeLattice *>> results; |
| 28 | for (OpResult result : op->getResults()) { |
| 29 | const auto *state = solver.lookupState<StridedMetadataRangeLattice>(anchor: result); |
| 30 | // Skip the result if it's uninitialized. |
| 31 | if (!state || state->getValue().isUninitialized()) |
| 32 | continue; |
| 33 | |
| 34 | // Skip the result if the range is empty. |
| 35 | const mlir::StridedMetadataRange &md = state->getValue(); |
| 36 | if (md.getOffsets().empty() && md.getSizes().empty() && |
| 37 | md.getStrides().empty()) |
| 38 | continue; |
| 39 | results.push_back(Elt: {result.getResultNumber(), state}); |
| 40 | } |
| 41 | |
| 42 | // Early exit if there's no metadata to print. |
| 43 | if (results.empty()) |
| 44 | return; |
| 45 | |
| 46 | // Print the metadata. |
| 47 | os << "Op: " << OpWithFlags(op, OpPrintingFlags().skipRegions()) << "\n" ; |
| 48 | for (auto [idx, state] : results) |
| 49 | os << " result[" << idx << "]: " << state->getValue() << "\n" ; |
| 50 | os << "\n" ; |
| 51 | } |
| 52 | |
| 53 | namespace { |
| 54 | struct TestStridedMetadataRangeAnalysisPass |
| 55 | : public PassWrapper<TestStridedMetadataRangeAnalysisPass, |
| 56 | OperationPass<>> { |
| 57 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID( |
| 58 | TestStridedMetadataRangeAnalysisPass) |
| 59 | |
| 60 | StringRef getArgument() const override { |
| 61 | return "test-strided-metadata-range-analysis" ; |
| 62 | } |
| 63 | void runOnOperation() override { |
| 64 | Operation *op = getOperation(); |
| 65 | |
| 66 | DataFlowSolver solver; |
| 67 | solver.load<DeadCodeAnalysis>(); |
| 68 | solver.load<SparseConstantPropagation>(); |
| 69 | solver.load<IntegerRangeAnalysis>(); |
| 70 | solver.load<StridedMetadataRangeAnalysis>(); |
| 71 | if (failed(Result: solver.initializeAndRun(top: op))) |
| 72 | return signalPassFailure(); |
| 73 | |
| 74 | op->walk( |
| 75 | callback: [&](Operation *op) { printAnalysisResults(solver, op, os&: llvm::errs()); }); |
| 76 | } |
| 77 | }; |
| 78 | } // end anonymous namespace |
| 79 | |
| 80 | namespace mlir { |
| 81 | namespace test { |
| 82 | void registerTestStridedMetadataRangeAnalysisPass() { |
| 83 | PassRegistration<TestStridedMetadataRangeAnalysisPass>(); |
| 84 | } |
| 85 | } // end namespace test |
| 86 | } // end namespace mlir |
| 87 | |