Skip to content

Conversation

@Lunderberg
Copy link
Contributor

Prior to this commit, a tvm::NameSupply needed to be constructed with an explicit const String& prefix argument. Omitting this argument would fall back to the default constructor provided by the TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS macro, producing a NameSupply holding a nullptr. This then leads to a segfault when the null NameSupply is used.

The vast majority of usages of NameSupply::NameSupply (29 out of 31) initialize it with an empty prefix string. The remaining two use cases initialize it with a non-empty prefix string. There are no cases in which a null NameSupply is initialized.

This commit updates NameSupply to use the TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS macro instead of TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS. This allows the default constructor to provide the common usage of a NameSupply with an empty prefix, rather than the error-prone usage of a null NameSupply

A similar change is also made for GlobalVarSupply, as the majority of its uses also default to an empty prefix (11 out of 13).

Prior to this commit, a `tvm::NameSupply` needed to be constructed
with an explicit `const String& prefix` argument.  Omitting this
argument would fall back to the default constructor provided by the
`TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS` macro, producing a
`NameSupply` holding a nullptr.  This then leads to a segfault when
the null `NameSupply` is used.

The vast majority of usages of `NameSupply::NameSupply` (29 out of 31)
initialize it with an empty `prefix` string.  The remaining two use
cases initialize it with a non-empty `prefix` string.  There are no
cases in which a null `NameSupply` is initialized.

This commit updates `NameSupply` to use the
`TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS` macro instead of
`TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS`.  This allows the default
constructor to provide the common usage of a `NameSupply` with an
empty prefix, rather than the error-prone usage of a null `NameSupply`

A similar change is also made for `GlobalVarSupply`, as the majority
of its uses also default to an empty prefix (11 out of 13).
@Lunderberg
Copy link
Contributor Author

I've accidentally made this mistake too many times, and wanted to make it harder to hit this segfault.

@Lunderberg Lunderberg requested a review from masahi July 12, 2024 18:58
@masahi masahi merged commit f60b08c into apache:main Jul 12, 2024
@Lunderberg Lunderberg deleted the qol_default_constructor_for_name_supply branch July 14, 2024 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants