15aa6038aSthomasraoux //===------------- TestSlice.cpp - Test slice related analisis ------------===// 25aa6038aSthomasraoux // 35aa6038aSthomasraoux // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 45aa6038aSthomasraoux // See https://llvm.org/LICENSE.txt for license information. 55aa6038aSthomasraoux // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 65aa6038aSthomasraoux // 75aa6038aSthomasraoux //===----------------------------------------------------------------------===// 85aa6038aSthomasraoux 95aa6038aSthomasraoux #include "mlir/Analysis/SliceAnalysis.h" 10*36d3efeaSRiver Riddle #include "mlir/IR/SymbolTable.h" 115aa6038aSthomasraoux #include "mlir/Pass/Pass.h" 125aa6038aSthomasraoux 135aa6038aSthomasraoux using namespace mlir; 145aa6038aSthomasraoux 155aa6038aSthomasraoux static const StringLiteral kOrderMarker = "__test_sort_original_idx__"; 165aa6038aSthomasraoux 175aa6038aSthomasraoux namespace { 185aa6038aSthomasraoux 195aa6038aSthomasraoux struct TestTopologicalSortPass 2087d6bf37SRiver Riddle : public PassWrapper<TestTopologicalSortPass, 2187d6bf37SRiver Riddle InterfacePass<SymbolOpInterface>> { MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID__anon4a8a3a6b0111::TestTopologicalSortPass225e50dd04SRiver Riddle MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestTopologicalSortPass) 235e50dd04SRiver Riddle 245aa6038aSthomasraoux StringRef getArgument() const final { return "test-print-topological-sort"; } getDescription__anon4a8a3a6b0111::TestTopologicalSortPass255aa6038aSthomasraoux StringRef getDescription() const final { 265aa6038aSthomasraoux return "Print operations in topological order"; 275aa6038aSthomasraoux } runOnOperation__anon4a8a3a6b0111::TestTopologicalSortPass2841574554SRiver Riddle void runOnOperation() override { 295aa6038aSthomasraoux std::map<int, Operation *> ops; 3041574554SRiver Riddle getOperation().walk([&ops](Operation *op) { 315aa6038aSthomasraoux if (auto originalOrderAttr = op->getAttrOfType<IntegerAttr>(kOrderMarker)) 325aa6038aSthomasraoux ops[originalOrderAttr.getInt()] = op; 335aa6038aSthomasraoux }); 345aa6038aSthomasraoux SetVector<Operation *> sortedOp; 355aa6038aSthomasraoux for (auto op : ops) 365aa6038aSthomasraoux sortedOp.insert(op.second); 375aa6038aSthomasraoux sortedOp = topologicalSort(sortedOp); 3841574554SRiver Riddle llvm::errs() << "Testing : " << getOperation().getName() << "\n"; 395aa6038aSthomasraoux for (Operation *op : sortedOp) { 405aa6038aSthomasraoux op->print(llvm::errs()); 415aa6038aSthomasraoux llvm::errs() << "\n"; 425aa6038aSthomasraoux } 435aa6038aSthomasraoux } 445aa6038aSthomasraoux }; 455aa6038aSthomasraoux 46be0a7e9fSMehdi Amini } // namespace 475aa6038aSthomasraoux 485aa6038aSthomasraoux namespace mlir { 495aa6038aSthomasraoux namespace test { registerTestSliceAnalysisPass()505aa6038aSthomasraouxvoid registerTestSliceAnalysisPass() { 515aa6038aSthomasraoux PassRegistration<TestTopologicalSortPass>(); 525aa6038aSthomasraoux } 535aa6038aSthomasraoux } // namespace test 545aa6038aSthomasraoux } // namespace mlir 55