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

[RUNTIME][IR] Allow non-nullable ObjectRef, introduce Optional<T>. #5314

Merged
merged 4 commits into from
Apr 13, 2020

Conversation

tqchen
Copy link
Member

@tqchen tqchen commented Apr 12, 2020

We use ObjectRef and their sub-classes extensively throughout our codebase.
Each of ObjectRef's sub-classes are nullable, which means they can hold nullptr
as their values.

While in some places we need nullptr as an alternative value. The implicit support
for nullptr in all ObjectRef creates additional burdens for the developer
to explicitly check defined in many places of the codebase.

Moreover, it is unclear from the API's intentional point of view whether
we want a nullable object or not-null version(many cases we want the later).

Borrowing existing wisdoms from languages like Rust. We propose to
introduce non-nullable ObjectRef, and Optional container that
represents a nullable variant.

To keep backward compatiblity, we will start by allowing most ObjectRef to be nullable.
However, we should start to use Optional as the type in places where
we know nullable is a requirement. Gradually, we will move most of the ObjectRef
to be non-nullable and use Optional in the nullable cases.

Such explicitness in typing can help reduce the potential problems
in our codebase overall.

Changes in this PR:

  • Introduce _type_is_nullable attribute to ObjectRef
  • Introduce Optional
  • Change String to be non-nullable.
  • Change the API of function->GetAttr to return Optional

@tqchen tqchen changed the title [IR] Allow non-nullable ObjectRef, introduce Optional<T>. [RUNTIME][IR] Allow non-nullable ObjectRef, introduce Optional<T>. Apr 12, 2020
@tqchen
Copy link
Member Author

tqchen commented Apr 12, 2020

@tqchen tqchen force-pushed the optional branch 4 times, most recently from 5fa3274 to 4218304 Compare April 12, 2020 23:54
tests/cpp/container_test.cc Outdated Show resolved Hide resolved
include/tvm/ir/attrs.h Outdated Show resolved Hide resolved
include/tvm/runtime/container.h Show resolved Hide resolved
We use ObjectRef and their sub-classes extensively throughout our codebase.
Each of ObjectRef's sub-classes are nullable, which means they can hold nullptr
as their values.

While in some places we need nullptr as an alternative value. The implicit support
for nullptr in all ObjectRef creates additional burdens for the developer
to explicitly check defined in many places of the codebase.

Moreover, it is unclear from the API's intentional point of view whether
we want a nullable object or not-null version(many cases we want the later).

Borrowing existing wisdoms from languages like Rust. We propose to
introduce non-nullable ObjectRef, and Optional<T> container that
represents a nullable variant.

To keep backward compatiblity, we will start by allowing most ObjectRef to be nullable.
However, we should start to use Optional<T> as the type in places where
we know nullable is a requirement. Gradually, we will move most of the ObjectRef
to be non-nullable and use Optional<T> in the nullable cases.

Such explicitness in typing can help reduce the potential problems
in our codebase overall.

Changes in this PR:
- Introduce _type_is_nullable attribute to ObjectRef
- Introduce Optional<T>
- Change String to be non-nullable.
- Change the API of function->GetAttr to return Optional<T>
Copy link
Member

@junrushao junrushao left a comment

Choose a reason for hiding this comment

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

Looks good to me :-)

@jroesch
Copy link
Member

jroesch commented Apr 13, 2020

I'll review Monday morning.

Copy link
Contributor

@MarisaKirisame MarisaKirisame left a comment

Choose a reason for hiding this comment

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

LGTM. Does it make sense to expose optional to python as well?

@tqchen tqchen merged commit fc75de9 into apache:master Apr 13, 2020
@tqchen
Copy link
Member Author

tqchen commented Apr 13, 2020

@MarisaKirisame we don't have to expose optional to python as we can directly use python's Optional for typing. When the normal object typing is used, i believe they are not nullable, the runtime python object themselves are nullable.

@tqchen tqchen deleted the optional branch April 13, 2020 17:57
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Apr 16, 2020
…pache#5314)

* [RUNTIME] Allow non-nullable ObjectRef, introduce Optional<T>.

We use ObjectRef and their sub-classes extensively throughout our codebase.
Each of ObjectRef's sub-classes are nullable, which means they can hold nullptr
as their values.

While in some places we need nullptr as an alternative value. The implicit support
for nullptr in all ObjectRef creates additional burdens for the developer
to explicitly check defined in many places of the codebase.

Moreover, it is unclear from the API's intentional point of view whether
we want a nullable object or not-null version(many cases we want the later).

Borrowing existing wisdoms from languages like Rust. We propose to
introduce non-nullable ObjectRef, and Optional<T> container that
represents a nullable variant.

To keep backward compatiblity, we will start by allowing most ObjectRef to be nullable.
However, we should start to use Optional<T> as the type in places where
we know nullable is a requirement. Gradually, we will move most of the ObjectRef
to be non-nullable and use Optional<T> in the nullable cases.

