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
13using namespace mlir;
14
15static const StringLiteral kOrderMarker = "__test_sort_original_idx__";
16
17namespace {
18
19struct 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(X: op.second);
37 sortedOp = topologicalSort(toSort: sortedOp);
38 llvm::errs() << "Testing : " << getOperation().getName() << "\n";
39 for (Operation *op : sortedOp) {
40 op->print(os&: llvm::errs());
41 llvm::errs() << "\n";
42 }
43 }
44};
45
46} // namespace
47
48namespace mlir {
49namespace test {
50void registerTestSliceAnalysisPass() {
51 PassRegistration<TestTopologicalSortPass>();
52}
53} // namespace test
54} // namespace mlir
55

source code of mlir/test/lib/Analysis/TestSlice.cpp