Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overload get() function for Optional type. #9748

Merged
merged 7 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/tvm/runtime/container/optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class Optional : public ObjectRef {
ICHECK(data_ != nullptr);
return T(data_);
}
/*!
* \return The internal object pointer with container type of T.
* \note This function do not perform not-null checking.
*/
const ContainerType* get() const { return static_cast<ContainerType*>(data_.get()); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that get is not a standard API as per stl style for Optional. While get is a standard stl style name. We might want to have a longer name in this case.

How about GetContainerPtr(use CamelCase to indicate that it is not STL compatible).

The comment can be updated to explain the behavior of nullopt case(return nullptr) and non-nullopt case(returns the container ptr)

Copy link
Member

@junrushao junrushao Feb 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @tqchen for the valuable feedback! On the other hand, I would say this PR actually does is to simply improve the Optional::get method to return the underlying type instead of the plain Object pointer, and hence it might make sense to keep the name as it is. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I didn't noticed that ObjectRef also have a get method. I think this sounds good to me then. Perhaps we can just add additional comments about the behavior

/*!
* \return The contained value if the Optional is not null
* otherwise return the default_value.
Expand Down
2 changes: 1 addition & 1 deletion src/tir/transforms/inject_rolling_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class RollingBufferInjector : public StmtExprMutator {

auto it{std::find_if(
bound_iter_vars.begin(), bound_iter_vars.end(),
[&](Optional<Var> var) { return var && (var.value().get() == loop_var.get()); })};
[&](Optional<Var> var) { return var && (var.get() == loop_var.get()); })};

if (it != bound_iter_vars.end()) {
auto i{std::distance(bound_iter_vars.begin(), it)};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class BufferAllocationLocator : public StmtExprMutator {
// create buffers to be allocated at each stmts
for (const auto& kv : buffer_lca) {
const Buffer& buffer = kv.first;
const StmtNode* stmt = kv.second.defined() ? kv.second.value().get() : nullptr;
const StmtNode* stmt = kv.second.get();
if (arg_buffers.count(buffer.get())) {
continue;
}
Expand Down