[Object] Implemented .as<T> for ObjectRef param, returns Optional<T> - #14522
Merged
Merged
Conversation
Prior to this commit, the `ObjectRef::as<T>()` method could be used
for any `T` that inherits from `tvm::Object`, and would return a
`const T*` if the class could be cast to the specified type, or
`nullptr` otherwise. However, if the
caller needed a `ObjectRef`, they would then need to call
`GetRef<MyObjRef>` to convert from a `const T*`.
This commit extends `ObjectRef::as<T>` to operate on a `T` that
inherits from `tvm::ObjectRef` as well. In this case, the return type
is `Optional<T>`, returning either an instance of the specified
subclass, or `NullOpt` if the object was not an instance of the
specified subclass. Example usage of this new conversion, along with
how it relates to existing functionality, is shown below.
```c++
// Unconditionally convert, throwing an exception if the object isn't
// of the specified type. In contexts where the type of the object is
// unknown, this shouldn't be used.
PrimExpr expr = Downcast<PrimExpr>(obj);
// Protect the Downcast from throwing an exception using IsInstance.
// This avoids the error, but performs the type-checking twice. In
// addition, it requires the caller to specify both the ObjectRef
// subclass and the Object subclass, even though these usually have a
// 1:1 correspondence.
if (obj->IsInstance<PrimExprNode>()) {
PrimExpr expr = Downcast<PrimExpr>(obj);
}
// Perform both type-checking and downcasting with the ObjectRef::as()
// method, then use GetRef to convert to an ObjectRef. This avoids
// double-checking the type, but still requires the caller to
if (const PrimExprNode* ptr = obj.as<PrimExprNode>()) {
PrimExpr expr = GetRef<PrimExpr>(ptr);
}
// New method introduced by this PR. The type-checking is only
// performed once, and the Object subclass is inferred from the
// ObjectRef subclass.
if (Optional<PrimExpr> opt = obj.as<PrimExpr>()) {
PrimExpr expr = opt.value();
}
```
Collaborator
|
Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.
Generated by tvm-bot |
Lunderberg
force-pushed
the
object_ref_as_subref
branch
from
April 7, 2023 19:08
124deb8 to
2426f17
Compare
This commit looked for cases where `ObjectRef::as<ObjectSubclass>()` was used to convert to a `const ObjectSubclass*` followed immediately by a call to `GetRef<ObjectRefSubclass>()`, and replaced them with a single call to `ObjectRef::as<ObjectRefSubclass>()`.
Lunderberg
force-pushed
the
object_ref_as_subref
branch
from
April 10, 2023 14:26
2426f17 to
4c87738
Compare
kparzysz-quic
approved these changes
Apr 10, 2023
kparzysz-quic
left a comment
Contributor
There was a problem hiding this comment.
Looks good. Thanks!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Prior to this PR, the
ObjectRef::as<T>()method could be used for anyTthat inherits fromtvm::Object, and would return aconst T*if the class could be cast to the specified type, ornullptrotherwise. However, if the caller needed aObjectRef, they would then need to callGetRef<MyObjRef>to convert from aconst T*.This PR extends
ObjectRef::as<T>to operate on aTthat inherits fromtvm::ObjectRefas well. In this case, the return type isOptional<T>, returning either an instance of the specified subclass, orNullOptif the object was not an instance of the specified subclass. Example usage of this new conversion, along with how it relates to existing functionality, is shown below.This PR is implemented as two commits. The first commit implements the new functionality, but makes no further changes. The second commit looks for cases where
object_ref.as<TNode>()was immediately followed byGetRef<T>(), replacing with eitherobject_ref.as<T>()or withDowncast<T>(object_ref).