#include "mlir/Analysis/DataFlow/SparseAnalysis.h"
#include "mlir/Analysis/DataFlow/DeadCodeAnalysis.h"
#include "mlir/Interfaces/CallInterfaces.h"
using namespace mlir;
using namespace mlir::dataflow;
void AbstractSparseLattice::onUpdate(DataFlowSolver *solver) const {
for (Operation *user : point.get<Value>().getUsers())
for (DataFlowAnalysis *analysis : useDefSubscribers)
solver->enqueue({user, analysis});
}
AbstractSparseDataFlowAnalysis::AbstractSparseDataFlowAnalysis(
DataFlowSolver &solver)
: DataFlowAnalysis(solver) {
registerPointKind<CFGEdge>();
}
LogicalResult AbstractSparseDataFlowAnalysis::initialize(Operation *top) {
for (Region ®ion : top->getRegions()) {
if (region.empty())
continue;
for (Value argument : region.front().getArguments())
markAllPessimisticFixpoint(getLatticeElement(argument));
}
return initializeRecursively(top);
}
LogicalResult
AbstractSparseDataFlowAnalysis::initializeRecursively(Operation *op) {
visitOperation(op);
for (Region ®ion : op->getRegions()) {
for (Block &block : region) {
getOrCreate<Executable>(&block)->blockContentSubscribe(this);
visitBlock(&block);
for (Operation &op : block)
if (failed(initializeRecursively(&op)))
return failure();
}
}
return success();
}
LogicalResult AbstractSparseDataFlowAnalysis::visit(ProgramPoint point) {
if (Operation *op = point.dyn_cast<Operation *>())
visitOperation(op);
else if (Block *block = point.dyn_cast<Block *>())
visitBlock(block);
else
return failure();
return success();
}
void AbstractSparseDataFlowAnalysis::visitOperation(Operation *op) {
if (op->getNumResults() == 0)
return;
if (!getOrCreate<Executable>(op->getBlock())->isLive())
return;
SmallVector<AbstractSparseLattice *> resultLattices;
resultLattices.reserve(op->getNumResults());
bool allAtFixpoint = true;
for (Value result : op->getResults()) {
AbstractSparseLattice *resultLattice = getLatticeElement(result);
allAtFixpoint &= resultLattice->isAtFixpoint();
resultLattices.push_back(resultLattice);
}
if (allAtFixpoint)
return;
if (auto branch = dyn_cast<RegionBranchOpInterface>(op)) {
return visitRegionSuccessors({branch}, branch,
llvm::None, resultLattices);
}
if (auto call = dyn_cast<CallOpInterface>(op)) {
const auto *predecessors = getOrCreateFor<PredecessorState>(op, call);
if (!predecessors->allPredecessorsKnown())
return markAllPessimisticFixpoint(resultLattices);
for (Operation *predecessor : predecessors->getKnownPredecessors())
for (auto it : llvm::zip(predecessor->getOperands(), resultLattices))
join(std::get<1>(it), *getLatticeElementFor(op, std::get<0>(it)));
return;
}
SmallVector<const AbstractSparseLattice *> operandLattices;
operandLattices.reserve(op->getNumOperands());
for (Value operand : op->getOperands()) {
AbstractSparseLattice *operandLattice = getLatticeElement(operand);
operandLattice->useDefSubscribe(this);
if (operandLattice->isUninitialized())
return;
operandLattices.push_back(operandLattice);
}
visitOperationImpl(op, operandLattices, resultLattices);
}
void AbstractSparseDataFlowAnalysis::visitBlock(Block *block) {
if (block->getNumArguments() == 0)
return;
if (!getOrCreate<Executable>(block)->isLive())
return;
SmallVector<AbstractSparseLattice *> argLattices;
argLattices.reserve(block->getNumArguments());
bool allAtFixpoint = true;
for (BlockArgument argument : block->getArguments()) {
AbstractSparseLattice *argLattice = getLatticeElement(argument);
allAtFixpoint &= argLattice->isAtFixpoint();
argLattices.push_back(argLattice);
}
if (allAtFixpoint)
return;
if (block->isEntryBlock()) {
auto callable = dyn_cast<CallableOpInterface>(block->getParentOp());
if (callable && callable.getCallableRegion() == block->getParent()) {
const auto *callsites = getOrCreateFor<PredecessorState>(block, callable);
if (!callsites->allPredecessorsKnown())
return markAllPessimisticFixpoint(argLattices);
for (Operation *callsite : callsites->getKnownPredecessors()) {
auto call = cast<CallOpInterface>(callsite);
for (auto it : llvm::zip(call.getArgOperands(), argLattices))
join(std::get<1>(it), *getLatticeElementFor(block, std::get<0>(it)));
}
return;
}
if (auto branch = dyn_cast<RegionBranchOpInterface>(block->getParentOp())) {
return visitRegionSuccessors(
block, branch, block->getParent()->getRegionNumber(), argLattices);
}
return visitNonControlFlowArgumentsImpl(block->getParentOp(),
RegionSuccessor(block->getParent()),
argLattices, 0);
}
for (Block::pred_iterator it = block->pred_begin(), e = block->pred_end();
it != e; ++it) {
Block *predecessor = *it;
auto *edgeExecutable =
getOrCreate<Executable>(getProgramPoint<CFGEdge>(predecessor, block));
edgeExecutable->blockContentSubscribe(this);
if (!edgeExecutable->isLive())
continue;
if (auto branch =
dyn_cast<BranchOpInterface>(predecessor->getTerminator())) {
SuccessorOperands operands =
branch.getSuccessorOperands(it.getSuccessorIndex());
for (auto &it : llvm::enumerate(argLattices)) {
if (Value operand = operands[it.index()]) {
join(it.value(), *getLatticeElementFor(block, operand));
} else {
markAllPessimisticFixpoint(it.value());
}
}
} else {
return markAllPessimisticFixpoint(argLattices);
}
}
}
void AbstractSparseDataFlowAnalysis::visitRegionSuccessors(
ProgramPoint point, RegionBranchOpInterface branch,
Optional<unsigned> successorIndex,
ArrayRef<AbstractSparseLattice *> lattices) {
const auto *predecessors = getOrCreateFor<PredecessorState>(point, point);
assert(predecessors->allPredecessorsKnown() &&
"unexpected unresolved region successors");
for (Operation *op : predecessors->getKnownPredecessors()) {
Optional<OperandRange> operands;
if (op == branch) {
operands = branch.getSuccessorEntryOperands(successorIndex);
} else {
if (isRegionReturnLike(op))
operands = getRegionBranchSuccessorOperands(op, successorIndex);
}
if (!operands) {
return markAllPessimisticFixpoint(lattices);
}
ValueRange inputs = predecessors->getSuccessorInputs(op);
assert(inputs.size() == operands->size() &&
"expected the same number of successor inputs as operands");
unsigned firstIndex = 0;
if (inputs.size() != lattices.size()) {
if (auto *op = point.dyn_cast<Operation *>()) {
if (!inputs.empty())
firstIndex = inputs.front().cast<OpResult>().getResultNumber();
visitNonControlFlowArgumentsImpl(
branch,
RegionSuccessor(
branch->getResults().slice(firstIndex, inputs.size())),
lattices, firstIndex);
} else {
if (!inputs.empty())
firstIndex = inputs.front().cast<BlockArgument>().getArgNumber();
Region *region = point.get<Block *>()->getParent();
visitNonControlFlowArgumentsImpl(
branch,
RegionSuccessor(region, region->getArguments().slice(
firstIndex, inputs.size())),
lattices, firstIndex);
}
}
for (auto it : llvm::zip(*operands, lattices.drop_front(firstIndex)))
join(std::get<1>(it), *getLatticeElementFor(point, std::get<0>(it)));
}
}
const AbstractSparseLattice *
AbstractSparseDataFlowAnalysis::getLatticeElementFor(ProgramPoint point,
Value value) {
AbstractSparseLattice *state = getLatticeElement(value);
addDependency(state, point);
return state;
}
void AbstractSparseDataFlowAnalysis::markAllPessimisticFixpoint(
ArrayRef<AbstractSparseLattice *> lattices) {
for (AbstractSparseLattice *lattice : lattices)
propagateIfChanged(lattice, lattice->markPessimisticFixpoint());
}
void AbstractSparseDataFlowAnalysis::join(AbstractSparseLattice *lhs,
const AbstractSparseLattice &rhs) {
propagateIfChanged(lhs, lhs->join(rhs));
}