xref: /llvm-project/mlir/test/lib/Analysis/TestSlice.cpp (revision 36d3efea15e6202edd64b05de38d8379e2baddb2)
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/SliceAnalysis.h"
10 #include "mlir/IR/SymbolTable.h"
11 #include "mlir/Pass/Pass.h"
12 
13 using namespace mlir;
14 
15 static const StringLiteral kOrderMarker = "__test_sort_original_idx__";
16 
17 namespace {
18 
19 struct TestTopologicalSortPass
20     : public PassWrapper<TestTopologicalSortPass,
21                          InterfacePass<SymbolOpInterface>> {
22   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestTopologicalSortPass)
23 
24   StringRef getArgument() const final { return "test-print-topological-sort"; }
25   StringRef getDescription() const final {
26     return "Print operations in topological order";
27   }
28   void runOnOperation() override {
29     std::map<int, Operation *> ops;
30     getOperation().walk([&ops](Operation *op) {
31       if (auto originalOrderAttr = op->getAttrOfType<IntegerAttr>(kOrderMarker))
32         ops[originalOrderAttr.getInt()] = op;
33     });
34     SetVector<Operation *> sortedOp;
35     for (auto op : ops)
36       sortedOp.insert(op.second);
37     sortedOp = topologicalSort(sortedOp);
38     llvm::errs() << "Testing : " << getOperation().getName() << "\n";
39     for (Operation *op : sortedOp) {
40       op->print(llvm::errs());
41       llvm::errs() << "\n";
42     }
43   }
44 };
45 
46 } // namespace
47 
48 namespace mlir {
49 namespace test {
50 void registerTestSliceAnalysisPass() {
51   PassRegistration<TestTopologicalSortPass>();
52 }
53 } // namespace test
54 } // namespace mlir
55