Such explicitness in typing can help reduce the potential problems
in our codebase overall.

Changes in this PR:
- Introduce _type_is_nullable attribute to ObjectRef
- Introduce Optional<T>
- Change String to be non-nullable.
- Change the API of function->GetAttr to return Optional<T>

* Address review comments

* Upgrade all compiler flags to c++14

* Update as per review comment
zhiics pushed a commit to neo-ai/tvm that referenced this pull request Apr 17, 2020
…pache#5314)

* [RUNTIME] Allow non-nullable ObjectRef, introduce Optional<T>.

We use ObjectRef and their sub-classes extensively throughout our codebase.
Each of ObjectRef's sub-classes are nullable, which means they can hold nullptr
as their values.

While in some places we need nullptr as an alternative value. The implicit support
for nullptr in all ObjectRef creates additional burdens for the developer
to explicitly check defined in many places of the codebase.

Moreover, it is unclear from the API's intentional point of view whether
we want a nullable object or not-null version(many cases we want the later).

Borrowing existing wisdoms from languages like Rust. We propose to
introduce non-nullable ObjectRef, and Optional<T> container that
represents a nullable variant.

To keep backward compatiblity, we will start by allowing most ObjectRef to be nullable.
However, we should start to use Optional<T> as the type in places where
we know nullable is a requirement. Gradually, we will move most of the ObjectRef
to be non-nullable and use Optional<T> in the nullable cases.

Such explicitness in typing can help reduce the potential problems
in our codebase overall.

Changes in this PR:
- Introduce _type_is_nullable attribute to ObjectRef
- Introduce Optional<T>
- Change String to be non-nullable.
- Change the API of function->GetAttr to return Optional<T>

* Address review comments

* Upgrade all compiler flags to c++14

* Update as per review comment
dpankratz pushed a commit to dpankratz/incubator-tvm that referenced this pull request Apr 24, 2020
…pache#5314)

* [RUNTIME] Allow non-nullable ObjectRef, introduce Optional<T>.

We use ObjectRef and their sub-classes extensively throughout our codebase.
Each of ObjectRef's sub-classes are nullable, which means they can hold nullptr
as their values.

While in some places we need nullptr as an alternative value. The implicit support
for nullptr in all ObjectRef creates additional burdens for the developer
to explicitly check defined in many places of the codebase.

Moreover, it is unclear from the API's intentional point of view whether
we want a nullable object or not-null version(many cases we want the later).

Borrowing existing wisdoms from languages like Rust. We propose to
introduce non-nullable ObjectRef, and Optional<T> container that
represents a nullable variant.

To keep backward compatiblity, we will start by allowing most ObjectRef to be nullable.
However, we should start to use Optional<T> as the type in places where
we know nullable is a requirement. Gradually, we will move most of the ObjectRef
to be non-nullable and use Optional<T> in the nullable cases.

Such explicitness in typing can help reduce the potential problems
in our codebase overall.

Changes in this PR:
- Introduce _type_is_nullable attribute to ObjectRef
- Introduce Optional<T>
- Change String to be non-nullable.
- Change the API of function->GetAttr to return Optional<T>

* Address review comments

* Upgrade all compiler flags to c++14

* Update as per review comment
Lunderberg added a commit to Lunderberg/tvm that referenced this pull request Oct 27, 2022
This parameter is nullable for cases where the else block isn't
present.  Previously, it was represented as a `Stmt` holding
`nullptr`, because
`IfThenElse` (apache#3533) predates the
`Optional` utility (apache#5314).  This
commit updates to use `Optional<Stmt>` instead, and updates all usages
of `else_case`.
junrushao pushed a commit that referenced this pull request Oct 29, 2022
This parameter is nullable for cases where the else block isn't
present.  Previously, it was represented as a `Stmt` holding
`nullptr`, because
`IfThenElse` (#3533) predates the
`Optional` utility (#5314).  This
commit updates to use `Optional<Stmt>` instead, and updates all usages
of `else_case`.
xinetzone pushed a commit to daobook/tvm that referenced this pull request Nov 10, 2022
This parameter is nullable for cases where the else block isn't
present.  Previously, it was represented as a `Stmt` holding
`nullptr`, because
`IfThenElse` (apache#3533) predates the
`Optional` utility (apache#5314).  This
commit updates to use `Optional<Stmt>` instead, and updates all usages
of `else_case`.
xinetzone pushed a commit to daobook/tvm that referenced this pull request Nov 25, 2022
This parameter is nullable for cases where the else block isn't
present.  Previously, it was represented as a `Stmt` holding
`nullptr`, because
`IfThenElse` (apache#3533) predates the
`Optional` utility (apache#5314).  This
commit updates to use `Optional<Stmt>` instead, and updates all usages
of `else_case`.
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.

5 participants