Skip to content

Commit 9642c76

Browse files
committed
Fix -Werror diagnostics in vendored runtime and kernel sources
On toolchains where int32_t and uint32_t are long (the Xtensa and RISC-V ESP-IDF toolchains, and arm-none-eabi-gcc), log and check messages that pass them to %d or %u trip -Wformat. ESP-IDF builds with -Werror, so its component currently compiles the vendored ExecuTorch sources with a blanket -Wno-error; these fixes let the next change narrow that. The runtime, flat_tensor and portable kernel sources now use PRId32/PRIu32, or cast to int where the rest of a message already does. ET_PRI_SIZES_AND_STRIDES, the format for elements of sizes() and strides(), was "d" in lean mode, which is wrong for int32_t on these toolchains, so its users such as extension/tensor's tensor_ptr.cpp failed -Werror=format there. It is now PRId32, which is "d" wherever int32_t is int. The size and stride messages in op_fft_r2c and in the ET_CHECK_CONTIGUOUS and ET_CHECK_SAME_STRIDES macros of tensor_util.h use it, so the STRIDES macros no longer truncate ATen-mode strides to int32_t, and those macros loop over and print dimensions as size_t, which kernels outside this tree also get. Other diagnostics that ESP-IDF turns into errors are fixed in the same sources: an always-true `index >= 0` on a uint32_t in flat_tensor, a cast to a const-qualified type in native_dropout, prod's `*=` on bool outputs (-Wint-in-bool-context), missing parentheses in ET_CHECK_SAME_SHAPE3, and always_inline on non-inline templates in the Cadence generic im2row operators, which now use ET_INLINE. prod's bool product, in both prod_out and prod_int_out, is now `&&` through a template helper, because `if constexpr` only discards a branch inside a template; results are unchanged for every dtype. Verified by compiling every vendored runtime and kernel source (281) as the generated component builds them for esp32s3 and esp32c6, under ESP-IDF 6.1's -Wall -Werror, at both -Os and -Og; by compiling extension/tensor/tensor_ptr.cpp and the tensor_util.h check macros with both ESP-IDF toolchains under -Werror=format and on host in ATen mode; and by running the component's emulator tests. On host, portable_kernels_test and the exec_aten util tests pass. Authored with Claude Code.
1 parent 161acb5 commit 9642c76

14 files changed

Lines changed: 93 additions & 67 deletions

File tree

‎backends/cadence/generic/operators/op_im2row.cpp‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@
99
#include <executorch/backends/cadence/generic/operators/op_im2row.h>
1010

1111
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
12-
13-
#ifndef DISABLE_ALWAYS_INLINE
14-
#define ALWAYS_INLINE __attribute__((always_inline))
15-
#else
16-
#define ALWAYS_INLINE inline
17-
#endif
12+
#include <executorch/runtime/platform/compiler.h>
1813

