Skip to content

Commit 15b55b7

Browse files
authored
Revert back from C/C++17 to C/C++11 (#3095)
There are older toolchains that don't support C++17 and are still used in shipping products. Rewrite hexdump.cc to not use C++17 features (std::byte and Class Template Argument Deduction). Also remove forcing of -std=libc for xtensa targets, which was added because stdlib isn't supported with -std=c++17. BUG=410831256
1 parent c3cded7 commit 15b55b7

File tree

6 files changed

+29
-16
lines changed

6 files changed

+29
-16
lines changed

tensorflow/lite/micro/compression/metadata_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ metadata->subgraphs.push_back(std::move(subgraph0));
8181
flatbuffers::FlatBufferBuilder builder;
8282
auto root = Metadata::Pack(builder, metadata.get());
8383
builder.Finish(root);
84-
auto flatbuffer = tflite::Span<const std::byte>{
85-
reinterpret_cast<const std::byte*>(builder.GetBufferPointer()),
84+
auto flatbuffer = tflite::Span<const uint8_t>{
85+
reinterpret_cast<const uint8_t*>(builder.GetBufferPointer()),
8686
builder.GetSize()};
8787

8888
TF_LITE_MICRO_TEST(ReadbackEqualsWrite) {

tensorflow/lite/micro/hexdump.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
#include <algorithm>
1818
#include <cctype>
1919
#include <cstdarg>
20+
#include <cstdint>
2021

2122
#include "tensorflow/lite/micro/micro_log.h"
22-
#include "tensorflow/lite/micro/static_vector.h"
2323

2424
namespace {
2525

@@ -53,15 +53,16 @@ tflite::Span<char> output(const tflite::Span<char>& buf, const char* format,
5353

5454
} // end anonymous namespace
5555

56-
tflite::Span<char> tflite::hexdump(const tflite::Span<const std::byte> region,
56+
tflite::Span<char> tflite::hexdump(const tflite::Span<const uint8_t> region,
5757
const tflite::Span<char> out) {
5858
tflite::Span<char> buffer{out};
5959
std::size_t byte_nr = 0;
6060
constexpr int per_line = 16;
6161
const int lines = (region.size() + per_line - 1) / per_line; // round up
6262

6363
for (int line = 0; line < lines; ++line) {
64-
tflite::StaticVector<char, per_line> ascii;
64+
char ascii[per_line];
65+
int ascii_nr = 0;
6566

6667
// print address
6768
buffer = output(buffer, "%08X:", line);
@@ -77,7 +78,7 @@ tflite::Span<char> tflite::hexdump(const tflite::Span<const std::byte> region,
7778
if (std::isprint(as_int)) {
7879
c = static_cast<char>(as_int);
7980
}
80-
ascii.push_back(c);
81+
ascii[ascii_nr++] = c;
8182
} else {
8283
buffer = output(buffer, " ");
8384
}
@@ -90,15 +91,15 @@ tflite::Span<char> tflite::hexdump(const tflite::Span<const std::byte> region,
9091

9192
// print the ascii value
9293
buffer = output(buffer, " ");
93-
for (const auto& c : ascii) {
94-
buffer = output(buffer, "%c", c);
94+
for (int ascii_index = 0; ascii_index < ascii_nr; ascii_index++) {
95+
buffer = output(buffer, "%c", ascii[ascii_index]);
9596
}
9697
buffer = output(buffer, "%c", '\n');
9798
}
9899

99100
return {out.data(), out.size() - buffer.size()};
100101
}
101102

102-
void tflite::hexdump(const tflite::Span<const std::byte> region) {
103+
void tflite::hexdump(const tflite::Span<const uint8_t> region) {
103104
hexdump(region, {nullptr, 0});
104105
}

tensorflow/lite/micro/hexdump.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,20 @@
1616
#define TENSORFLOW_LITE_MICRO_HEXDUMP_H_
1717

1818
#include <cstddef>
19+
#include <cstdint>
1920

2021
#include "tensorflow/lite/micro/span.h"
2122

2223
namespace tflite {
2324

2425
// Displays the contents of a memory region, formatted in hexadecimal and ASCII
2526
// in a style matching Python's hexdump module, using DebugLog().
26-
void hexdump(Span<const std::byte> region);
27+
void hexdump(Span<const uint8_t> region);
2728

2829
// Writes the contents of a memory region, formatted in hexadecimal and ASCII
2930
// in a style matching Python's hexdump module, to a buffer. Returns the portion
3031
// of the buffer written.
31-
Span<char> hexdump(Span<const std::byte> region, Span<char> buffer);
32+
Span<char> hexdump(Span<const uint8_t> region, Span<char> buffer);
3233

3334
} // end namespace tflite
3435

tensorflow/lite/micro/hexdump_test.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
#include "tensorflow/lite/micro/hexdump.h"
1616

1717
#include <array>
18+
#include <cstdint>
1819

1920
#include "tensorflow/lite/micro/span.h"
2021
#include "tensorflow/lite/micro/testing/micro_test.h"
2122

2223
constexpr tflite::Span<const char> input{
2324
"This is an input string for testing."};
2425

25-
const tflite::Span<const std::byte> region{
26-
reinterpret_cast<const std::byte*>(input.data()), input.size()};
26+
const tflite::Span<const uint8_t> region{
27+
reinterpret_cast<const uint8_t*>(input.data()), input.size()};
2728

2829
// clang-format off
2930
constexpr tflite::Span<const char> expected{

tensorflow/lite/micro/tools/make/Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ ifeq ($(TARGET), $(HOST_OS))
183183
endif
184184

185185
CXXFLAGS := \
186-
-std=c++17 \
187186
-fno-rtti \
188187
-fno-exceptions \
189188
-fno-threadsafe-statics \
@@ -192,7 +191,6 @@ CXXFLAGS := \
192191

193192
CCFLAGS := \
194193
-Wimplicit-function-declaration \
195-
-std=c17 \
196194
$(COMMON_FLAGS)
197195

198196
ARFLAGS := -r
@@ -260,6 +258,14 @@ else ifeq ($(BUILD_TYPE), no_tf_lite_static_memory)
260258
CCFLAGS := $(filter-out -DTF_LITE_STATIC_MEMORY, $(CCFLAGS))
261259
endif
262260

261+
ifeq ($(CC_VER11), true)
262+
CXXFLAGS+=-std=c++11
263+
CCFLAGS+=-std=c11
264+
else
265+
CXXFLAGS+=-std=c++17
266+
CCFLAGS+=-std=c17
267+
endif
268+
263269
# This library is the main target for this makefile. It will contain a minimal
264270
# runtime that can be linked in to other programs.
265271
MICROLITE_LIB_NAME := libtensorflow-microlite.a

tensorflow/lite/micro/tools/make/targets/xtensa_makefile.inc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ endif
3535
TARGET_ARCH_DEFINES := -D$(shell echo $(TARGET_ARCH) | tr [a-z] [A-Z])
3636

3737
PLATFORM_FLAGS = \
38-
-stdlib=libc++ \
3938
-DTF_LITE_MCU_DEBUG_LOG \
4039
-DTF_LITE_USE_CTIME \
4140
--xtensa-core=$(XTENSA_CORE) \
@@ -48,6 +47,11 @@ TARGET_TOOLCHAIN_PREFIX := xt-
4847
CXX_TOOL := clang++
4948
CC_TOOL := clang
5049

50+
# Building with C++17 requires libc++
51+
ifneq ($(CC_VER11), true)
52+
PLATFORM_FLAGS += -stdlib=libc++
53+
endif
54+
5155
# Unused exception related symbols make their way into a binary that links
5256
# against TFLM as described in https://github.com/tensorflow/tensorflow/issues/47575.
5357
# We have two options to avoid this. The first involves using -stdlib=libc++ and

0 commit comments

Comments
 (0)