| 1 | //===- TestSlice.cpp - Test slice related analisis ------------------------===// |
|---|---|
| 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/SliceWalk.h" |
| 10 | #include "mlir/Analysis/TopologicalSortUtils.h" |
| 11 | #include "mlir/IR/BuiltinTypes.h" |
| 12 | #include "mlir/IR/SymbolTable.h" |
| 13 | #include "mlir/Interfaces/ControlFlowInterfaces.h" |
| 14 | #include "mlir/Interfaces/FunctionInterfaces.h" |
| 15 | #include "mlir/Pass/Pass.h" |
| 16 | |
| 17 | using namespace mlir; |
| 18 | |
| 19 | static const StringLiteral kToSortMark = "test_to_sort"; |
| 20 | static const StringLiteral kOrderIndex = "test_sort_index"; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | struct TestTopologicalSortPass |
| 25 | : public PassWrapper<TestTopologicalSortPass, |
| 26 | InterfacePass<SymbolOpInterface>> { |
| 27 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestTopologicalSortPass) |
| 28 | |
| 29 | StringRef getArgument() const final { return "test-print-topological-sort"; } |
| 30 | StringRef getDescription() const final { |
| 31 | return "Sorts operations topologically and attaches attributes with their " |
| 32 | "corresponding index in the ordering to them"; |
| 33 | } |
| 34 | void runOnOperation() override { |
| 35 | SetVector<Operation *> toSort; |
| 36 | getOperation().walk(callback: [&](Operation *op) { |
| 37 | if (op->hasDiscardableAttrOfType<UnitAttr>(name: kToSortMark)) |
| 38 | toSort.insert(X: op); |
| 39 | }); |
| 40 | |
| 41 | auto i32Type = IntegerType::get(context: &getContext(), width: 32); |
| 42 | SetVector<Operation *> sortedOps = topologicalSort(toSort); |
| 43 | for (auto [index, op] : llvm::enumerate(First&: sortedOps)) |
| 44 | op->setDiscardableAttr(name: kOrderIndex, value: IntegerAttr::get(type: i32Type, value: index)); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | /// Pass to test getControlFlowPredecessors from SliceWalk. |
| 49 | struct TestControlFlowPredecessorsPass |
| 50 | : public PassWrapper<TestControlFlowPredecessorsPass, |
| 51 | InterfacePass<FunctionOpInterface>> { |
| 52 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestControlFlowPredecessorsPass) |
| 53 | |
| 54 | StringRef getArgument() const final { |
| 55 | return "test-control-flow-predecessors"; |
| 56 | } |
| 57 | StringRef getDescription() const final { |
| 58 | return "Test getControlFlowPredecessors from SliceWalk."; |
| 59 | } |
| 60 | |
| 61 | void runOnOperation() override { |
| 62 | FunctionOpInterface func = getOperation(); |
| 63 | llvm::errs() << "Control flow predecessors for '"<< func.getNameAttr() |
| 64 | << "':\n"; |
| 65 | func->walk(callback: [](Operation *op) { |
| 66 | if (!isa<RegionBranchOpInterface>(Val: op)) |
| 67 | return; |
| 68 | for (OpResult result : op->getResults()) { |
| 69 | auto predecessors = mlir::getControlFlowPredecessors(value: result); |
| 70 | llvm::errs() << " '"<< op->getName() << "': result #" |
| 71 | << result.getResultNumber() << ": "; |
| 72 | if (!predecessors) |
| 73 | llvm::errs() << "no predecessors\n"; |
| 74 | else |
| 75 | llvm::errs() << predecessors->size() << " predecessor(s)\n"; |
| 76 | } |
| 77 | for (Region ®ion : op->getRegions()) { |
| 78 | if (region.empty()) |
| 79 | continue; |
| 80 | for (BlockArgument arg : region.front().getArguments()) { |
| 81 | auto predecessors = mlir::getControlFlowPredecessors(value: arg); |
| 82 | llvm::errs() << " '"<< op->getName() << "': region #" |
| 83 | << region.getRegionNumber() << " block arg #" |
| 84 | << arg.getArgNumber() << ": "; |
| 85 | if (!predecessors) |
| 86 | llvm::errs() << "no predecessors\n"; |
| 87 | else |
| 88 | llvm::errs() << predecessors->size() << " predecessor(s)\n"; |
| 89 | } |
| 90 | } |
| 91 | }); |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | } // namespace |
| 96 | |
| 97 | namespace mlir { |
| 98 | namespace test { |
| 99 | void registerTestSliceAnalysisPass() { |
| 100 | PassRegistration<TestTopologicalSortPass>(); |
| 101 | PassRegistration<TestControlFlowPredecessorsPass>(); |
| 102 | } |
| 103 | } // namespace test |
| 104 | } // namespace mlir |
| 105 |