Skip to content

Commit f1f3612

Browse files
committed
[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
1 parent ce1a2cc commit f1f3612

File tree

5 files changed

+35
-14
lines changed

5 files changed

+35
-14
lines changed

mlir/include/mlir/IR/Value.h

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,22 @@ class Value {
8888

8989
template <typename U>
9090
bool isa() const {
91-
assert(*this && "isa<> used on a null type.");
92-
return U::classof(*this);
91+
return llvm::isa<U>(*this);
9392
}
9493

95-
template <typename First, typename Second, typename... Rest>
96-
bool isa() const {
97-
return isa<First>() || isa<Second, Rest...>();
98-
}
9994
template <typename U>
10095
U dyn_cast() const {
101-
return isa<U>() ? U(impl) : U(nullptr);
96+
return llvm::dyn_cast<U>(*this);
10297
}
98+
10399
template <typename U>
104100
U dyn_cast_or_null() const {
105-
return (*this && isa<U>()) ? U(impl) : U(nullptr);
101+
return llvm::dyn_cast_if_present<U>(*this);
106102
}
103+
107104
template <typename U>
108105
U cast() const {
109-
assert(isa<U>());
110-
return U(impl);
106+
return llvm::cast<U>(*this);
111107
}
112108

113109
explicit operator bool() const { return impl; }
@@ -560,6 +556,31 @@ struct PointerLikeTypeTraits<mlir::OpResult>
560556
}
561557
};
562558

559+
/// Add support for llvm style casts. We provide a cast between To and From if
560+
/// From is mlir::Value or derives from it.
561+
template <typename To, typename From>
562+
struct CastInfo<
563+
To, From,
564+
std::enable_if_t<std::is_same_v<mlir::Value, std::remove_const_t<From>> ||
565+
std::is_base_of_v<mlir::Value, From>>>
566+
: NullableValueCastFailed<To>,
567+
DefaultDoCastIfPossible<To, From, CastInfo<To, From>> {
568+
/// Arguments are taken as mlir::Value here and not as `From`, because
569+
/// when casting from an intermediate type of the hierarchy to one of its
570+
/// children, the val.getKind() inside T::classof will use the static
571+
/// getKind() of the parent instead of the non-static ValueImpl::getKind()
572+
/// that returns the dynamic type. This means that T::classof would end up
573+
/// comparing the static Kind of the children to the static Kind of its
574+
/// parent, making it impossible to downcast from the parent to the child.
575+
static inline bool isPossible(mlir::Value ty) {
576+
/// Return a constant true instead of a dynamic true when casting to self or
577+
/// up the hierarchy.
578+
return std::is_same_v<To, std::remove_const_t<From>> ||
579+
std::is_base_of_v<To, From> || To::classof(ty);
580+
}
581+
static inline To doCast(mlir::Value value) { return To(value.getImpl()); }
582+
};
583+
563584
} // namespace llvm
564585

565586
#endif

mlir/lib/AsmParser/AsmParserState.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ void AsmParserState::addDefinition(BlockArgument blockArg, SMLoc location) {
273273

274274
void AsmParserState::addUses(Value value, ArrayRef<SMLoc> locations) {
275275
// Handle the case where the value is an operation result.
276-
if (OpResult result = value.dyn_cast<OpResult>()) {
276+
if (OpResult result = dyn_cast<OpResult>(value)) {
277277
// Check to see if a definition for the parent operation has been recorded.
278278
// If one hasn't, we treat the provided value as a placeholder value that
279279
// will be refined further later.

mlir/lib/AsmParser/Parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2255,7 +2255,7 @@ ParseResult OperationParser::codeCompleteSSAUse() {
22552255

22562256
// If the value isn't a forward reference, we also add the name of the op
22572257
// to the detail.
2258-
if (auto result = frontValue.dyn_cast<OpResult>()) {
2258+
if (auto result = dyn_cast<OpResult>(frontValue)) {
22592259
if (!forwardRefPlaceholders.count(result))
22602260
detailOS << result.getOwner()->getName() << ": ";
22612261
} else {

mlir/lib/IR/AsmPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ void SSANameState::printValueID(Value value, bool printResultNo,
10091009

10101010
// If this is an operation result, collect the head lookup value of the result
10111011
// group and the result number of 'result' within that group.
1012-
if (OpResult result = value.dyn_cast<OpResult>())
1012+
if (OpResult result = dyn_cast<OpResult>(value))
10131013
getResultIDAndNumber(result, lookupValue, resultNo);
10141014

10151015
auto it = valueIDs.find(lookupValue);

mlir/lib/IR/Dominance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ bool DominanceInfo::properlyDominatesImpl(Operation *a, Operation *b,
297297
bool DominanceInfo::properlyDominates(Value a, Operation *b) const {
298298
// block arguments properly dominate all operations in their own block, so
299299
// we use a dominates check here, not a properlyDominates check.
300-
if (auto blockArg = a.dyn_cast<BlockArgument>())
300+
if (auto blockArg = dyn_cast<BlockArgument>(a))
301301
return dominates(blockArg.getOwner(), b->getBlock());
302302

303303
// `a` properlyDominates `b` if the operation defining `a` properlyDominates

0 commit comments

Comments
 (0)