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

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