1914
namespace impl {
2015
namespace generic {
@@ -26,7 +21,7 @@ using ::executorch::aten::Tensor;
2621
using ::executorch::runtime::KernelRuntimeContext;
2722

2823
template <typename T>
29-
ALWAYS_INLINE void im2row_(
24+
ET_INLINE void im2row_(
3025
const T* __restrict__ data_im,
3126
const int32_t in_zero_point,
3227
/* input parameters*/

‎backends/cadence/generic/operators/op_transposed_im2row.cpp‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@
1212

1313
#include <executorch/backends/cadence/generic/kernels/kernels.h>
1414
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
15-
16-
#ifndef DISABLE_ALWAYS_INLINE
17-
#define ALWAYS_INLINE __attribute__((always_inline))
18-
#else
19-
#define ALWAYS_INLINE inline
20-
#endif
15+
#include <executorch/runtime/platform/compiler.h>
2116

2217
namespace impl {
2318
namespace generic {
@@ -29,7 +24,7 @@ using ::executorch::aten::Tensor;
2924
using ::executorch::runtime::KernelRuntimeContext;
3025

3126
template <typename T>
32-
ALWAYS_INLINE void transposed_im2row_(
27+
ET_INLINE void transposed_im2row_(
3328
const T* __restrict__ data_im,
3429
const int32_t in_zero_point,
3530
/* input parameters*/

‎extension/flat_tensor/flat_tensor_data_map.cpp‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ Result<const flat_tensor_flatbuffer::NamedData*> get_named_data(
7474
ET_CHECK_OR_RETURN_ERROR(
7575
segment_index < segments->size(),
7676
InvalidExternalData,
77-
"Segment index %zu for key %.*s is out of bounds for segment size %d. Malformed PTD file.",
77+
"Segment index %zu for key %.*s is out of bounds for segment size %" PRIu32
78+
". Malformed PTD file.",
7879
static_cast<size_t>(segment_index),
7980
static_cast<int>(key.size()),
8081
key.data(),
@@ -255,9 +256,9 @@ ET_NODISCARD Result<const char*> FlatTensorDataMap::get_key(
255256
uint32_t index) const {
256257
uint32_t num_keys = get_num_keys().get();
257258
ET_CHECK_OR_RETURN_ERROR(
258-
index >= 0 && index < num_keys,
259+
index < num_keys,
259260
InvalidArgument,
260-
"Index %u out of range of size %u",
261+
"Index %" PRIu32 " out of range of size %" PRIu32,
261262
index,
262263
num_keys);
263264
return flat_tensor_->named_data()->Get(index)->key()->c_str();
@@ -280,7 +281,8 @@ ET_NODISCARD Result<const char*> FlatTensorDataMap::get_key(
280281
ET_CHECK_OR_RETURN_ERROR(
281282
fh.ok(),
282283
InvalidExternalData,
283-
"Failed to parse FlatTensor header with error code %u. File may be corrupt.",
284+
"Failed to parse FlatTensor header with error code %" PRIu32
285+
". File may be corrupt.",
284286
static_cast<uint32_t>(fh.error()));
285287

286288
Result<uint64_t> expected_size = get_segment_end_offset(fh.get());
@@ -366,7 +368,7 @@ ET_NODISCARD Result<const char*> FlatTensorDataMap::get_key(
366368
root_offset <=
367369
flat_tensor_data->size() - sizeof(flatbuffers::soffset_t),
368370
InvalidExternalData,
369-
"FlatTensor root table offset %u is invalid for data size %zu",
371+
"FlatTensor root table offset %" PRIu32 " is invalid for data size %zu",
370372
root_offset,
371373
flat_tensor_data->size());
372374

@@ -383,9 +385,9 @@ ET_NODISCARD Result<const char*> FlatTensorDataMap::get_key(
383385
ET_CHECK_OR_RETURN_ERROR(
384386
flat_tensor->version() <= kMaxSupportedSchemaVersion,
385387
InvalidExternalData,
386-
"FlatTensor schema version %u is newer than the highest this runtime "
387-
"supports (%u). Export the data with an older ExecuTorch, or update the "
388-
"runtime.",
388+
"FlatTensor schema version %" PRIu32
389+
" is newer than the highest this runtime supports (%" PRIu32
390+
"). Export the data with an older ExecuTorch, or update the runtime.",
389391
flat_tensor->version(),
390392
kMaxSupportedSchemaVersion);
391393

‎kernels/portable/cpu/op_fft_r2c.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ Tensor& _fft_r2c_out(
252252
out_sizes.data(), out_sizes.size())) == Error::Ok,
253253
InvalidArgument,
254254
out,
255-
"Failed to resize output tensor (last dim %d).",
255+
"Failed to resize output tensor (last dim %" ET_PRI_SIZES_AND_STRIDES
256+
").",
256257
out_sizes[dim.back()]);
257258

258259
// NOTE: as of this writing, upstream PyTorch only supports float/double, so

‎kernels/portable/cpu/op_native_dropout.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ std::tuple<Tensor&, Tensor&> native_dropout_out(
6363
utils::SupportedTensorDtypes::SAME_AS_COMMON>(
6464
[](const CTYPE_COMPUTE val, const CTYPE_COMPUTE mask_val) {
6565
if (!mask_val) {
66-
return static_cast<decltype(val)>(0);
66+
return static_cast<CTYPE_COMPUTE>(0);
6767
}
6868
return val;
6969
},

‎kernels/portable/cpu/op_prod.cpp‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,29 @@
1010
#include <executorch/kernels/portable/cpu/util/reduce_util.h>
1111
#include <executorch/runtime/kernel/kernel_includes.h>
1212

13+
#include <type_traits>
14+
1315
namespace torch {
1416
namespace executor {
1517
namespace native {
1618

1719
using Tensor = executorch::aten::Tensor;
1820
using ScalarType = executorch::aten::ScalarType;
1921

22+
namespace {
23+
24+
// `*` on bool trips -Wint-in-bool-context; for bool the product is `&&`.
25+
template <typename T>
26+
T multiply(T a, T b) {
27+
if constexpr (std::is_same_v<T, bool>) {
28+
return a && b;
29+
} else {
30+
return a * b;
31+
}
32+
}
33+
34+
} // namespace
35+
2036
Tensor& prod_out(
2137
KernelRuntimeContext& ctx,
2238
const Tensor& in,
@@ -40,7 +56,7 @@ Tensor& prod_out(
4056
auto data_out = out.mutable_data_ptr<CTYPE_OUT>();
4157
data_out[0] = static_cast<CTYPE_OUT>(1);
4258
for (const auto i : c10::irange(in.numel())) {
43-
data_out[0] *= static_cast<CTYPE_OUT>(data_in[i]);
59+
data_out[0] = multiply(data_out[0], static_cast<CTYPE_OUT>(data_in[i]));
4460
}
4561
});
4662
});
@@ -86,7 +102,8 @@ Tensor& prod_int_out(
86102
map_reduce_over_dim<CTYPE_IN, CTYPE_OUT>(
87103
[](CTYPE_IN v) { return static_cast<CTYPE_OUT>(v); },
88104
[](CTYPE_OUT outv, long, CTYPE_OUT acc, long) {
89-
return std::tuple<CTYPE_OUT, long>{acc * outv, 0};
105+
return std::tuple<CTYPE_OUT, long>{
106+
multiply(acc, outv), 0};
90107
},
91108
in,
92109
dim,

‎kernels/portable/cpu/op_repeat_interleave.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ bool check_repeat_interleave_args(
5050
for (const auto i : c10::irange(repeats.numel())) {
5151
ET_CHECK_OR_RETURN_FALSE(
5252
repeats_data[i] >= 0,
53-
"repeats cannot be negative; repeats_data[%" PRId64 "] = %d",
53+
"repeats cannot be negative; repeats_data[%" PRId64 "] = %" PRId32,
5454
static_cast<int64_t>(i),
5555
repeats_data[i]);
5656
}

‎runtime/core/exec_aten/util/tensor_util.h‎

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
ET_CHECK_MSG( \
104104
a_numel__ == b_numel__ && b_numel__ == c_numel__ && \
105105
((a_numel__ == 1 && b_numel__ == 1 && c_numel__ == 1) || \
106-
a_dim__ == b_dim__ && b_dim__ == c_dim__), \
106+
(a_dim__ == b_dim__ && b_dim__ == c_dim__)), \
107107
ET_TENSOR_CHECK_PREFIX__ \
108108
": numel={%zu, %zu, %zu}, dim={%zu, %zu, %zu}", \
109109
a_numel__, \
@@ -248,13 +248,14 @@
248248
ET_CHECK_MSG( \
249249
strides[strides.size() - 1] == 1, \
250250
"The stride of the last dimension shall be 1 for contiguous tensor, " \
251-
"not %d", \
251+
"not %" ET_PRI_SIZES_AND_STRIDES, \
252252
strides[strides.size() - 1]); \
253253
for (size_t i = strides.size() - 1; i > 0; i--) { \
254254
ET_CHECK_MSG( \
255255
strides[i - 1] == strides[i] * sizes[i], \
256256
"The stride of the %zu-th dimension shall equal to " \
257-
"strides[%zu] * sizes[%zu], now is %d and %d", \
257+
"strides[%zu] * sizes[%zu], now is %" ET_PRI_SIZES_AND_STRIDES \
258+
" and %" ET_PRI_SIZES_AND_STRIDES, \
258259
i - 1, \
259260
i, \
260261
i, \
@@ -270,24 +271,27 @@
270271
*/
271272
#define ET_CHECK_SAME_STRIDES2(a__, b__) \
272273
do { \
274+
const size_t a_dim__ = a__.dim(); \
275+
const size_t b_dim__ = b__.dim(); \
273276
ET_CHECK_MSG( \
274-
a__.dim() == b__.dim(), \
277+
a_dim__ == b_dim__, \
275278
"Two tensors shall have same number of strides, but not %zu and %zu.", \
276-
a__.dim(), \
277-
b__.dim()); \
279+
a_dim__, \
280+
b_dim__); \
278281
const ::executorch::aten::ArrayRef<executorch::aten::StridesType> \
279282
a_strides = a__.strides(); \
280283
const ::executorch::aten::ArrayRef<executorch::aten::StridesType> \
281284
b_strides = b__.strides(); \
282-
for (const auto i : c10::irange(a__.dim())) { \
285+
for (size_t i = 0; i < a_dim__; i++) { \
283286
ET_CHECK_MSG( \
284287
a_strides[i] == b_strides[i], \
285288
"a.strides()[%zu] shall equal to b.strides()[%zu], " \
286-
"but now is %d and %d.", \
289+
"but now is %" ET_PRI_SIZES_AND_STRIDES \
290+
" and %" ET_PRI_SIZES_AND_STRIDES ".", \
287291
i, \
288292
i, \
289-
(int32_t)a_strides[i], \
290-
(int32_t)b_strides[i]); \
293+
a_strides[i], \
294+
b_strides[i]); \
291295
} \
292296
} while (0)
293297

@@ -298,30 +302,35 @@
298302
*/
299303
#define ET_CHECK_SAME_STRIDES3(a__, b__, c__) \
300304
do { \
305+
const size_t a_dim__ = a__.dim(); \
306+
const size_t b_dim__ = b__.dim(); \
307+
const size_t c_dim__ = c__.dim(); \
301308
ET_CHECK_MSG( \
302-
a__.dim() == b__.dim() && b__.dim() == c__.dim(), \
309+
a_dim__ == b_dim__ && b_dim__ == c_dim__, \
303310
"Three tensors shall have same number of strides, " \
304311
"but not %zu, %zu and %zu.", \
305-
a__.dim(), \
306-
b__.dim(), \
307-
c__.dim()); \
312+
a_dim__, \
313+
b_dim__, \
314+
c_dim__); \
308315
const ::executorch::aten::ArrayRef<executorch::aten::StridesType> \
309316
a_strides = a__.strides(); \
310317
const ::executorch::aten::ArrayRef<executorch::aten::StridesType> \
311318
b_strides = b__.strides(); \
312319
const ::executorch::aten::ArrayRef<executorch::aten::StridesType> \
313320
c_strides = c__.strides(); \
314-
for (const auto i : c10::irange(a__.dim())) { \
321+
for (size_t i = 0; i < a_dim__; i++) { \
315322
ET_CHECK_MSG( \
316323
a_strides[i] == b_strides[i] && b_strides[i] == c_strides[i], \
317324
"a_strides[%zu], b_strides[%zu] and c_strides[%zu] " \
318-
"shall share same value, but now is %d, %d and %d", \
325+
"shall share same value, but now is " \
326+
"%" ET_PRI_SIZES_AND_STRIDES ", %" ET_PRI_SIZES_AND_STRIDES \
327+
" and %" ET_PRI_SIZES_AND_STRIDES, \
319328
i, \
320329
i, \
321330
i, \
322-
(int32_t)a_strides[i], \
323-
(int32_t)b_strides[i], \
324-
(int32_t)c_strides[i]); \
331+
a_strides[i], \
332+
b_strides[i], \
333+
c_strides[i]); \
325334
} \
326335
} while (0)
327336

‎runtime/core/portable_type/tensor_impl.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <executorch/runtime/core/tensor_shape_dynamism.h>
1717
#include <executorch/runtime/platform/compiler.h>
1818

19+
#include <cinttypes>
20+
1921
// Forward declaration of a helper that provides access to internal resizing
2022
// methods of TensorImpl. Real definition is in
2123
// executorch/runtime/core/exec_aten/tensor_util.h.
@@ -334,7 +336,7 @@ ::executorch::runtime::Result<ssize_t> safe_numel(
334336
// Appropriate format specifier for elements of sizes() and
335337
// strides(). Must be used instead of using d directly to support ATen
336338
// mode.
337-
#define ET_PRI_SIZES_AND_STRIDES "d"
339+
#define ET_PRI_SIZES_AND_STRIDES PRId32
338340

339341
} // namespace etensor
340342
} // namespace runtime

‎runtime/executor/program.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ Result<executorch_flatbuffer::ExecutionPlan*> get_execution_plan(
218218
root_offset <=
219219
program_data->size() - sizeof(flatbuffers::soffset_t),
220220
InvalidProgram,
221-
"Root table offset %u is invalid for program size %zu",
221+
"Root table offset %" PRIu32 " is invalid for program size %zu",
222222
root_offset,
223223
program_data->size());
224224
}
@@ -235,8 +235,9 @@ Result<executorch_flatbuffer::ExecutionPlan*> get_execution_plan(
235235
ET_CHECK_OR_RETURN_ERROR(
236236
flatbuffer_program->version() <= kMaxSupportedSchemaVersion,
237237
InvalidProgram,
238-
"Program schema version %u is newer than the highest this runtime "
239-
"supports (%u). Export the model with an older ExecuTorch, or update "
238+
"Program schema version %" PRIu32
239+
" is newer than the highest this runtime supports (%" PRIu32
240+
"). Export the model with an older ExecuTorch, or update "
240241
"the runtime.",
241242
flatbuffer_program->version(),
242243
kMaxSupportedSchemaVersion);

0 commit comments

Comments
 (0)