[Mlir-commits] [mlir] f1f3612 - [mlir] Update Values to use new casting infra
Nick Kreeger
llvmlistbot at llvm.org
Fri Oct 14 09:56:58 PDT 2022
Author: Nick Kreeger
Date: 2022-10-14T11:56:35-05:00
New Revision: f1f3612417a89f797ec2d6d405dd30d6890bef9e
URL: https://github.com/llvm/llvm-project/commit/f1f3612417a89f797ec2d6d405dd30d6890bef9e
DIFF: https://github.com/llvm/llvm-project/commit/f1f3612417a89f797ec2d6d405dd30d6890bef9e.diff
LOG: [mlir] Update Values to use new casting infra
This allows for using the llvm namespace cast methods instead of the ones on the Value class. The Value class method are kept for now, but we'll want to remove these eventually (with a really long lead time).
Related change: https://reviews.llvm.org/D134327
Differential Revision: https://reviews.llvm.org/D135870
Added:
Modified:
mlir/include/mlir/IR/Value.h
mlir/lib/AsmParser/AsmParserState.cpp
mlir/lib/AsmParser/Parser.cpp
mlir/lib/IR/AsmPrinter.cpp
mlir/lib/IR/Dominance.cpp
Removed:
################################################################################
diff --git a/mlir/include/mlir/IR/Value.h b/mlir/include/mlir/IR/Value.h
index c9ff4f0145938..3557d2d0e0d5d 100644
--- a/mlir/include/mlir/IR/Value.h
+++ b/mlir/include/mlir/IR/Value.h
@@ -88,26 +88,22 @@ class Value {
template <typename U>
bool isa() const {
- assert(*this && "isa<> used on a null type.");
- return U::classof(*this);
+ return llvm::isa<U>(*this);
}
- template <typename First, typename Second, typename... Rest>
- bool isa() const {
- return isa<First>() || isa<Second, Rest...>();
- }
template <typename U>
U dyn_cast() const {
- return isa<U>() ? U(impl) : U(nullptr);
+ return llvm::dyn_cast<U>(*this);
}
+
template <typename U>
U dyn_cast_or_null() const {
- return (*this && isa<U>()) ? U(impl) : U(nullptr);
+ return llvm::dyn_cast_if_present<U>(*this);
}
+
template <typename U>
U cast() const {
- assert(isa<U>());
- return U(impl);
+ return llvm::cast<U>(*this);
}
explicit operator bool() const { return impl; }
@@ -560,6 +556,31 @@ struct PointerLikeTypeTraits<mlir::OpResult>
}
};
+/// Add support for llvm style casts. We provide a cast between To and From if
+/// From is mlir::Value or derives from it.
+template <typename To, typename From>
+struct CastInfo<
+ To, From,
+ std::enable_if_t<std::is_same_v<mlir::Value, std::remove_const_t<From>> ||
+ std::is_base_of_v<mlir::Value, From>>>
+ : NullableValueCastFailed<To>,
+ DefaultDoCastIfPossible<To, From, CastInfo<To, From>> {
+ /// Arguments are taken as mlir::Value here and not as `From`, because
+ /// when casting from an intermediate type of the hierarchy to one of its
+ /// children, the val.getKind() inside T::classof will use the static
+ /// getKind() of the parent instead of the non-static ValueImpl::getKind()
+ /// that returns the dynamic type. This means that T::classof would end up
+ /// comparing the static Kind of the children to the static Kind of its
+ /// parent, making it impossible to downcast from the parent to the child.
+ static inline bool isPossible(mlir::Value ty) {
+ /// Return a constant true instead of a dynamic true when casting to self or
+ /// up the hierarchy.
+ return std::is_same_v<To, std::remove_const_t<From>> ||
+ std::is_base_of_v<To, From> || To::classof(ty);
+ }
+ static inline To doCast(mlir::Value value) { return To(value.getImpl()); }
+};
+
} // namespace llvm
#endif
diff --git a/mlir/lib/AsmParser/AsmParserState.cpp b/mlir/lib/AsmParser/AsmParserState.cpp
index d94a25017615f..4860b101d598d 100644
--- a/mlir/lib/AsmParser/AsmParserState.cpp
+++ b/mlir/lib/AsmParser/AsmParserState.cpp
@@ -273,7 +273,7 @@ void AsmParserState::addDefinition(BlockArgument blockArg, SMLoc location) {
void AsmParserState::addUses(Value value, ArrayRef<SMLoc> locations) {
// Handle the case where the value is an operation result.
- if (OpResult result = value.dyn_cast<OpResult>()) {
+ if (OpResult result = dyn_cast<OpResult>(value)) {
// Check to see if a definition for the parent operation has been recorded.
// If one hasn't, we treat the provided value as a placeholder value that
// will be refined further later.
diff --git a/mlir/lib/AsmParser/Parser.cpp b/mlir/lib/AsmParser/Parser.cpp
index b92d27b37dc2a..ac19412543119 100644
--- a/mlir/lib/AsmParser/Parser.cpp
+++ b/mlir/lib/AsmParser/Parser.cpp
@@ -2255,7 +2255,7 @@ ParseResult OperationParser::codeCompleteSSAUse() {
// If the value isn't a forward reference, we also add the name of the op
// to the detail.
- if (auto result = frontValue.dyn_cast<OpResult>()) {
+ if (auto result = dyn_cast<OpResult>(frontValue)) {
if (!forwardRefPlaceholders.count(result))
detailOS << result.getOwner()->getName() << ": ";
} else {
diff --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp
index 53da51cf10862..b2de0b3e4d7fc 100644
--- a/mlir/lib/IR/AsmPrinter.cpp
+++ b/mlir/lib/IR/AsmPrinter.cpp
@@ -1009,7 +1009,7 @@ void SSANameState::printValueID(Value value, bool printResultNo,
// If this is an operation result, collect the head lookup value of the result
// group and the result number of 'result' within that group.
- if (OpResult result = value.dyn_cast<OpResult>())
+ if (OpResult result = dyn_cast<OpResult>(value))
getResultIDAndNumber(result, lookupValue, resultNo);
auto it = valueIDs.find(lookupValue);
diff --git a/mlir/lib/IR/Dominance.cpp b/mlir/lib/IR/Dominance.cpp
index 68cc2039ab646..beb7f7bfeedfa 100644
--- a/mlir/lib/IR/Dominance.cpp
+++ b/mlir/lib/IR/Dominance.cpp
@@ -297,7 +297,7 @@ bool DominanceInfo::properlyDominatesImpl(Operation *a, Operation *b,
bool DominanceInfo::properlyDominates(Value a, Operation *b) const {
// block arguments properly dominate all operations in their own block, so
// we use a dominates check here, not a properlyDominates check.
- if (auto blockArg = a.dyn_cast<BlockArgument>())
+ if (auto blockArg = dyn_cast<BlockArgument>(a))
return dominates(blockArg.getOwner(), b->getBlock());
// `a` properlyDominates `b` if the operation defining `a` properlyDominates
More information about the Mlir-commits
mailing list