forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Object] Implemented .as<T> for ObjectRef param, returns Optional<T> (a…
…pache#14522) * [Object] Implemented .as<T> for ObjectRef param, returns Optional<T> 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(); } ``` * Use the ObjectRef to Optional<ObjectRefSubclass> where possible 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>()`. * Fixed usage in preprocess.cc * Fix an updated usage in rolling buffer
- Loading branch information
1 parent
f28fcd1
commit 7766f3c
Showing
103 changed files
with
548 additions
and
536 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.