xref: /llvm-project/mlir/test/lib/Analysis/TestSlice.cpp (revision be0a7e9f27083ada6072fcc0711ffa5630daa5ec)
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, FunctionPass> {
20   StringRef getArgument() const final { return "test-print-topological-sort"; }
21   StringRef getDescription() const final {
22     return "Print operations in topological order";
23   }
24   void runOnFunction() override {
25     std::map<int, Operation *> ops;
26     getFunction().walk([&ops](Operation *op) {
27       if (auto originalOrderAttr = op->getAttrOfType<IntegerAttr>(kOrderMarker))
28         ops[originalOrderAttr.getInt()] = op;
29     });
30     SetVector<Operation *> sortedOp;
31     for (auto op : ops)
32       sortedOp.insert(op.second);
33     sortedOp = topologicalSort(sortedOp);
34     llvm::errs() << "Testing : " << getFunction().getName() << "\n";
35     for (Operation *op : sortedOp) {
36       op->print(llvm::errs());
37       llvm::errs() << "\n";
38     }
39   }
40 };
41 
42 } // namespace
43 
44 namespace mlir {
45 namespace test {
46 void registerTestSliceAnalysisPass() {
47   PassRegistration<TestTopologicalSortPass>();
48 }
49 } // namespace test
50 } // namespace mlir
51