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