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->getDiscardableAttrOfType<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 for (auto [regionIndex, region] : llvm::enumerate(First: op->getRegions())) {
60 os << " region: #" << regionIndex << ":\n";
61 for (auto [blockIndex, block] : llvm::enumerate(First&: region)) {
62 os << " block: #" << blockIndex << ":\n";
63 for (auto [argumentIndex, argument] :
64 llvm::enumerate(First: block.getArguments())) {
65 const Liveness *liveness = livenessAnalysis.getLiveness(val: argument);
66 assert(liveness && "expected a sparse lattice");
67 os << " argument: #" << argumentIndex << ": ";
68 liveness->print(os);
69 os << "\n";
70 }
71 }
72 }
73 });
74 }
75};
76} // end anonymous namespace
77
78namespace mlir {
79namespace test {
80void registerTestLivenessAnalysisPass() {
81 PassRegistration<TestLivenessAnalysisPass>();
82}
83} // end namespace test
84} // end namespace mlir
85