13#include <mlir/Analysis/DataFlow/DeadCodeAnalysis.h>
14#include <mlir/Analysis/DataFlowFramework.h>
15#include <mlir/IR/Block.h>
16#include <mlir/IR/Operation.h>
17#include <mlir/IR/Region.h>
18#include <mlir/IR/Value.h>
19#include <mlir/Interfaces/CallInterfaces.h>
20#include <mlir/Interfaces/ControlFlowInterfaces.h>
21#include <mlir/Support/LogicalResult.h>
23#include <llvm/ADT/SmallVector.h>
24#include <llvm/Support/Casting.h>
35 for (Region ®ion : top->getRegions()) {
39 for (Value argument : region.front().getArguments()) {
40 setToEntryState(getLatticeElement(argument));
44 return initializeRecursivelyInProgramOrder(top);
48 if (point->isBlockStart()) {
49 return ::mlir::dataflow::AbstractSparseForwardDataFlowAnalysis::visit(point);
52 Operation *op = point->getPrevOp();
53 if (op->getNumResults() != 0) {
54 return ::mlir::dataflow::AbstractSparseForwardDataFlowAnalysis::visit(point);
56 return visitZeroResultOperation(op);
60AbstractSparseForwardDataFlowAnalysis::initializeRecursivelyInProgramOrder(Operation *op) {
61 if (failed(visitOperationDuringInitialization(op))) {
65 for (Region ®ion : op->getRegions()) {
66 for (Block &block : region) {
67 ProgramPoint *blockStart = getProgramPointBefore(&block);
68 getOrCreate<::mlir::dataflow::Executable>(blockStart)->blockContentSubscribe(
this);
69 if (failed(::mlir::dataflow::AbstractSparseForwardDataFlowAnalysis::visit(blockStart))) {
72 for (Operation &nestedOp : block) {
73 if (failed(initializeRecursivelyInProgramOrder(&nestedOp))) {
84AbstractSparseForwardDataFlowAnalysis::visitOperationDuringInitialization(Operation *op) {
85 if (op->getNumResults() == 0) {
88 return visitZeroResultOperation(op);
90 return ::mlir::dataflow::AbstractSparseForwardDataFlowAnalysis::visit(getProgramPointAfter(op));
93bool AbstractSparseForwardDataFlowAnalysis::isOperationLive(Operation *op) {
94 if (op->getBlock() ==
nullptr) {
97 return getOrCreate<::mlir::dataflow::Executable>(getProgramPointBefore(op->getBlock()))->isLive();
100llvm::SmallVector<const AbstractSparseLattice *, 4>
101AbstractSparseForwardDataFlowAnalysis::collectOperandLatticesAndSubscribe(Operation *op) {
102 llvm::SmallVector<const AbstractSparseLattice *, 4> operandLattices;
103 operandLattices.reserve(op->getNumOperands());
104 for (Value operand : op->getOperands()) {
106 operandLattice->useDefSubscribe(
this);
107 operandLattices.push_back(operandLattice);
109 return operandLattices;
112LogicalResult AbstractSparseForwardDataFlowAnalysis::visitZeroResultCallOperation(
113 CallOpInterface call, ArrayRef<const AbstractSparseLattice *> operandLattices
115 ArrayRef<AbstractSparseLattice *> emptyResultLattices;
119 auto callable = llvm::dyn_cast_if_present<CallableOpInterface>(call.resolveCallable());
120 if (!getSolverConfig().isInterprocedural() || (callable && !callable.getCallableRegion())) {
121 visitExternalCallImpl(call, operandLattices, emptyResultLattices);
127 Operation *callOp = call.getOperation();
128 (void)getOrCreateFor<::mlir::dataflow::PredecessorState>(
129 getProgramPointAfter(callOp), getProgramPointAfter(callOp)
134LogicalResult AbstractSparseForwardDataFlowAnalysis::visitZeroResultOperation(Operation *op) {
135 if (!isOperationLive(op)) {
142 if (llvm::isa<RegionBranchOpInterface>(op)) {
146 auto operandLattices = collectOperandLatticesAndSubscribe(op);
148 if (
auto call = llvm::dyn_cast<CallOpInterface>(op)) {
149 return visitZeroResultCallOperation(call, operandLattices);
153 ArrayRef<AbstractSparseLattice *> emptyResultLattices;
154 return visitOperationImpl(op, operandLattices, emptyResultLattices);
This file provides LLZK's sparse forward data-flow analysis compatibility layer.
mlir::LogicalResult visit(mlir::ProgramPoint *point) override
Delegate block starts and result-producing operations to upstream MLIR.
mlir::LogicalResult initialize(mlir::Operation *top) override
Initialize the analysis while preserving the program-order visitation of the old LLZK sparse analysis...
AbstractSparseForwardDataFlowAnalysis(mlir::DataFlowSolver &s)
mlir::dataflow::AbstractSparseLattice AbstractSparseLattice