mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-29 10:56:07 +00:00

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. Caveats include: - This clang-tidy script probably has more problems. - This only touches C++ code, so nothing that is being generated. 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 first patch was created with the following steps. The intention is to only do automated changes at first, so I waste less time if it's reverted, and so the first mass change is more clear as an example to other teams that will need to follow similar steps. 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: https://github.com/llvm/llvm-project/compare/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. 4. Some changes have been deleted for the following reasons: - Some files had a variable also named cast - Some files had not included a header file that defines the cast functions - Some files are definitions of the classes that have the casting methods, so the code still refers to the method instead of the function without adding a prefix or removing the method declaration at the same time. ``` 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 git restore mlir/lib/IR mlir/lib/Dialect/DLTI/DLTI.cpp\ mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp\ mlir/lib/**/IR/\ mlir/lib/Dialect/SparseTensor/Transforms/SparseVectorization.cpp\ mlir/lib/Dialect/Vector/Transforms/LowerVectorMultiReduction.cpp\ mlir/test/lib/Dialect/Test/TestTypes.cpp\ mlir/test/lib/Dialect/Transform/TestTransformDialectExtension.cpp\ mlir/test/lib/Dialect/Test/TestAttributes.cpp\ mlir/unittests/TableGen/EnumsGenTest.cpp\ mlir/test/python/lib/PythonTestCAPI.cpp\ mlir/include/mlir/IR/ ``` Differential Revision: https://reviews.llvm.org/D150123
129 lines
4.6 KiB
C++
129 lines
4.6 KiB
C++
//===- InferIntRangeInterface.cpp - Integer range inference interface ---===//
|
|
//
|
|
// 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/Interfaces/InferIntRangeInterface.h"
|
|
#include "mlir/IR/BuiltinTypes.h"
|
|
#include <optional>
|
|
#include "mlir/Interfaces/InferIntRangeInterface.cpp.inc"
|
|
|
|
using namespace mlir;
|
|
|
|
bool ConstantIntRanges::operator==(const ConstantIntRanges &other) const {
|
|
return umin().getBitWidth() == other.umin().getBitWidth() &&
|
|
umin() == other.umin() && umax() == other.umax() &&
|
|
smin() == other.smin() && smax() == other.smax();
|
|
}
|
|
|
|
const APInt &ConstantIntRanges::umin() const { return uminVal; }
|
|
|
|
const APInt &ConstantIntRanges::umax() const { return umaxVal; }
|
|
|
|
const APInt &ConstantIntRanges::smin() const { return sminVal; }
|
|
|
|
const APInt &ConstantIntRanges::smax() const { return smaxVal; }
|
|
|
|
unsigned ConstantIntRanges::getStorageBitwidth(Type type) {
|
|
if (type.isIndex())
|
|
return IndexType::kInternalStorageBitWidth;
|
|
if (auto integerType = dyn_cast<IntegerType>(type))
|
|
return integerType.getWidth();
|
|
// Non-integer types have their bounds stored in width 0 `APInt`s.
|
|
return 0;
|
|
}
|
|
|
|
ConstantIntRanges ConstantIntRanges::maxRange(unsigned bitwidth) {
|
|
return fromUnsigned(APInt::getZero(bitwidth), APInt::getMaxValue(bitwidth));
|
|
}
|
|
|
|
ConstantIntRanges ConstantIntRanges::constant(const APInt &value) {
|
|
return {value, value, value, value};
|
|
}
|
|
|
|
ConstantIntRanges ConstantIntRanges::range(const APInt &min, const APInt &max,
|
|
bool isSigned) {
|
|
if (isSigned)
|
|
return fromSigned(min, max);
|
|
return fromUnsigned(min, max);
|
|
}
|
|
|
|
ConstantIntRanges ConstantIntRanges::fromSigned(const APInt &smin,
|
|
const APInt &smax) {
|
|
unsigned int width = smin.getBitWidth();
|
|
APInt umin, umax;
|
|
if (smin.isNonNegative() == smax.isNonNegative()) {
|
|
umin = smin.ult(smax) ? smin : smax;
|
|
umax = smin.ugt(smax) ? smin : smax;
|
|
} else {
|
|
umin = APInt::getMinValue(width);
|
|
umax = APInt::getMaxValue(width);
|
|
}
|
|
return {umin, umax, smin, smax};
|
|
}
|
|
|
|
ConstantIntRanges ConstantIntRanges::fromUnsigned(const APInt &umin,
|
|
const APInt &umax) {
|
|
unsigned int width = umin.getBitWidth();
|
|
APInt smin, smax;
|
|
if (umin.isNonNegative() == umax.isNonNegative()) {
|
|
smin = umin.slt(umax) ? umin : umax;
|
|
smax = umin.sgt(umax) ? umin : umax;
|
|
} else {
|
|
smin = APInt::getSignedMinValue(width);
|
|
smax = APInt::getSignedMaxValue(width);
|
|
}
|
|
return {umin, umax, smin, smax};
|
|
}
|
|
|
|
ConstantIntRanges
|
|
ConstantIntRanges::rangeUnion(const ConstantIntRanges &other) const {
|
|
// "Not an integer" poisons everything and also cannot be fed to comparison
|
|
// operators.
|
|
if (umin().getBitWidth() == 0)
|
|
return *this;
|
|
if (other.umin().getBitWidth() == 0)
|
|
return other;
|
|
|
|
const APInt &uminUnion = umin().ult(other.umin()) ? umin() : other.umin();
|
|
const APInt &umaxUnion = umax().ugt(other.umax()) ? umax() : other.umax();
|
|
const APInt &sminUnion = smin().slt(other.smin()) ? smin() : other.smin();
|
|
const APInt &smaxUnion = smax().sgt(other.smax()) ? smax() : other.smax();
|
|
|
|
return {uminUnion, umaxUnion, sminUnion, smaxUnion};
|
|
}
|
|
|
|
ConstantIntRanges
|
|
ConstantIntRanges::intersection(const ConstantIntRanges &other) const {
|
|
// "Not an integer" poisons everything and also cannot be fed to comparison
|
|
// operators.
|
|
if (umin().getBitWidth() == 0)
|
|
return *this;
|
|
if (other.umin().getBitWidth() == 0)
|
|
return other;
|
|
|
|
const APInt &uminIntersect = umin().ugt(other.umin()) ? umin() : other.umin();
|
|
const APInt &umaxIntersect = umax().ult(other.umax()) ? umax() : other.umax();
|
|
const APInt &sminIntersect = smin().sgt(other.smin()) ? smin() : other.smin();
|
|
const APInt &smaxIntersect = smax().slt(other.smax()) ? smax() : other.smax();
|
|
|
|
return {uminIntersect, umaxIntersect, sminIntersect, smaxIntersect};
|
|
}
|
|
|
|
std::optional<APInt> ConstantIntRanges::getConstantValue() const {
|
|
// Note: we need to exclude the trivially-equal width 0 values here.
|
|
if (umin() == umax() && umin().getBitWidth() != 0)
|
|
return umin();
|
|
if (smin() == smax() && smin().getBitWidth() != 0)
|
|
return smin();
|
|
return std::nullopt;
|
|
}
|
|
|
|
raw_ostream &mlir::operator<<(raw_ostream &os, const ConstantIntRanges &range) {
|
|
return os << "unsigned : [" << range.umin() << ", " << range.umax()
|
|
<< "] signed : [" << range.smin() << ", " << range.smax() << "]";
|
|
}
|