82 lines · cpp
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-exception6//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 TestLivenessAnalysisPass29 : 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([&](Operation *op) {41 auto tag = op->getAttrOfType<StringAttr>("tag");42 if (!tag)43 return;44 os << "test_tag: " << tag.getValue() << ":\n";45 for (auto [index, operand] : llvm::enumerate(op->getOperands())) {46 const Liveness *liveness = livenessAnalysis.getLiveness(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(op->getResults())) {53 const Liveness *liveness = livenessAnalysis.getLiveness(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(op->getRegions())) {60 os << " region: #" << regionIndex << ":\n";61 for (auto [argumntIndex, argument] :62 llvm::enumerate(region.getArguments())) {63 const Liveness *liveness = livenessAnalysis.getLiveness(argument);64 assert(liveness && "expected a sparse lattice");65 os << " argument: #" << argumntIndex << ": ";66 liveness->print(os);67 os << "\n";68 }69 }70 });71 }72};73} // end anonymous namespace74 75namespace mlir {76namespace test {77void registerTestLivenessAnalysisPass() {78 PassRegistration<TestLivenessAnalysisPass>();79}80} // end namespace test81} // end namespace mlir82