From 49b1b6e6148c693409797253319cd7a01cf663ea Mon Sep 17 00:00:00 2001 From: Jared Duke Date: Thu, 1 Aug 2019 10:33:09 -0700 Subject: [PATCH] Avoid c11 usage in TFLite's C API Dno't redefine typedefs, but rather use forward declarations and explicit struct keywords where necessary. Note that this style was already used for the TfLiteContext type. PiperOrigin-RevId: 261148927 --- tensorflow/lite/c/c_api_internal.h | 36 ++++++++++--------- tensorflow/lite/experimental/c/c_api_types.h | 36 ++++++++++--------- .../interpreter_wrapper/interpreter_wrapper.h | 3 +- 3 files changed, 39 insertions(+), 36 deletions(-) diff --git a/tensorflow/lite/c/c_api_internal.h b/tensorflow/lite/c/c_api_internal.h index e1c54cba9b3..c31d3e50cc0 100644 --- a/tensorflow/lite/c/c_api_internal.h +++ b/tensorflow/lite/c/c_api_internal.h @@ -51,7 +51,11 @@ typedef enum { kTfLiteMaxExternalContexts = 4 } TfLiteExternalContextType; +// Forward declare so dependent structs and methods can reference these types +// prior to the struct definitions. struct TfLiteContext; +struct TfLiteDelegate; +struct TfLiteRegistration; // An external context is a collection of information unrelated to the TF Lite // framework, but useful to a subset of the ops. TF Lite knows very little @@ -63,10 +67,6 @@ typedef struct { TfLiteStatus (*Refresh)(struct TfLiteContext* context); } TfLiteExternalContext; -// Forward declare so GetNode can use this is in Context. -typedef struct _TfLiteRegistration TfLiteRegistration; -typedef struct _TfLiteDelegate TfLiteDelegate; - #define kOptionalTensor (-1) // Fixed size list of integers. Used for dimensions and inputs/outputs tensor @@ -330,7 +330,7 @@ typedef struct { // The delegate which knows how to handle `buffer_handle`. // WARNING: This is an experimental interface that is subject to change. - TfLiteDelegate* delegate; + struct TfLiteDelegate* delegate; // An integer buffer handle that can be handled by `delegate`. // The value is valid only when delegate is not null. @@ -405,7 +405,7 @@ typedef struct { // The pointer to the delegate. This is non-null only when the node is // created by calling `interpreter.ModifyGraphWithDelegate`. // WARNING: This is an experimental interface that is subject to change. - TfLiteDelegate* delegate; + struct TfLiteDelegate* delegate; } TfLiteNode; typedef struct TfLiteContext { @@ -451,15 +451,15 @@ typedef struct TfLiteContext { // Get a Tensor node by node_index. // WARNING: This is an experimental interface that is subject to change. - TfLiteStatus (*GetNodeAndRegistration)(struct TfLiteContext*, int node_index, - TfLiteNode** node, - TfLiteRegistration** registration); + TfLiteStatus (*GetNodeAndRegistration)( + struct TfLiteContext*, int node_index, TfLiteNode** node, + struct TfLiteRegistration** registration); // Replace ops with one or more stub delegate operations. This function // does not take ownership of `nodes_to_replace`. TfLiteStatus (*ReplaceNodeSubsetsWithDelegateKernels)( - struct TfLiteContext*, TfLiteRegistration registration, - const TfLiteIntArray* nodes_to_replace, TfLiteDelegate* delegate); + struct TfLiteContext*, struct TfLiteRegistration registration, + const TfLiteIntArray* nodes_to_replace, struct TfLiteDelegate* delegate); // Number of threads that are recommended to subsystems like gemmlowp and // eigen. @@ -484,7 +484,7 @@ typedef struct TfLiteContext { void* profiler; } TfLiteContext; -typedef struct _TfLiteRegistration { +typedef struct TfLiteRegistration { // Initializes the op from serialized data. // If a built-in op: // `buffer` is the op's params data (TfLiteLSTMParams*). @@ -560,7 +560,7 @@ typedef enum { } TfLiteDelegateFlags; // WARNING: This is an experimental interface that is subject to change. -typedef struct _TfLiteDelegate { +typedef struct TfLiteDelegate { // Data that delegate needs to identify itself. This data is owned by the // delegate. The delegate is owned in the user code, so the delegate is // responsible for doing this when it is destroyed. @@ -571,20 +571,21 @@ typedef struct _TfLiteDelegate { // will look at the nodes and call ReplaceNodeSubsetsWithDelegateKernels() // to ask the TensorFlow lite runtime to create macro-nodes to represent // delegated subgraphs of the original graph. - TfLiteStatus (*Prepare)(TfLiteContext* context, TfLiteDelegate* delegate); + TfLiteStatus (*Prepare)(TfLiteContext* context, + struct TfLiteDelegate* delegate); // Copy the data from delegate buffer handle into raw memory of the given // 'tensor'. This cannot be null. The delegate is allowed to allocate the raw // bytes as long as it follows the rules for kTfLiteDynamic tensors. TfLiteStatus (*CopyFromBufferHandle)(TfLiteContext* context, - TfLiteDelegate* delegate, + struct TfLiteDelegate* delegate, TfLiteBufferHandle buffer_handle, TfLiteTensor* tensor); // Copy the data from raw memory of the given 'tensor' to delegate buffer // handle. This can be null if the delegate doesn't use its own buffer. TfLiteStatus (*CopyToBufferHandle)(TfLiteContext* context, - TfLiteDelegate* delegate, + struct TfLiteDelegate* delegate, TfLiteBufferHandle buffer_handle, TfLiteTensor* tensor); @@ -592,7 +593,8 @@ typedef struct _TfLiteDelegate { // this doesn't release the underlying resource (e.g. textures). The // resources are either owned by application layer or the delegate. // This can be null if the delegate doesn't use its own buffer. - void (*FreeBufferHandle)(TfLiteContext* context, TfLiteDelegate* delegate, + void (*FreeBufferHandle)(TfLiteContext* context, + struct TfLiteDelegate* delegate, TfLiteBufferHandle* handle); // Bitmask flags. See the comments in `TfLiteDelegateFlags`. diff --git a/tensorflow/lite/experimental/c/c_api_types.h b/tensorflow/lite/experimental/c/c_api_types.h index e1c54cba9b3..c31d3e50cc0 100644 --- a/tensorflow/lite/experimental/c/c_api_types.h +++ b/tensorflow/lite/experimental/c/c_api_types.h @@ -51,7 +51,11 @@ typedef enum { kTfLiteMaxExternalContexts = 4 } TfLiteExternalContextType; +// Forward declare so dependent structs and methods can reference these types +// prior to the struct definitions. struct TfLiteContext; +struct TfLiteDelegate; +struct TfLiteRegistration; // An external context is a collection of information unrelated to the TF Lite // framework, but useful to a subset of the ops. TF Lite knows very little @@ -63,10 +67,6 @@ typedef struct { TfLiteStatus (*Refresh)(struct TfLiteContext* context); } TfLiteExternalContext; -// Forward declare so GetNode can use this is in Context. -typedef struct _TfLiteRegistration TfLiteRegistration; -typedef struct _TfLiteDelegate TfLiteDelegate; - #define kOptionalTensor (-1) // Fixed size list of integers. Used for dimensions and inputs/outputs tensor @@ -330,7 +330,7 @@ typedef struct { // The delegate which knows how to handle `buffer_handle`. // WARNING: This is an experimental interface that is subject to change. - TfLiteDelegate* delegate; + struct TfLiteDelegate* delegate; // An integer buffer handle that can be handled by `delegate`. // The value is valid only when delegate is not null. @@ -405,7 +405,7 @@ typedef struct { // The pointer to the delegate. This is non-null only when the node is // created by calling `interpreter.ModifyGraphWithDelegate`. // WARNING: This is an experimental interface that is subject to change. - TfLiteDelegate* delegate; + struct TfLiteDelegate* delegate; } TfLiteNode; typedef struct TfLiteContext { @@ -451,15 +451,15 @@ typedef struct TfLiteContext { // Get a Tensor node by node_index. // WARNING: This is an experimental interface that is subject to change. - TfLiteStatus (*GetNodeAndRegistration)(struct TfLiteContext*, int node_index, - TfLiteNode** node, - TfLiteRegistration** registration); + TfLiteStatus (*GetNodeAndRegistration)( + struct TfLiteContext*, int node_index, TfLiteNode** node, + struct TfLiteRegistration** registration); // Replace ops with one or more stub delegate operations. This function // does not take ownership of `nodes_to_replace`. TfLiteStatus (*ReplaceNodeSubsetsWithDelegateKernels)( - struct TfLiteContext*, TfLiteRegistration registration, - const TfLiteIntArray* nodes_to_replace, TfLiteDelegate* delegate); + struct TfLiteContext*, struct TfLiteRegistration registration, + const TfLiteIntArray* nodes_to_replace, struct TfLiteDelegate* delegate); // Number of threads that are recommended to subsystems like gemmlowp and // eigen. @@ -484,7 +484,7 @@ typedef struct TfLiteContext { void* profiler; } TfLiteContext; -typedef struct _TfLiteRegistration { +typedef struct TfLiteRegistration { // Initializes the op from serialized data. // If a built-in op: // `buffer` is the op's params data (TfLiteLSTMParams*). @@ -560,7 +560,7 @@ typedef enum { } TfLiteDelegateFlags; // WARNING: This is an experimental interface that is subject to change. -typedef struct _TfLiteDelegate { +typedef struct TfLiteDelegate { // Data that delegate needs to identify itself. This data is owned by the // delegate. The delegate is owned in the user code, so the delegate is // responsible for doing this when it is destroyed. @@ -571,20 +571,21 @@ typedef struct _TfLiteDelegate { // will look at the nodes and call ReplaceNodeSubsetsWithDelegateKernels() // to ask the TensorFlow lite runtime to create macro-nodes to represent // delegated subgraphs of the original graph. - TfLiteStatus (*Prepare)(TfLiteContext* context, TfLiteDelegate* delegate); + TfLiteStatus (*Prepare)(TfLiteContext* context, + struct TfLiteDelegate* delegate); // Copy the data from delegate buffer handle into raw memory of the given // 'tensor'. This cannot be null. The delegate is allowed to allocate the raw // bytes as long as it follows the rules for kTfLiteDynamic tensors. TfLiteStatus (*CopyFromBufferHandle)(TfLiteContext* context, - TfLiteDelegate* delegate, + struct TfLiteDelegate* delegate, TfLiteBufferHandle buffer_handle, TfLiteTensor* tensor); // Copy the data from raw memory of the given 'tensor' to delegate buffer // handle. This can be null if the delegate doesn't use its own buffer. TfLiteStatus (*CopyToBufferHandle)(TfLiteContext* context, - TfLiteDelegate* delegate, + struct TfLiteDelegate* delegate, TfLiteBufferHandle buffer_handle, TfLiteTensor* tensor); @@ -592,7 +593,8 @@ typedef struct _TfLiteDelegate { // this doesn't release the underlying resource (e.g. textures). The // resources are either owned by application layer or the delegate. // This can be null if the delegate doesn't use its own buffer. - void (*FreeBufferHandle)(TfLiteContext* context, TfLiteDelegate* delegate, + void (*FreeBufferHandle)(TfLiteContext* context, + struct TfLiteDelegate* delegate, TfLiteBufferHandle* handle); // Bitmask flags. See the comments in `TfLiteDelegateFlags`. diff --git a/tensorflow/lite/python/interpreter_wrapper/interpreter_wrapper.h b/tensorflow/lite/python/interpreter_wrapper/interpreter_wrapper.h index 56fe36000c0..da3e5516743 100644 --- a/tensorflow/lite/python/interpreter_wrapper/interpreter_wrapper.h +++ b/tensorflow/lite/python/interpreter_wrapper/interpreter_wrapper.h @@ -26,8 +26,7 @@ limitations under the License. // automatically move before . #include -struct _TfLiteDelegate; -typedef struct _TfLiteDelegate TfLiteDelegate; +struct TfLiteDelegate; // We forward declare TFLite classes here to avoid exposing them to SWIG. namespace tflite {