xref: /llvm-project/mlir/test/lib/Analysis/DataFlow/TestLivenessAnalysis.cpp (revision db791b278a414fb6df1acc1799adcf11d8fb9169)
1 //===- TestLivenessAnalysis.cpp - Test liveness analysis ------------------===//
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 <llvm/ADT/STLExtras.h>
10 #include <llvm/Support/raw_ostream.h>
11 #include <mlir/Analysis/DataFlow/LivenessAnalysis.h>
12 
13 #include <cassert>
14 #include <mlir/Analysis/DataFlowFramework.h>
15 #include <mlir/IR/BuiltinAttributes.h>
16 #include <mlir/IR/Operation.h>
17 #include <mlir/IR/SymbolTable.h>
18 #include <mlir/Pass/Pass.h>
19 #include <mlir/Pass/PassRegistry.h>
20 #include <mlir/Support/LLVM.h>
21 #include <mlir/Support/TypeID.h>
22 
23 using namespace mlir;
24 using namespace mlir::dataflow;
25 
26 namespace {
27 
28 struct TestLivenessAnalysisPass
29     : public PassWrapper<TestLivenessAnalysisPass, OperationPass<>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID__anon230f108c0111::TestLivenessAnalysisPass30   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestLivenessAnalysisPass)
31 
32   StringRef getArgument() const override { return "test-liveness-analysis"; }
33 
runOnOperation__anon230f108c0111::TestLivenessAnalysisPass34   void runOnOperation() override {
35     auto &livenessAnalysis = getAnalysis<RunLivenessAnalysis>();
36 
37     Operation *op = getOperation();
38 
39     raw_ostream &os = llvm::outs();
40 
41     op->walk([&](Operation *op) {
42       auto tag = op->getAttrOfType<StringAttr>("tag");
43       if (!tag)
44         return;
45       os << "test_tag: " << tag.getValue() << ":\n";
46       for (auto [index, operand] : llvm::enumerate(op->getOperands())) {
47         const Liveness *liveness = livenessAnalysis.getLiveness(operand);
48         assert(liveness && "expected a sparse lattice");
49         os << " operand #" << index << ": ";
50         liveness->print(os);
51         os << "\n";
52       }
53       for (auto [index, operand] : llvm::enumerate(op->getResults())) {
54         const Liveness *liveness = livenessAnalysis.getLiveness(operand);
55         assert(liveness && "expected a sparse lattice");
56         os << " result #" << index << ": ";
57         liveness->print(os);
58         os << "\n";
59       }
60     });
61   }
62 };
63 } // end anonymous namespace
64 
65 namespace mlir {
66 namespace test {
registerTestLivenessAnalysisPass()67 void registerTestLivenessAnalysisPass() {
68   PassRegistration<TestLivenessAnalysisPass>();
69 }
70 } // end namespace test
71 } // end namespace mlir
72