xref: /llvm-project/mlir/test/lib/Analysis/TestSlice.cpp (revision b00e0c167186d69e1e6bceda57c09b272bd6acfc)
1*b00e0c16SChristian Ulmann //===- 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 
9*b00e0c16SChristian Ulmann #include "mlir/Analysis/TopologicalSortUtils.h"
10*b00e0c16SChristian Ulmann #include "mlir/IR/BuiltinTypes.h"
1136d3efeaSRiver Riddle #include "mlir/IR/SymbolTable.h"
125aa6038aSthomasraoux #include "mlir/Pass/Pass.h"
135aa6038aSthomasraoux 
145aa6038aSthomasraoux using namespace mlir;
155aa6038aSthomasraoux 
16*b00e0c16SChristian Ulmann static const StringLiteral kToSortMark = "test_to_sort";
17*b00e0c16SChristian Ulmann static const StringLiteral kOrderIndex = "test_sort_index";
185aa6038aSthomasraoux 
195aa6038aSthomasraoux namespace {
205aa6038aSthomasraoux 
215aa6038aSthomasraoux struct TestTopologicalSortPass
2287d6bf37SRiver Riddle     : public PassWrapper<TestTopologicalSortPass,
2387d6bf37SRiver Riddle                          InterfacePass<SymbolOpInterface>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID__anona1b0e00e0111::TestTopologicalSortPass245e50dd04SRiver Riddle   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestTopologicalSortPass)
255e50dd04SRiver Riddle 
265aa6038aSthomasraoux   StringRef getArgument() const final { return "test-print-topological-sort"; }
getDescription__anona1b0e00e0111::TestTopologicalSortPass275aa6038aSthomasraoux   StringRef getDescription() const final {
28*b00e0c16SChristian Ulmann     return "Sorts operations topologically and attaches attributes with their "
29*b00e0c16SChristian Ulmann            "corresponding index in the ordering to them";
305aa6038aSthomasraoux   }
runOnOperation__anona1b0e00e0111::TestTopologicalSortPass3141574554SRiver Riddle   void runOnOperation() override {
32*b00e0c16SChristian Ulmann     SetVector<Operation *> toSort;
33*b00e0c16SChristian Ulmann     getOperation().walk([&](Operation *op) {
34*b00e0c16SChristian Ulmann       if (op->hasAttrOfType<UnitAttr>(kToSortMark))
35*b00e0c16SChristian Ulmann         toSort.insert(op);
365aa6038aSthomasraoux     });
37*b00e0c16SChristian Ulmann 
38*b00e0c16SChristian Ulmann     auto i32Type = IntegerType::get(&getContext(), 32);
39*b00e0c16SChristian Ulmann     SetVector<Operation *> sortedOps = topologicalSort(toSort);
40*b00e0c16SChristian Ulmann     for (auto [index, op] : llvm::enumerate(sortedOps))
41*b00e0c16SChristian Ulmann       op->setAttr(kOrderIndex, IntegerAttr::get(i32Type, index));
425aa6038aSthomasraoux   }
435aa6038aSthomasraoux };
445aa6038aSthomasraoux 
45be0a7e9fSMehdi Amini } // namespace
465aa6038aSthomasraoux 
475aa6038aSthomasraoux namespace mlir {
485aa6038aSthomasraoux namespace test {
registerTestSliceAnalysisPass()495aa6038aSthomasraoux void registerTestSliceAnalysisPass() {
505aa6038aSthomasraoux   PassRegistration<TestTopologicalSortPass>();
515aa6038aSthomasraoux }
525aa6038aSthomasraoux } // namespace test
535aa6038aSthomasraoux } // namespace mlir
54