diff --git a/extension/pytree/function_ref.h b/extension/pytree/function_ref.h index c81add169c8..aba9f87c2df 100644 --- a/extension/pytree/function_ref.h +++ b/extension/pytree/function_ref.h @@ -6,117 +6,19 @@ * LICENSE file in the root directory of this source tree. */ -//===- llvm/ADT/STLFunctionalExtras.h - Extras for -*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// This file contains some extension to . -// -// No library is required when using these functions. -// -//===----------------------------------------------------------------------===// -// Extra additions to -//===----------------------------------------------------------------------===// - -/// An efficient, type-erasing, non-owning reference to a callable. This is -/// intended for use as the type of a function parameter that is not used -/// after the function in question returns. -/// -/// This class does not own the callable, so it is not in general safe to store -/// a FunctionRef. - -// torch::executor: modified from llvm::function_ref -// - renamed to FunctionRef -// - removed LLVM_GSL_POINTER and LLVM_LIFETIME_BOUND macro uses -// - use namespaced internal::remove_cvref_t - #pragma once -#include -#include -#include - -namespace executorch { -namespace extension { -namespace pytree { - -//===----------------------------------------------------------------------===// -// Features from C++20 -//===----------------------------------------------------------------------===// - -namespace internal { - -template -struct remove_cvref { - using type = - typename std::remove_cv::type>::type; -}; - -template -using remove_cvref_t = typename remove_cvref::type; - -} // namespace internal - -template -class FunctionRef; - -template -class FunctionRef { - Ret (*callback)(intptr_t callable, Params... params) = nullptr; - intptr_t callable; - - template - static Ret callback_fn(intptr_t callable, Params... params) { - return (*reinterpret_cast(callable))( - std::forward(params)...); - } - - public: - FunctionRef() = default; - FunctionRef(std::nullptr_t) {} - - template - FunctionRef( - Callable&& callable, - // This is not the copy-constructor. - std::enable_if_t, - FunctionRef>::value>* = nullptr, - // Functor must be callable and return a suitable type. - std::enable_if_t< - std::is_void::value || - std::is_convertible< - decltype(std::declval()(std::declval()...)), - Ret>::value>* = nullptr) - : callback(callback_fn>), - callable(reinterpret_cast(&callable)) {} - - Ret operator()(Params... params) const { - return callback(callable, std::forward(params)...); - } +#include - explicit operator bool() const { - return callback; - } +/// This header is DEPRECATED; use executorch/runtime/core/function_ref.h +/// directly instead. - bool operator==(const FunctionRef& Other) const { - return callable == Other.callable; - } -}; -} // namespace pytree -} // namespace extension -} // namespace executorch +namespace executorch::extension::pytree { +using executorch::runtime::FunctionRef; +} // namespace executorch::extension::pytree -namespace torch { -namespace executor { -namespace pytree { +namespace torch::executor::pytree { // TODO(T197294990): Remove these deprecated aliases once all users have moved // to the new `::executorch` namespaces. using ::executorch::extension::pytree::FunctionRef; -} // namespace pytree -} // namespace executor -} // namespace torch +} // namespace torch::executor::pytree diff --git a/extension/pytree/test/CMakeLists.txt b/extension/pytree/test/CMakeLists.txt index 5d99bad1339..ce9b2cec6ec 100644 --- a/extension/pytree/test/CMakeLists.txt +++ b/extension/pytree/test/CMakeLists.txt @@ -19,6 +19,6 @@ set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..) include(${EXECUTORCH_ROOT}/tools/cmake/Test.cmake) -set(_test_srcs function_ref_test.cpp test_pytree.cpp) +set(_test_srcs test_pytree.cpp) et_cxx_test(extension_pytree_test SOURCES ${_test_srcs} EXTRA_LIBS) diff --git a/extension/pytree/test/TARGETS b/extension/pytree/test/TARGETS index 190bdb0bc67..e49e8cd2791 100644 --- a/extension/pytree/test/TARGETS +++ b/extension/pytree/test/TARGETS @@ -10,12 +10,6 @@ cpp_unittest( deps = ["//executorch/extension/pytree:pytree"], ) -cpp_unittest( - name = "function_ref_test", - srcs = ["function_ref_test.cpp"], - deps = ["//executorch/extension/pytree:pytree"], -) - python_unittest( name = "pybindings_test", srcs = [ diff --git a/runtime/core/function_ref.h b/runtime/core/function_ref.h new file mode 100644 index 00000000000..07d3d582b0c --- /dev/null +++ b/runtime/core/function_ref.h @@ -0,0 +1,108 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. + */ + +//===- llvm/ADT/STLFunctionalExtras.h - Extras for -*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file contains some extension to . +// +// No library is required when using these functions. +// +//===----------------------------------------------------------------------===// +// Extra additions to +//===----------------------------------------------------------------------===// + +/// An efficient, type-erasing, non-owning reference to a callable. This is +/// intended for use as the type of a function parameter that is not used +/// after the function in question returns. +/// +/// This class does not own the callable, so it is not in general safe to store +/// a FunctionRef. + +// torch::executor: modified from llvm::function_ref +// - renamed to FunctionRef +// - removed LLVM_GSL_POINTER and LLVM_LIFETIME_BOUND macro uses +// - use namespaced internal::remove_cvref_t + +#pragma once + +#include +#include +#include + +namespace executorch::runtime { + +//===----------------------------------------------------------------------===// +// Features from C++20 +//===----------------------------------------------------------------------===// + +namespace internal { + +template +struct remove_cvref { + using type = + typename std::remove_cv::type>::type; +}; + +template +using remove_cvref_t = typename remove_cvref::type; + +} // namespace internal + +template +class FunctionRef; + +template +class FunctionRef { + Ret (*callback)(intptr_t callable, Params... params) = nullptr; + intptr_t callable; + + template + static Ret callback_fn(intptr_t callable, Params... params) { + return (*reinterpret_cast(callable))( + std::forward(params)...); + } + + public: + FunctionRef() = default; + FunctionRef(std::nullptr_t) {} + + template + FunctionRef( + Callable&& callable, + // This is not the copy-constructor. + std::enable_if_t, + FunctionRef>::value>* = nullptr, + // Functor must be callable and return a suitable type. + std::enable_if_t< + std::is_void::value || + std::is_convertible< + decltype(std::declval()(std::declval()...)), + Ret>::value>* = nullptr) + : callback(callback_fn>), + callable(reinterpret_cast(&callable)) {} + + Ret operator()(Params... params) const { + return callback(callable, std::forward(params)...); + } + + explicit operator bool() const { + return callback; + } + + bool operator==(const FunctionRef& Other) const { + return callable == Other.callable; + } +}; +} // namespace executorch::runtime diff --git a/runtime/core/targets.bzl b/runtime/core/targets.bzl index d3e02b1afb5..efc7853f3c1 100644 --- a/runtime/core/targets.bzl +++ b/runtime/core/targets.bzl @@ -41,6 +41,7 @@ def define_common_targets(): "defines.h", "error.h", "freeable_buffer.h", + "function_ref.h", "result.h", "span.h", ], diff --git a/runtime/core/test/CMakeLists.txt b/runtime/core/test/CMakeLists.txt index 70f7cbf4bfd..bdc427baf7d 100644 --- a/runtime/core/test/CMakeLists.txt +++ b/runtime/core/test/CMakeLists.txt @@ -20,14 +20,15 @@ set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..) include(${EXECUTORCH_ROOT}/tools/cmake/Test.cmake) set(_test_srcs - span_test.cpp + array_ref_test.cpp error_handling_test.cpp + evalue_test.cpp event_tracer_test.cpp freeable_buffer_test.cpp - array_ref_test.cpp - memory_allocator_test.cpp + function_ref_test.cpp hierarchical_allocator_test.cpp - evalue_test.cpp + memory_allocator_test.cpp + span_test.cpp ) et_cxx_test(runtime_core_test SOURCES ${_test_srcs} EXTRA_LIBS) diff --git a/extension/pytree/test/function_ref_test.cpp b/runtime/core/test/function_ref_test.cpp similarity index 91% rename from extension/pytree/test/function_ref_test.cpp rename to runtime/core/test/function_ref_test.cpp index cdb2c0538fd..beef00893cf 100644 --- a/extension/pytree/test/function_ref_test.cpp +++ b/runtime/core/test/function_ref_test.cpp @@ -6,13 +6,13 @@ * LICENSE file in the root directory of this source tree. */ -#include +#include #include using namespace ::testing; -using ::executorch::extension::pytree::FunctionRef; +using ::executorch::runtime::FunctionRef; namespace { void one(int32_t& i) { diff --git a/runtime/core/test/targets.bzl b/runtime/core/test/targets.bzl index 7db74475c92..1ad0940c62e 100644 --- a/runtime/core/test/targets.bzl +++ b/runtime/core/test/targets.bzl @@ -33,6 +33,16 @@ def define_common_targets(): ], ) + runtime.cxx_test( + name = "function_ref_test", + srcs = [ + "function_ref_test.cpp", + ], + deps = [ + "//executorch/runtime/core:core", + ], + ) + runtime.cxx_test( name = "event_tracer_test", srcs = [ diff --git a/test/utils/OSSTestConfig.json b/test/utils/OSSTestConfig.json index be594f9d5f4..2cfc4b8a995 100644 --- a/test/utils/OSSTestConfig.json +++ b/test/utils/OSSTestConfig.json @@ -45,7 +45,6 @@ { "directory": "extension/pytree/test", "sources": [ - "function_ref_test.cpp", "test_pytree.cpp" ] }, @@ -96,14 +95,15 @@ { "directory": "runtime/core/test", "sources": [ - "span_test.cpp", + "array_ref_test.cpp", "error_handling_test.cpp", + "evalue_test.cpp", "event_tracer_test.cpp", "freeable_buffer_test.cpp", - "array_ref_test.cpp", - "memory_allocator_test.cpp", + "function_ref_test.cpp", "hierarchical_allocator_test.cpp", - "evalue_test.cpp" + "memory_allocator_test.cpp", + "span_test.cpp" ] }, {