Files
apache--tvm/tests/cpp/object_protocol_test.cc
Tianqi Chen 556571fc9d [REFACTOR][RUNTIME] Phase out include/tvm/runtime/object.h (#19476)
## Summary

include/tvm/runtime/object.h was a vestige of the pre-tvm-ffi world — a
thin compat layer re-exporting
Object/ObjectRef/ObjectPtr/GetRef/GetObjectPtr
aliases into tvm::runtime:: and tvm::, plus a few TVM-specific macros
and
an enum TypeIndex with mostly-dead constants.

This PR phases the header out entirely, with no shim:

- `TVM_DEFINE_OBJECT_REF_COW_METHOD` relocated to a new
`include/tvm/ir/cow.h`
  (its consumer set is entirely IR/TIRX/relax/arith/te).
-
`TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE_WITHOUT_DEFAULT_CONSTRUCTOR`
  inlined at its 2 callers (rare; not worth a new home).
- `TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN` inlined at its 1 caller.
- `TVM_STR_CONCAT` removed; callers switch to `TVM_FFI_STR_CONCAT`
  (already in tvm-ffi; the local copy was a duplicate).
- `kRuntimeRPCObjectRef` / `kRuntimeDiscoDRef` inlined into
`rpc_session.h`
  / `disco/session.h` respectively (the only live type-index constants).
- All using-aliases (`tvm::runtime::Object` etc.) rewritten to fully-
qualified `tvm::ffi::Object` at use sites — no using-injections
anywhere.
- `include/tvm/runtime/object.h` deleted.

## Test plan

- ninja build clean (USE_LLVM=ON, default targets) — exit 0.
- ./cpptest — 118/118.
- pytest tests/python/all-platform-minimal-test/ — green.
- pytest tests/python/runtime/ — green.
- pre-commit clean.
2026-04-29 20:05:42 -04:00

86 lines
2.8 KiB
C++

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <gtest/gtest.h>
#include <tvm/ffi/memory.h>
#include <tvm/runtime/logging.h>
namespace tvm {
namespace test {
using namespace tvm::runtime;
class ObjBase : public ffi::Object {
public:
// dynamically allocate slow
static constexpr const uint32_t _type_child_slots = 1;
TVM_FFI_DECLARE_OBJECT_INFO("test.ObjBase", ObjBase, ffi::Object);
};
class ObjA : public ObjBase {
public:
static constexpr const uint32_t _type_child_slots = 0;
TVM_FFI_DECLARE_OBJECT_INFO("test.ObjA", ObjA, ObjBase);
};
class ObjB : public ObjBase {
public:
static constexpr const uint32_t _type_child_slots = 0;
TVM_FFI_DECLARE_OBJECT_INFO("test.ObjB", ObjB, ObjBase);
};
class ObjAA : public ObjA {
public:
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("test.ObjAA", ObjAA, ObjA);
};
} // namespace test
} // namespace tvm
TEST(ObjectHierachy, Basic) {
using namespace tvm;
using namespace tvm::runtime;
using namespace tvm::test;
using namespace tvm::ffi;
ffi::ObjectRef refA(ffi::make_object<ObjA>());
TVM_FFI_ICHECK_EQ(refA->type_index(), ObjA::RuntimeTypeIndex());
TVM_FFI_ICHECK(refA.as<ffi::Object>() != nullptr);
TVM_FFI_ICHECK(refA.as<ObjA>() != nullptr);
TVM_FFI_ICHECK(refA.as<ObjBase>() != nullptr);
TVM_FFI_ICHECK(refA.as<ObjB>() == nullptr);
TVM_FFI_ICHECK(refA.as<ObjAA>() == nullptr);
ffi::ObjectRef refAA(ffi::make_object<ObjAA>());
TVM_FFI_ICHECK_EQ(refAA->type_index(), ObjAA::RuntimeTypeIndex());
TVM_FFI_ICHECK(refAA.as<ffi::Object>() != nullptr);
TVM_FFI_ICHECK(refAA.as<ObjBase>() != nullptr);
TVM_FFI_ICHECK(refAA.as<ObjA>() != nullptr);
TVM_FFI_ICHECK(refAA.as<ObjAA>() != nullptr);
TVM_FFI_ICHECK(refAA.as<ObjB>() == nullptr);
ffi::ObjectRef refB(ffi::make_object<ObjB>());
TVM_FFI_ICHECK_EQ(refB->type_index(), ObjB::RuntimeTypeIndex());
TVM_FFI_ICHECK(refB.as<ffi::Object>() != nullptr);
TVM_FFI_ICHECK(refB.as<ObjBase>() != nullptr);
TVM_FFI_ICHECK(refB.as<ObjA>() == nullptr);
TVM_FFI_ICHECK(refB.as<ObjAA>() == nullptr);
TVM_FFI_ICHECK(refB.as<ObjB>() != nullptr);
}