From 4148ee2e95eeba489be4a5b2994778f1956432fc Mon Sep 17 00:00:00 2001 From: Nat Jeffries Date: Wed, 20 May 2020 15:44:57 -0700 Subject: [PATCH] Align example models to 64-bit boundaries to guarantee correctness for all 64-bit flatbuffer accesses. Aligning 64-bit datatypes to 32-bits can cause memory errors on some architectures. PiperOrigin-RevId: 312570183 Change-Id: I023dc868d9ec3026d23d461a21fcfe0f6251150d --- .../benchmarks/keyword_scrambled_model_data.cc | 15 ++------------- .../lite/micro/examples/hello_world/model.cc | 15 ++------------- .../examples/magic_wand/magic_wand_model_data.cc | 15 ++------------- 3 files changed, 6 insertions(+), 39 deletions(-) diff --git a/tensorflow/lite/micro/benchmarks/keyword_scrambled_model_data.cc b/tensorflow/lite/micro/benchmarks/keyword_scrambled_model_data.cc index c1e37dfb37e..834f44ca5ab 100644 --- a/tensorflow/lite/micro/benchmarks/keyword_scrambled_model_data.cc +++ b/tensorflow/lite/micro/benchmarks/keyword_scrambled_model_data.cc @@ -15,19 +15,8 @@ limitations under the License. #include "tensorflow/lite/micro/benchmarks/keyword_scrambled_model_data.h" -// We need to keep the data array aligned on some architectures. -#ifdef __has_attribute -#define HAVE_ATTRIBUTE(x) __has_attribute(x) -#else -#define HAVE_ATTRIBUTE(x) 0 -#endif -#if HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__)) -#define DATA_ALIGN_ATTRIBUTE __attribute__((aligned(4))) -#else -#define DATA_ALIGN_ATTRIBUTE -#endif - -const unsigned char g_keyword_scrambled_model_data[] DATA_ALIGN_ATTRIBUTE = { +// Keep model aligned to 8 bytes to guarantee aligned 64-bit accesses. +alignas(8) const unsigned char g_keyword_scrambled_model_data[] = { 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x14, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x08, 0x00, 0x00, 0x00, 0x04, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0xd0, 0x6e, 0x00, 0x00, diff --git a/tensorflow/lite/micro/examples/hello_world/model.cc b/tensorflow/lite/micro/examples/hello_world/model.cc index 232e4a14115..f774985fd48 100644 --- a/tensorflow/lite/micro/examples/hello_world/model.cc +++ b/tensorflow/lite/micro/examples/hello_world/model.cc @@ -24,19 +24,8 @@ limitations under the License. #include "tensorflow/lite/micro/examples/hello_world/model.h" -// We need to keep the data array aligned on some architectures. -#ifdef __has_attribute -#define HAVE_ATTRIBUTE(x) __has_attribute(x) -#else -#define HAVE_ATTRIBUTE(x) 0 -#endif -#if HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__)) -#define DATA_ALIGN_ATTRIBUTE __attribute__((aligned(4))) -#else -#define DATA_ALIGN_ATTRIBUTE -#endif - -const unsigned char g_model[] DATA_ALIGN_ATTRIBUTE = { +// Keep model aligned to 8 bytes to guarantee aligned 64-bit accesses. +alignas(8) const unsigned char g_model[] = { 0x1c, 0x00, 0x00, 0x00, 0x54, 0x46, 0x4c, 0x33, 0x00, 0x00, 0x12, 0x00, 0x1c, 0x00, 0x04, 0x00, 0x08, 0x00, 0x0c, 0x00, 0x10, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x00, 0x12, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, diff --git a/tensorflow/lite/micro/examples/magic_wand/magic_wand_model_data.cc b/tensorflow/lite/micro/examples/magic_wand/magic_wand_model_data.cc index 1b8dca8eb0a..d56571dfd6f 100644 --- a/tensorflow/lite/micro/examples/magic_wand/magic_wand_model_data.cc +++ b/tensorflow/lite/micro/examples/magic_wand/magic_wand_model_data.cc @@ -19,19 +19,8 @@ limitations under the License. #include "tensorflow/lite/micro/examples/magic_wand/magic_wand_model_data.h" -// We need to keep the data array aligned on some architectures. -#ifdef __has_attribute -#define HAVE_ATTRIBUTE(x) __has_attribute(x) -#else -#define HAVE_ATTRIBUTE(x) 0 -#endif -#if HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__)) -#define DATA_ALIGN_ATTRIBUTE __attribute__((aligned(4))) -#else -#define DATA_ALIGN_ATTRIBUTE -#endif - -const unsigned char g_magic_wand_model_data[] DATA_ALIGN_ATTRIBUTE = { +// Keep model aligned to 8 bytes to guarantee aligned 64-bit accesses. +alignas(8) const unsigned char g_magic_wand_model_data[] = { 0x1c, 0x00, 0x00, 0x00, 0x54, 0x46, 0x4c, 0x33, 0x00, 0x00, 0x12, 0x00, 0x1c, 0x00, 0x04, 0x00, 0x08, 0x00, 0x0c, 0x00, 0x10, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x00, 0x12, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,