[RUNTIME][IR] Allow non-nullable ObjectRef, introduce Optional<T>. (#5314)
authorTianqi Chen <tqchen@users.noreply.github.com>
Mon, 13 Apr 2020 17:49:48 +0000 (10:49 -0700)
committerGitHub <noreply@github.com>
Mon, 13 Apr 2020 17:49:48 +0000 (10:49 -0700)
commitfc75de9d680ada8a8ac4b258a60c3f70de1c2e07
tree81c6be2eac115f45ff524fa1855cc2f60cb9925e
parent3df8d560f2b6d34ba43a069cd5809560d2c96983
[RUNTIME][IR] Allow non-nullable ObjectRef, introduce Optional<T>. (#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
56 files changed:
apps/android_camera/app/src/main/jni/Application.mk
apps/android_deploy/app/src/main/jni/Application.mk
apps/android_rpc/app/src/main/jni/Application.mk
apps/cpp_rpc/Makefile
apps/dso_plugin_module/Makefile
apps/extension/Makefile
apps/howto_deploy/Makefile
apps/howto_deploy/tvm_runtime_pack.cc
apps/rocm_rpc/Makefile
apps/tf_tvmdsoop/CMakeLists.txt
golang/Makefile
include/tvm/ir/attrs.h
include/tvm/ir/expr.h
include/tvm/ir/function.h
include/tvm/node/node.h
include/tvm/runtime/container.h
include/tvm/runtime/object.h
include/tvm/runtime/packed_func.h
python/setup.py
src/driver/driver_api.cc
src/relay/backend/compile_engine.cc
src/relay/backend/contrib/codegen_c/codegen_c.h
src/relay/backend/vm/compiler.cc
src/relay/backend/vm/lambda_lift.cc
src/relay/ir/transform.cc
src/relay/transforms/annotate_target.cc
src/target/build_common.h
src/target/llvm/codegen_cpu.cc
src/target/llvm/codegen_llvm.cc
src/target/llvm/llvm_module.cc
src/target/opt/build_cuda_on.cc
src/target/source/codegen_aocl.cc
src/target/source/codegen_c.cc
src/target/source/codegen_metal.cc
src/target/source/codegen_opencl.cc
src/target/source/codegen_opengl.cc
src/target/source/codegen_vhls.cc
src/target/spirv/build_vulkan.cc
src/target/spirv/codegen_spirv.cc
src/target/stackvm/codegen_stackvm.cc
src/tir/analysis/verify_memory.cc
src/tir/transforms/bind_device_type.cc
src/tir/transforms/lower_custom_datatypes.cc
src/tir/transforms/lower_intrin.cc
src/tir/transforms/lower_thread_allreduce.cc
src/tir/transforms/lower_warp_memory.cc
src/tir/transforms/make_packed_api.cc
src/tir/transforms/remap_thread_axis.cc
src/tir/transforms/split_host_device.cc
tests/cpp/container_test.cc
tests/python/relay/test_external_codegen.py
tests/python/relay/test_external_runtime.py
tests/python/relay/test_pass_annotate_target.py
tests/python/relay/test_pass_partition_graph.py
tests/python/unittest/test_runtime_module_export.py
vta/python/vta/exec/rpc_server.py