This is an automated email from the ASF dual-hosted git repository. tqchen pushed a commit to branch refactor-s2 in repository https://gitbox.apache.org/repos/asf/tvm.git
commit b8e1d9b5723af049266b1fe589d766558740c2af Author: tqchen <[email protected]> AuthorDate: Mon Apr 21 09:31:11 2025 -0400 Cleanup boxed primitive --- include/tvm/runtime/container/boxed_primitive.h | 143 ------------------------ include/tvm/runtime/packed_func.h | 1 - src/meta_schedule/mutator/mutate_tile_size.cc | 2 +- src/node/boxed_primitive.cc | 134 ---------------------- src/runtime/boxed_primitive.cc | 65 ----------- src/target/llvm/codegen_cpu.cc | 4 +- 6 files changed, 3 insertions(+), 346 deletions(-) diff --git a/include/tvm/runtime/container/boxed_primitive.h b/include/tvm/runtime/container/boxed_primitive.h deleted file mode 100644 index 8d01b5dc17..0000000000 --- a/include/tvm/runtime/container/boxed_primitive.h +++ /dev/null @@ -1,143 +0,0 @@ -/* - * 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. - */ - -/*! - * \file tvm/runtime/container/boxed_primitive.h - * \brief Runtime container types for primitives stored as ObjectRef. - */ -#ifndef TVM_RUNTIME_CONTAINER_BOXED_PRIMITIVE_H_ -#define TVM_RUNTIME_CONTAINER_BOXED_PRIMITIVE_H_ - -#include <tvm/runtime/memory.h> -#include <tvm/runtime/object.h> - -namespace tvm { -namespace runtime { - -namespace detail { -/* \brief Provide the BoxNode<T> type traits in templated contexts - * - * The Box<T> class is used in many templated contexts, and is easier - * to have templated over the primitive type. - * - * However, much of the TVM type system depends on classes having a - * unique name. For example, the use of `Object::IsInstance` depends - * on `Object::GetOrAllocRuntimeTypeIndex`. Any duplicate names will - * result in duplicate indices, and invalid downcasting. Furthermore, - * the name must be specified in the Python FFI using - * `tvm._ffi.register_object`. This prevents use of - * `typeid(T)::name()` to build a unique name, as the name is not - * required to be human-readable or consistent across compilers. - * - * This utility struct should be specialized over the primitive type - * held by the box, to allow explicit listing of the `_type_key` and - * other similar tratis. - * - * Note: This should only contain traits that are required at runtime, - * and should *not* contain extensions for features that are only - * available at compile-time. For integration with compile-time-only - * functionality (e.g. StructuralHash, StructuralEqual), see - * `BoxNodeCompileTimeTraits` in `src/node/boxed_primitive.cc`. - */ -template <typename Prim> -struct BoxNodeRuntimeTraits; - -} // namespace detail - -template <typename Prim> -class BoxNode : public Object { - public: - /*! \brief Constructor - * - * \param value The value to be boxed - */ - explicit BoxNode(Prim value) : value(value) {} - - /*! \brief The boxed value */ - Prim value; - - static constexpr const char* _type_key = detail::BoxNodeRuntimeTraits<Prim>::_type_key; - static constexpr bool _type_has_method_visit_attrs = false; - TVM_DECLARE_FINAL_OBJECT_INFO(BoxNode, Object); -}; - -template <typename Prim> -class Box : public ObjectRef { - public: - /*! \brief Constructor - * - * \param value The value to be boxed - */ - Box(Prim value) : ObjectRef(make_object<BoxNode<Prim>>(value)) {} // NOLINT(*) - - operator Prim() const { return (*this)->value; } - - TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(Box, ObjectRef, BoxNode<Prim>); -}; - -/*! \brief Boxed version of C++ int64_t - * - * Can be used to store POD integer values as a TVM ObjectRef. Used - * for FFI handling, and for storing POD types inside TVM containers. - */ -using Int = Box<int64_t>; - -/*! \brief Boxed version of C++ double - * - * Can be used to store POD floating-point values as a TVM ObjectRef. - * Used for FFI handling, and for storing POD types inside TVM - * containers. - */ -using Float = Box<double>; - -/*! \brief Boxed version of C++ bool - * - * Can be used to store POD boolean values as a TVM ObjectRef. Used - * for FFI handling, and for storing POD types inside TVM containers. - * - * When passing from Python to C++, TVM PackedFunc conversion follow - * C++ conversion rules, and allow bool->int and int->bool - * conversions. When passing from C++ to Python, the types are - * returned as bool or int. If the C++ function uses ObjectRef to - * hold the object, a Python to C++ to Python round trip will preserve - * the distinction between bool and int. - */ -using Bool = Box<bool>; - -namespace detail { -template <> -struct BoxNodeRuntimeTraits<int64_t> { - static constexpr const char* _type_key = "runtime.BoxInt"; -}; - -template <> -struct BoxNodeRuntimeTraits<double> { - static constexpr const char* _type_key = "runtime.BoxFloat"; -}; - -template <> -struct BoxNodeRuntimeTraits<bool> { - static constexpr const char* _type_key = "runtime.BoxBool"; -}; -} // namespace detail - -} // namespace runtime -} // namespace tvm - -#endif // TVM_RUNTIME_CONTAINER_BOXED_PRIMITIVE_H_ diff --git a/include/tvm/runtime/packed_func.h b/include/tvm/runtime/packed_func.h index 0746edc195..cbfc2aacd6 100644 --- a/include/tvm/runtime/packed_func.h +++ b/include/tvm/runtime/packed_func.h @@ -27,7 +27,6 @@ #include <tvm/ffi/any.h> #include <tvm/ffi/function.h> #include <tvm/runtime/c_runtime_api.h> -#include <tvm/runtime/container/boxed_primitive.h> #include <tvm/runtime/logging.h> #include <tvm/runtime/module.h> #include <tvm/runtime/ndarray.h> diff --git a/src/meta_schedule/mutator/mutate_tile_size.cc b/src/meta_schedule/mutator/mutate_tile_size.cc index 201dee69da..03c438b0f5 100644 --- a/src/meta_schedule/mutator/mutate_tile_size.cc +++ b/src/meta_schedule/mutator/mutate_tile_size.cc @@ -135,7 +135,7 @@ void FindSampleVectorize(const Trace& trace, std::vector<Instruction>* inst, // Skip mutating the sampling instructions who have only single candidate. continue; } - const auto* d = TVM_TYPE_AS(decision, runtime::Int::ContainerType); + const auto* d = TVM_TYPE_AS(decision, IntImmNode); instructions.push_back(inst); decisions.push_back(d->value); } diff --git a/src/node/boxed_primitive.cc b/src/node/boxed_primitive.cc deleted file mode 100644 index 86596fb5ce..0000000000 --- a/src/node/boxed_primitive.cc +++ /dev/null @@ -1,134 +0,0 @@ -/* - * 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. - */ - -/*! - * \file node/boxed_primitive.cc - * - * \brief Reflection utilities for runtime-supported classes - * - * The fundamental support for boxing and unboxing of primitives - * during FFI calls is implemented in runtime/boxed_primitive.cc. In - * addition, boxed primitives may be registered with compile-time - * utilities (e.g. reflection, JSON import/export) that can provide - * additional functionality and improved debugging ability. However, - * neither these compile-time utilities nor any registration of - * `Box<Prim>` into the compile-time utilities should be included as - * part of `libtvm_runtime.so`. - * - * This file contains the registration of the `libtvm_runtime.so` - * class `Box<Prim>` for utilities that are contained in `libtvm.so`. - */ -#include <tvm/ir/attrs.h> -#include <tvm/node/node.h> -#include <tvm/node/reflection.h> -#include <tvm/runtime/registry.h> - -namespace tvm { -namespace runtime_ext { - -using runtime::Box; -using runtime::BoxNode; - -/* \brief Compile-time extension trait for runtime types - * - * Extends the use of boxed primitive during TVM's compilation step. - * - * Most TVM classes define these functions as part of the class - * definition. However, the boxed primitives must be usable at - * runtime, and so the class definition may only refer to types that - * are present in `libtvm_runtime.so`. - */ -template <typename Prim> -struct BoxNodeCompileTimeTraits { - static constexpr const std::nullptr_t VisitAttrs = nullptr; - - static void SHashReduce(const BoxNode<Prim>* node, SHashReducer hash_reduce) { - hash_reduce(node->value); - } - - static bool SEqualReduce(const BoxNode<Prim>* lhs, const BoxNode<Prim>* rhs, - SEqualReducer equal) { - return equal(lhs->value, rhs->value); - } -}; - -TVM_REGISTER_REFLECTION_VTABLE(BoxNode<int64_t>, BoxNodeCompileTimeTraits<int64_t>) - .set_creator([](const std::string& blob) -> ObjectPtr<Object> { - int64_t value = std::atoll(blob.c_str()); - return make_object<BoxNode<int64_t>>(value); - }) - .set_repr_bytes([](const Object* n) -> std::string { - int64_t value = GetRef<ObjectRef>(n).as<Box<int64_t>>().value()->value; - std::stringstream ss; - ss << value; - return ss.str(); - }); - -TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable) - .set_dispatch<BoxNode<int64_t>>([](const ObjectRef& node, ReprPrinter* p) { - auto box = Downcast<Box<int64_t>>(node); - p->stream << box->GetTypeKey() << "(" << box->value << ")"; - }); - -TVM_REGISTER_REFLECTION_VTABLE(BoxNode<bool>, BoxNodeCompileTimeTraits<bool>) - .set_creator([](const std::string& blob) -> ObjectPtr<Object> { - if (blob == "true") { - return make_object<BoxNode<bool>>(true); - } else if (blob == "false") { - return make_object<BoxNode<bool>>(false); - } else { - LOG(FATAL) << "Invalid string '" << blob << "' for boolean"; - } - }) - .set_repr_bytes([](const Object* n) -> std::string { - bool value = GetRef<ObjectRef>(n).as<Box<bool>>().value()->value; - if (value) { - return "true"; - } else { - return "false"; - } - }); - -TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable) - .set_dispatch<BoxNode<bool>>([](const ObjectRef& node, ReprPrinter* p) { - auto box = Downcast<Box<bool>>(node); - p->stream << box->GetTypeKey() << "(" << (box->value ? "true" : "false") << ")"; - }); - -TVM_REGISTER_REFLECTION_VTABLE(BoxNode<double>, BoxNodeCompileTimeTraits<double>) - .set_creator([](const std::string& blob) -> ObjectPtr<Object> { - double value = std::atof(blob.c_str()); - return make_object<BoxNode<double>>(value); - }) - .set_repr_bytes([](const Object* n) -> std::string { - double value = GetRef<ObjectRef>(n).as<Box<double>>().value()->value; - std::stringstream ss; - ss << value; - return ss.str(); - }); - -TVM_STATIC_IR_FUNCTOR(ReprPrinter, vtable) - .set_dispatch<BoxNode<double>>([](const ObjectRef& node, ReprPrinter* p) { - auto box = Downcast<Box<double>>(node); - p->stream << box->GetTypeKey() << "(" << box->value << ")"; - }); - -} // namespace runtime_ext - -} // namespace tvm diff --git a/src/runtime/boxed_primitive.cc b/src/runtime/boxed_primitive.cc deleted file mode 100644 index 9ab83a7b47..0000000000 --- a/src/runtime/boxed_primitive.cc +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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. - */ - -/*! - * \file src/runtime/boxed_primitive.cc - * \brief Implementations of ObjectRef wrapper. - */ - -#include <tvm/runtime/container/boxed_primitive.h> -#include <tvm/runtime/registry.h> - -namespace tvm { -namespace runtime { - -TVM_REGISTER_OBJECT_TYPE(BoxNode<int64_t>); -TVM_REGISTER_OBJECT_TYPE(BoxNode<double>); -TVM_REGISTER_OBJECT_TYPE(BoxNode<bool>); - -/* \brief Allow explicit construction of Box<bool> - * - * Convert a `bool` to `Box<bool>`. For use in FFI handling, to - * provide an umambiguous representation between `bool(true)` and - * `int(1)`. Will be automatically unboxed in the case where a - * `Box<bool>` is provided to a PackedFunc that requires `int` input, - * mimicking C++'s default conversions. - * - * This is only needed for Box<bool>, as Box<double> and Box<int64_t> - * can be converted in C++ as part of `TVMArgValue::operator - * ObjectRef()` without ambiguity, postponing conversions until - * required. - */ -TVM_REGISTER_GLOBAL("runtime.BoxBool").set_body_typed([](bool value) { return Box(value); }); - -/* \brief Return the underlying boolean object. - * - * Used while unboxing a boolean return value during FFI handling. - * The return type is intentionally `int` and not `bool`, to avoid - * recursive unwrapping of boolean values. - * - * This is only needed for Box<bool>, as Box<double> and Box<int64_t> - * can be unambiguously unboxed as part of - * `TVMRetValue::operator=(ObjectRef)`. - */ -TVM_REGISTER_GLOBAL("runtime.UnBoxBool").set_body_typed([](Box<bool> obj) -> int { - return obj->value; -}); - -} // namespace runtime -} // namespace tvm diff --git a/src/target/llvm/codegen_cpu.cc b/src/target/llvm/codegen_cpu.cc index b9941ff904..e8fc5e0dbb 100644 --- a/src/target/llvm/codegen_cpu.cc +++ b/src/target/llvm/codegen_cpu.cc @@ -875,11 +875,11 @@ CodeGenCPU::PackedCall CodeGenCPU::MakeCallPackedLowered(const Array<PrimExpr>& #endif pc.ret_value = CreateCast(r_api_type, r_type, rvalue); + llvm::Value* result_type_index = + builder_->CreateInBoundsGEP(t_tvm_ffi_any_, result, {ConstInt32(0), ConstInt32(0)}); // Load the return type code. #if TVM_LLVM_VERSION >= 110 - llvm::Value* result_type_index = - builder_->CreateInBoundsGEP(t_tvm_ffi_any_, result, {ConstInt32(0), ConstInt32(0)}); pc.ret_type_index = builder_->CreateAlignedLoad(t_int32_, result_type_index, llvm::Align(4)); #elif TVM_LLVM_VERSION >= 80 pc.ret_type_index = builder_->CreateAlignedLoad(t_int32_, result_type_index, 8);
