From d42d8c538d4d5a0f9ed570018e0e5bacc2b2c6b5 Mon Sep 17 00:00:00 2001 From: Terry Heo Date: Wed, 22 Jul 2020 02:07:52 -0700 Subject: [PATCH] tflite: Refactor core/macros.h, c/c_api.h and c/common.h - Move TFL_CAPI_EXPORT to c/common.h to make them available for delegates. - Create lite:macros module to make core/macros.h available for delegates. PiperOrigin-RevId: 322532576 Change-Id: I8cef05e5f85592e9b29c778a6c91e87b09f6f7cd --- tensorflow/lite/BUILD | 6 +++++- tensorflow/lite/c/c_api.h | 14 -------------- tensorflow/lite/c/common.h | 16 ++++++++++++++++ tensorflow/lite/delegates/flex/BUILD | 1 + tensorflow/lite/delegates/flex/delegate.cc | 3 ++- .../lite/delegates/gpu/cl/gpu_api_delegate.h | 14 -------------- tensorflow/lite/delegates/gpu/delegate.h | 14 -------------- tensorflow/lite/delegates/gpu/gl_delegate.h | 14 -------------- tensorflow/lite/delegates/gpu/metal_delegate.h | 14 +------------- .../lite/delegates/hexagon/hexagon_delegate.h | 14 -------------- .../tools/benchmark/experimental/c/c_api_types.h | 16 ++++++++++++++++ 11 files changed, 41 insertions(+), 85 deletions(-) diff --git a/tensorflow/lite/BUILD b/tensorflow/lite/BUILD index 1c0882ef0aa..61b9972c4d9 100644 --- a/tensorflow/lite/BUILD +++ b/tensorflow/lite/BUILD @@ -650,10 +650,14 @@ cc_test( cc_library( name = "shared_library", hdrs = ["shared_library.h"], - copts = TFLITE_DEFAULT_COPTS, linkopts = if_not_windows(["-ldl"]), ) +cc_library( + name = "macros", + hdrs = ["core/macros.h"], +) + # Shared lib target for convenience, pulls in the core runtime and builtin ops. # Note: This target is not yet finalized, and the exact set of exported (C/C++) # APIs is subject to change. The output library name is platform dependent: diff --git a/tensorflow/lite/c/c_api.h b/tensorflow/lite/c/c_api.h index 754fc3b8bbd..880b80e69b4 100644 --- a/tensorflow/lite/c/c_api.h +++ b/tensorflow/lite/c/c_api.h @@ -66,20 +66,6 @@ limitations under the License. /// TfLiteInterpreterOptionsDelete(options); /// TfLiteModelDelete(model); -#ifdef SWIG -#define TFL_CAPI_EXPORT -#else -#if defined(_WIN32) -#ifdef TFL_COMPILE_LIBRARY -#define TFL_CAPI_EXPORT __declspec(dllexport) -#else -#define TFL_CAPI_EXPORT __declspec(dllimport) -#endif // TFL_COMPILE_LIBRARY -#else -#define TFL_CAPI_EXPORT __attribute__((visibility("default"))) -#endif // _WIN32 -#endif // SWIG - #ifdef __cplusplus extern "C" { #endif // __cplusplus diff --git a/tensorflow/lite/c/common.h b/tensorflow/lite/c/common.h index 0979c4dddf8..615b5fbaa45 100644 --- a/tensorflow/lite/c/common.h +++ b/tensorflow/lite/c/common.h @@ -233,6 +233,22 @@ void TfLiteFloatArrayFree(TfLiteFloatArray* a); } \ } while (0) +// Define TFL_CAPI_EXPORT macro to export a function properly with a shared +// library. +#ifdef SWIG +#define TFL_CAPI_EXPORT +#else +#if defined(_WIN32) +#ifdef TFL_COMPILE_LIBRARY +#define TFL_CAPI_EXPORT __declspec(dllexport) +#else +#define TFL_CAPI_EXPORT __declspec(dllimport) +#endif // TFL_COMPILE_LIBRARY +#else +#define TFL_CAPI_EXPORT __attribute__((visibility("default"))) +#endif // _WIN32 +#endif // SWIG + // Single-precision complex data type compatible with the C99 definition. typedef struct TfLiteComplex64 { float re, im; // real and imaginary parts, respectively. diff --git a/tensorflow/lite/delegates/flex/BUILD b/tensorflow/lite/delegates/flex/BUILD index 8320ecebf9a..7039a9fa952 100644 --- a/tensorflow/lite/delegates/flex/BUILD +++ b/tensorflow/lite/delegates/flex/BUILD @@ -92,6 +92,7 @@ cc_library( "//tensorflow/lite/core/api", "//tensorflow/lite/c:common", "//tensorflow/lite:kernel_api", + "//tensorflow/lite:macros", "//tensorflow/lite:minimal_logging", "//tensorflow/lite:string", "//tensorflow/lite:string_util", diff --git a/tensorflow/lite/delegates/flex/delegate.cc b/tensorflow/lite/delegates/flex/delegate.cc index f85b5e60f91..4664ab34700 100644 --- a/tensorflow/lite/delegates/flex/delegate.cc +++ b/tensorflow/lite/delegates/flex/delegate.cc @@ -20,6 +20,7 @@ limitations under the License. #include "absl/strings/str_cat.h" #include "tensorflow/core/lib/core/status.h" #include "tensorflow/lite/context_util.h" +#include "tensorflow/lite/core/macros.h" #include "tensorflow/lite/delegates/flex/buffer_map.h" #include "tensorflow/lite/delegates/flex/kernel.h" #include "tensorflow/lite/delegates/flex/util.h" @@ -30,7 +31,7 @@ limitations under the License. namespace tflite { // Corresponding weak declaration found in lite/interpreter_builder.cc. -#if !defined(_WIN32) +#if TFLITE_HAS_ATTRIBUTE_WEAK // If weak symbol is not supported (Windows), it can use // TF_AcquireFlexDelegate() path instead. TfLiteDelegateUniquePtr AcquireFlexDelegate() { diff --git a/tensorflow/lite/delegates/gpu/cl/gpu_api_delegate.h b/tensorflow/lite/delegates/gpu/cl/gpu_api_delegate.h index e10489cc99b..81d03a9a32d 100644 --- a/tensorflow/lite/delegates/gpu/cl/gpu_api_delegate.h +++ b/tensorflow/lite/delegates/gpu/cl/gpu_api_delegate.h @@ -28,20 +28,6 @@ limitations under the License. #include "tensorflow/lite/c/common.h" #include "tensorflow/lite/delegates/gpu/delegate.h" -#ifdef SWIG -#define TFL_CAPI_EXPORT -#else -#if defined(_WIN32) -#ifdef TFL_COMPILE_LIBRARY -#define TFL_CAPI_EXPORT __declspec(dllexport) -#else -#define TFL_CAPI_EXPORT __declspec(dllimport) -#endif // TFL_COMPILE_LIBRARY -#else -#define TFL_CAPI_EXPORT __attribute__((visibility("default"))) -#endif // _WIN32 -#endif // SWIG - #ifdef __cplusplus extern "C" { #endif // __cplusplus diff --git a/tensorflow/lite/delegates/gpu/delegate.h b/tensorflow/lite/delegates/gpu/delegate.h index f03392d9a3c..9af586bfd75 100644 --- a/tensorflow/lite/delegates/gpu/delegate.h +++ b/tensorflow/lite/delegates/gpu/delegate.h @@ -20,20 +20,6 @@ limitations under the License. #include "tensorflow/lite/c/common.h" -#ifdef SWIG -#define TFL_CAPI_EXPORT -#else -#if defined(_WIN32) -#ifdef TFL_COMPILE_LIBRARY -#define TFL_CAPI_EXPORT __declspec(dllexport) -#else -#define TFL_CAPI_EXPORT __declspec(dllimport) -#endif // TFL_COMPILE_LIBRARY -#else -#define TFL_CAPI_EXPORT __attribute__((visibility("default"))) -#endif // _WIN32 -#endif // SWIG - #ifdef __cplusplus extern "C" { #endif // __cplusplus diff --git a/tensorflow/lite/delegates/gpu/gl_delegate.h b/tensorflow/lite/delegates/gpu/gl_delegate.h index fa8eec2ad6b..e6efd646fc3 100644 --- a/tensorflow/lite/delegates/gpu/gl_delegate.h +++ b/tensorflow/lite/delegates/gpu/gl_delegate.h @@ -22,20 +22,6 @@ limitations under the License. #include "absl/base/macros.h" #include "tensorflow/lite/c/common.h" -#ifdef SWIG -#define TFL_CAPI_EXPORT -#else -#if defined(_WIN32) -#ifdef TFL_COMPILE_LIBRARY -#define TFL_CAPI_EXPORT __declspec(dllexport) -#else -#define TFL_CAPI_EXPORT __declspec(dllimport) -#endif // TFL_COMPILE_LIBRARY -#else -#define TFL_CAPI_EXPORT __attribute__((visibility("default"))) -#endif // _WIN32 -#endif // SWIG - #ifdef __cplusplus extern "C" { #endif // __cplusplus diff --git a/tensorflow/lite/delegates/gpu/metal_delegate.h b/tensorflow/lite/delegates/gpu/metal_delegate.h index 1cb660c42d0..e4bdba36799 100644 --- a/tensorflow/lite/delegates/gpu/metal_delegate.h +++ b/tensorflow/lite/delegates/gpu/metal_delegate.h @@ -16,19 +16,7 @@ limitations under the License. #ifndef TENSORFLOW_LITE_DELEGATES_GPU_METAL_DELEGATE_H_ #define TENSORFLOW_LITE_DELEGATES_GPU_METAL_DELEGATE_H_ -#ifdef SWIG -#define TFL_CAPI_EXPORT -#else -#if defined(_WIN32) -#ifdef TFL_COMPILE_LIBRARY -#define TFL_CAPI_EXPORT __declspec(dllexport) -#else -#define TFL_CAPI_EXPORT __declspec(dllimport) -#endif // TFL_COMPILE_LIBRARY -#else -#define TFL_CAPI_EXPORT __attribute__((visibility("default"))) -#endif // _WIN32 -#endif // SWIG +#include "tensorflow/lite/c/common.h" #ifdef __cplusplus extern "C" { diff --git a/tensorflow/lite/delegates/hexagon/hexagon_delegate.h b/tensorflow/lite/delegates/hexagon/hexagon_delegate.h index 264a132b995..931b02e4fa5 100644 --- a/tensorflow/lite/delegates/hexagon/hexagon_delegate.h +++ b/tensorflow/lite/delegates/hexagon/hexagon_delegate.h @@ -17,20 +17,6 @@ limitations under the License. #include "tensorflow/lite/c/common.h" -#ifdef SWIG -#define TFL_CAPI_EXPORT -#else -#if defined(_WIN32) -#ifdef TFL_COMPILE_LIBRARY -#define TFL_CAPI_EXPORT __declspec(dllexport) -#else -#define TFL_CAPI_EXPORT __declspec(dllimport) -#endif // TFL_COMPILE_LIBRARY -#else -#define TFL_CAPI_EXPORT __attribute__((visibility("default"))) -#endif // _WIN32 -#endif // SWIG - #ifdef __cplusplus extern "C" { #endif // __cplusplus diff --git a/tensorflow/lite/tools/benchmark/experimental/c/c_api_types.h b/tensorflow/lite/tools/benchmark/experimental/c/c_api_types.h index 0979c4dddf8..615b5fbaa45 100644 --- a/tensorflow/lite/tools/benchmark/experimental/c/c_api_types.h +++ b/tensorflow/lite/tools/benchmark/experimental/c/c_api_types.h @@ -233,6 +233,22 @@ void TfLiteFloatArrayFree(TfLiteFloatArray* a); } \ } while (0) +// Define TFL_CAPI_EXPORT macro to export a function properly with a shared +// library. +#ifdef SWIG +#define TFL_CAPI_EXPORT +#else +#if defined(_WIN32) +#ifdef TFL_COMPILE_LIBRARY +#define TFL_CAPI_EXPORT __declspec(dllexport) +#else +#define TFL_CAPI_EXPORT __declspec(dllimport) +#endif // TFL_COMPILE_LIBRARY +#else +#define TFL_CAPI_EXPORT __attribute__((visibility("default"))) +#endif // _WIN32 +#endif // SWIG + // Single-precision complex data type compatible with the C99 definition. typedef struct TfLiteComplex64 { float re, im; // real and imaginary parts, respectively.