| 1 | //===- ConstantPropagationAnalysis.cpp - Constant propagation 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 "mlir/Analysis/DataFlow/ConstantPropagationAnalysis.h" |
| 10 | #include "mlir/Analysis/DataFlow/SparseAnalysis.h" |
| 11 | #include "mlir/IR/BuiltinAttributes.h" |
| 12 | #include "mlir/IR/OpDefinition.h" |
| 13 | #include "mlir/IR/Operation.h" |
| 14 | #include "mlir/IR/Value.h" |
| 15 | #include "mlir/Support/LLVM.h" |
| 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/Support/Casting.h" |
| 18 | #include "llvm/Support/Debug.h" |
| 19 | #include "llvm/Support/DebugLog.h" |
| 20 | #include <cassert> |
| 21 | |
| 22 | #define DEBUG_TYPE "constant-propagation" |
| 23 | |
| 24 | using namespace mlir; |
| 25 | using namespace mlir::dataflow; |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // ConstantValue |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | void ConstantValue::print(raw_ostream &os) const { |
| 32 | if (isUninitialized()) { |
| 33 | os << "<UNINITIALIZED>" ; |
| 34 | return; |
| 35 | } |
| 36 | if (getConstantValue() == nullptr) { |
| 37 | os << "<UNKNOWN>" ; |
| 38 | return; |
| 39 | } |
| 40 | return getConstantValue().print(os); |
| 41 | } |
| 42 | |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | // SparseConstantPropagation |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
| 47 | LogicalResult SparseConstantPropagation::visitOperation( |
| 48 | Operation *op, ArrayRef<const Lattice<ConstantValue> *> operands, |
| 49 | ArrayRef<Lattice<ConstantValue> *> results) { |
| 50 | LDBG() << "SCP: Visiting operation: " << *op; |
| 51 | |
| 52 | // Don't try to simulate the results of a region operation as we can't |
| 53 | // guarantee that folding will be out-of-place. We don't allow in-place |
| 54 | // folds as the desire here is for simulated execution, and not general |
| 55 | // folding. |
| 56 | if (op->getNumRegions()) { |
| 57 | setAllToEntryStates(results); |
| 58 | return success(); |
| 59 | } |
| 60 | |
| 61 | SmallVector<Attribute, 8> constantOperands; |
| 62 | constantOperands.reserve(N: op->getNumOperands()); |
| 63 | for (auto *operandLattice : operands) { |
| 64 | if (operandLattice->getValue().isUninitialized()) |
| 65 | return success(); |
| 66 | constantOperands.push_back(Elt: operandLattice->getValue().getConstantValue()); |
| 67 | } |
| 68 | |
| 69 | // Save the original operands and attributes just in case the operation |
| 70 | // folds in-place. The constant passed in may not correspond to the real |
| 71 | // runtime value, so in-place updates are not allowed. |
| 72 | SmallVector<Value, 8> originalOperands(op->getOperands()); |
| 73 | DictionaryAttr originalAttrs = op->getDiscardableAttrDictionary(); |
| 74 | Attribute originalProperties = op->getPropertiesAsAttribute(); |
| 75 | |
| 76 | // Simulate the result of folding this operation to a constant. |
| 77 | SmallVector<OpFoldResult, 8> foldResults; |
| 78 | foldResults.reserve(N: op->getNumResults()); |
| 79 | LogicalResult folded = op->fold(operands: constantOperands, results&: foldResults); |
| 80 | |
| 81 | // `fold` can mutate the operation in place and still return an out-of-place |
| 82 | // result, so the mutation must be reverted regardless of the outcome. |
| 83 | // Only write the operands back if the fold changed them, as `setOperands` |
| 84 | // relinks use-lists even for identical values. |
| 85 | if (!llvm::equal(LRange: op->getOperands(), RRange&: originalOperands)) |
| 86 | op->setOperands(originalOperands); |
| 87 | op->setDiscardableAttrs(originalAttrs); |
| 88 | if (originalProperties) |
| 89 | (void)op->setPropertiesFromAttribute(attr: originalProperties, |
| 90 | /*emitError=*/nullptr); |
| 91 | |
| 92 | // If folding failed or was in-place, mark the results as overdefined. We |
| 93 | // don't allow in-place folds here: the goal is simulated execution, not |
| 94 | // general folding. |
| 95 | if (failed(Result: folded) || foldResults.empty()) { |
| 96 | setAllToEntryStates(results); |
| 97 | return success(); |
| 98 | } |
| 99 | |
| 100 | // Merge the fold results into the lattice for this operation. |
| 101 | assert(foldResults.size() == op->getNumResults() && "invalid result size" ); |
| 102 | for (const auto it : llvm::zip(t&: results, u&: foldResults)) { |
| 103 | Lattice<ConstantValue> *lattice = std::get<0>(t: it); |
| 104 | |
| 105 | // Merge in the result of the fold, either a constant or a value. |
| 106 | OpFoldResult foldResult = std::get<1>(t: it); |
| 107 | if (Attribute attr = llvm::dyn_cast_if_present<Attribute>(Val&: foldResult)) { |
| 108 | LDBG() << "Folded to constant: " << attr; |
| 109 | propagateIfChanged(state: lattice, |
| 110 | changed: lattice->join(rhs: ConstantValue(attr, op->getDialect()))); |
| 111 | } else { |
| 112 | Value foldValue = cast<Value>(Val&: foldResult); |
| 113 | LDBG() << "Folded to value: " << foldValue; |
| 114 | // The folded value may not be an operand of `op`, so we need to use |
| 115 | // `getLatticeElementFor` (and not `getLatticeElement`) so that |
| 116 | // this operation is revisited if that value's lattice widens later. |
| 117 | AbstractSparseForwardDataFlowAnalysis::join( |
| 118 | lhs: lattice, rhs: *getLatticeElementFor(point: getProgramPointAfter(op), value: foldValue)); |
| 119 | } |
| 120 | } |
| 121 | return success(); |
| 122 | } |
| 123 | |
| 124 | void SparseConstantPropagation::setToEntryState( |
| 125 | Lattice<ConstantValue> *lattice) { |
| 126 | propagateIfChanged(state: lattice, |
| 127 | changed: lattice->join(rhs: ConstantValue::getUnknownConstant())); |
| 128 | } |
| 129 | |