
The MLIR classes Type/Attribute/Operation/Op/Value support cast/dyn_cast/isa/dyn_cast_or_null functionality through llvm's doCast functionality in addition to defining methods with the same name. This change begins the migration of uses of the method to the corresponding function call as has been decided as more consistent. Note that there still exist classes that only define methods directly, such as AffineExpr, and this does not include work currently to support a functional cast/isa call. Context: - https://mlir.llvm.org/deprecation/ at "Use the free function variants for dyn_cast/cast/isa/…" - Original discussion at https://discourse.llvm.org/t/preferred-casting-style-going-forward/68443 Implementation: This patch updates all remaining uses of the deprecated functionality in mlir/. This was done with clang-tidy as described below and further modifications to GPUBase.td and OpenMPOpsInterfaces.td. Steps are described per line, as comments are removed by git: 0. Retrieve the change from the following to build clang-tidy with an additional check: main...tpopp:llvm-project:tidy-cast-check 1. Build clang-tidy 2. Run clang-tidy over your entire codebase while disabling all checks and enabling the one relevant one. Run on all header files also. 3. Delete .inc files that were also modified, so the next build rebuilds them to a pure state. ``` ninja -C $BUILD_DIR clang-tidy run-clang-tidy -clang-tidy-binary=$BUILD_DIR/bin/clang-tidy -checks='-*,misc-cast-functions'\ -header-filter=mlir/ mlir/* -fix rm -rf $BUILD_DIR/tools/mlir/**/*.inc ``` Differential Revision: https://reviews.llvm.org/D151542
110 lines
4.1 KiB
C++
110 lines
4.1 KiB
C++
//===- ConstantPropagationAnalysis.cpp - Constant propagation analysis ----===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir/Analysis/DataFlow/ConstantPropagationAnalysis.h"
|
|
#include "mlir/IR/OpDefinition.h"
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#define DEBUG_TYPE "constant-propagation"
|
|
|
|
using namespace mlir;
|
|
using namespace mlir::dataflow;
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// ConstantValue
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
void ConstantValue::print(raw_ostream &os) const {
|
|
if (isUninitialized()) {
|
|
os << "<UNINITIALIZED>";
|
|
return;
|
|
}
|
|
if (getConstantValue() == nullptr) {
|
|
os << "<UNKNOWN>";
|
|
return;
|
|
}
|
|
return getConstantValue().print(os);
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// SparseConstantPropagation
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
void SparseConstantPropagation::visitOperation(
|
|
Operation *op, ArrayRef<const Lattice<ConstantValue> *> operands,
|
|
ArrayRef<Lattice<ConstantValue> *> results) {
|
|
LLVM_DEBUG(llvm::dbgs() << "SCP: Visiting operation: " << *op << "\n");
|
|
|
|
// Don't try to simulate the results of a region operation as we can't
|
|
// guarantee that folding will be out-of-place. We don't allow in-place
|
|
// folds as the desire here is for simulated execution, and not general
|
|
// folding.
|
|
if (op->getNumRegions()) {
|
|
setAllToEntryStates(results);
|
|
return;
|
|
}
|
|
|
|
SmallVector<Attribute, 8> constantOperands;
|
|
constantOperands.reserve(op->getNumOperands());
|
|
for (auto *operandLattice : operands) {
|
|
if (operandLattice->getValue().isUninitialized())
|
|
return;
|
|
constantOperands.push_back(operandLattice->getValue().getConstantValue());
|
|
}
|
|
|
|
// Save the original operands and attributes just in case the operation
|
|
// folds in-place. The constant passed in may not correspond to the real
|
|
// runtime value, so in-place updates are not allowed.
|
|
SmallVector<Value, 8> originalOperands(op->getOperands());
|
|
DictionaryAttr originalAttrs = op->getAttrDictionary();
|
|
|
|
// Simulate the result of folding this operation to a constant. If folding
|
|
// fails or was an in-place fold, mark the results as overdefined.
|
|
SmallVector<OpFoldResult, 8> foldResults;
|
|
foldResults.reserve(op->getNumResults());
|
|
if (failed(op->fold(constantOperands, foldResults))) {
|
|
setAllToEntryStates(results);
|
|
return;
|
|
}
|
|
|
|
// If the folding was in-place, mark the results as overdefined and reset
|
|
// the operation. We don't allow in-place folds as the desire here is for
|
|
// simulated execution, and not general folding.
|
|
if (foldResults.empty()) {
|
|
op->setOperands(originalOperands);
|
|
op->setAttrs(originalAttrs);
|
|
setAllToEntryStates(results);
|
|
return;
|
|
}
|
|
|
|
// Merge the fold results into the lattice for this operation.
|
|
assert(foldResults.size() == op->getNumResults() && "invalid result size");
|
|
for (const auto it : llvm::zip(results, foldResults)) {
|
|
Lattice<ConstantValue> *lattice = std::get<0>(it);
|
|
|
|
// Merge in the result of the fold, either a constant or a value.
|
|
OpFoldResult foldResult = std::get<1>(it);
|
|
if (Attribute attr = llvm::dyn_cast_if_present<Attribute>(foldResult)) {
|
|
LLVM_DEBUG(llvm::dbgs() << "Folded to constant: " << attr << "\n");
|
|
propagateIfChanged(lattice,
|
|
lattice->join(ConstantValue(attr, op->getDialect())));
|
|
} else {
|
|
LLVM_DEBUG(llvm::dbgs()
|
|
<< "Folded to value: " << foldResult.get<Value>() << "\n");
|
|
AbstractSparseDataFlowAnalysis::join(
|
|
lattice, *getLatticeElement(foldResult.get<Value>()));
|
|
}
|
|
}
|
|
}
|
|
|
|
void SparseConstantPropagation::setToEntryState(
|
|
Lattice<ConstantValue> *lattice) {
|
|
propagateIfChanged(lattice,
|
|
lattice->join(ConstantValue::getUnknownConstant()));
|
|
}
|