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
23using namespace mlir;
24using namespace mlir::dataflow;
25
26namespace {
27
28struct TestLivenessAnalysisPass
29 : public PassWrapper<TestLivenessAnalysisPass, OperationPass<>> {
30 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestLivenessAnalysisPass)
31
32 StringRef getArgument() const override { return "test-liveness-analysis"; }
33
34 void runOnOperation() override {
35 auto &livenessAnalysis = getAnalysis<RunLivenessAnalysis>();
36 Operation *op = getOperation();
37
38 raw_ostream &os = llvm::outs();
39
40 op->walk(callback: [&](Operation *op) {
41 auto tag = op->getAttrOfType<StringAttr>(name: "tag");
42 if (!tag)
43 return;
44 os << "test_tag: " << tag.getValue() << ":\n";
45 for (auto [index, operand] : llvm::enumerate(First: op->getOperands())) {
46 const Liveness *liveness = livenessAnalysis.getLiveness(val: operand);
47 assert(liveness && "expected a sparse lattice");
48 os << " operand #" << index << ": ";
49 liveness->print(os);
50 os << "\n";
51 }
52 for (auto [index, operand] : llvm::enumerate(First: op->getResults())) {
53 const Liveness *liveness = livenessAnalysis.getLiveness(val: operand);
54 assert(liveness && "expected a sparse lattice");
55 os << " result #" << index << ": ";
56 liveness->print(os);
57 os << "\n";
58 }
59 });
60 }
61};
62} // end anonymous namespace
63
64namespace mlir {
65namespace test {
66void registerTestLivenessAnalysisPass() {
67 PassRegistration<TestLivenessAnalysisPass>();
68}
69} // end namespace test
70} // end namespace mlir
71

source code of mlir/test/lib/Analysis/DataFlow/TestLivenessAnalysis.cpp