Skip to content

Commit df8386b

Browse files
authored
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 or int64_t where the rest of a message already does (Tensor::SizesType and StridesType differ between lean and ATen mode). The ET_CHECK_CONTIGUOUS and ET_CHECK_SAME_STRIDES macros in tensor_util.h get the same fix, since kernels outside this tree expand them. The size_t values in the messages touched here print with ET_PRIsize_t instead of %zu, which the Cadence Xtensa toolchain's C library does not support. 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. The bool product 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 (275) with the ESP-IDF 6.1 flags for esp32s3 and esp32c6 at -Og and -Os, under -Werror with format and type-limits errors enabled: all clean. On host, portable_kernels_test (1891 tests) and the exec_aten util tests pass. Authored with Claude Code.
1 parent eed2436 commit df8386b

13 files changed

Lines changed: 77 additions & 46 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
1212

1313
#ifndef DISABLE_ALWAYS_INLINE
14-
#define ALWAYS_INLINE __attribute__((always_inline))
14+
#define ALWAYS_INLINE inline __attribute__((always_inline))
1515
#else
1616
#define ALWAYS_INLINE inline
1717
#endif

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
1515

1616
#ifndef DISABLE_ALWAYS_INLINE
17-
#define ALWAYS_INLINE __attribute__((always_inline))
17+
#define ALWAYS_INLINE inline __attribute__((always_inline))
1818
#else
1919
#define ALWAYS_INLINE inline
2020
#endif

‎extension/flat_tensor/flat_tensor_data_map.cpp‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ 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 %" ET_PRIsize_t
78+
" for key %.*s is out of bounds for segment size %" PRIu32
79+
". Malformed PTD file.",
7880
static_cast<size_t>(segment_index),
7981
static_cast<int>(key.size()),
8082
key.data(),
@@ -255,9 +257,9 @@ ET_NODISCARD Result<const char*> FlatTensorDataMap::get_key(
255257
uint32_t index) const {
256258
uint32_t num_keys = get_num_keys().get();
257259
ET_CHECK_OR_RETURN_ERROR(
258-
index >= 0 && index < num_keys,
260+
index < num_keys,
259261
InvalidArgument,
260-
"Index %u out of range of size %u",
262+
"Index %" PRIu32 " out of range of size %" PRIu32,
261263
index,
262264
num_keys);
263265
return flat_tensor_->named_data()->Get(index)->key()->c_str();
@@ -280,7 +282,8 @@ ET_NODISCARD Result<const char*> FlatTensorDataMap::get_key(
280282
ET_CHECK_OR_RETURN_ERROR(
281283
fh.ok(),
282284
InvalidExternalData,
283-
"Failed to parse FlatTensor header with error code %u. File may be corrupt.",
285+
"Failed to parse FlatTensor header with error code %" PRIu32
286+
". File may be corrupt.",
284287
static_cast<uint32_t>(fh.error()));
285288

286289
Result<uint64_t> expected_size = get_segment_end_offset(fh.get());
@@ -366,7 +369,8 @@ ET_NODISCARD Result<const char*> FlatTensorDataMap::get_key(
366369
root_offset <=
367370
flat_tensor_data->size() - sizeof(flatbuffers::soffset_t),
368371
InvalidExternalData,
369-
"FlatTensor root table offset %u is invalid for data size %zu",
372+
"FlatTensor root table offset %" PRIu32
373+
" is invalid for data size %" ET_PRIsize_t,
370374
root_offset,
371375
flat_tensor_data->size());
372376

@@ -383,9 +387,9 @@ ET_NODISCARD Result<const char*> FlatTensorDataMap::get_key(
383387
ET_CHECK_OR_RETURN_ERROR(
384388
flat_tensor->version() <= kMaxSupportedSchemaVersion,
385389
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.",
390+
"FlatTensor schema version %" PRIu32
391+
" is newer than the highest this runtime supports (%" PRIu32
392+
"). Export the data with an older ExecuTorch, or update the runtime.",
389393
flat_tensor->version(),
390394
kMaxSupportedSchemaVersion);
391395

‎kernels/portable/cpu/op_fft_r2c.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +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).",
256-
out_sizes[dim.back()]);
255+
"Failed to resize output tensor (last dim %" PRId64 ").",
256+
static_cast<int64_t>(out_sizes[dim.back()]));
257257

258258
// NOTE: as of this writing, upstream PyTorch only supports float/double, so
259259
// we follow suit.

‎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: 17 additions & 1 deletion
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
});

‎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: 15 additions & 11 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,18 +248,19 @@
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", \
252-
strides[strides.size() - 1]); \
251+
"not %" PRId64, \
252+
static_cast<int64_t>(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], \
256-
"The stride of the %zu-th dimension shall equal to " \
257-
"strides[%zu] * sizes[%zu], now is %d and %d", \
256+
"The stride of the %" ET_PRIsize_t \
257+
"-th dimension shall equal to strides[%" ET_PRIsize_t \
258+
"] * sizes[%" ET_PRIsize_t "], now is %" PRId64 " and %" PRId64, \
258259
i - 1, \
259260
i, \
260261
i, \
261-
strides[i - 1], \
262-
strides[i] * sizes[i]); \
262+
static_cast<int64_t>(strides[i - 1]), \
263+
static_cast<int64_t>(strides[i] * sizes[i])); \
263264
} \
264265
} while (0)
265266

@@ -282,8 +283,9 @@
282283
for (const auto i : c10::irange(a__.dim())) { \
283284
ET_CHECK_MSG( \
284285
a_strides[i] == b_strides[i], \
285-
"a.strides()[%zu] shall equal to b.strides()[%zu], " \
286-
"but now is %d and %d.", \
286+
"a.strides()[%" ET_PRIsize_t \
287+
"] shall equal to b.strides()[%" ET_PRIsize_t \
288+
"], but now is %" PRId32 " and %" PRId32 ".", \
287289
i, \
288290
i, \
289291
(int32_t)a_strides[i], \
@@ -314,8 +316,10 @@
314316
for (const auto i : c10::irange(a__.dim())) { \
315317
ET_CHECK_MSG( \
316318
a_strides[i] == b_strides[i] && b_strides[i] == c_strides[i], \
317-
"a_strides[%zu], b_strides[%zu] and c_strides[%zu] " \
318-
"shall share same value, but now is %d, %d and %d", \
319+
"a_strides[%" ET_PRIsize_t "], b_strides[%" ET_PRIsize_t \
320+
"] and c_strides[%" ET_PRIsize_t \
321+
"] shall share same value, " \
322+
"but now is %" PRId32 ", %" PRId32 " and %" PRId32, \
319323
i, \
320324
i, \
321325
i, \

‎runtime/executor/program.cpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ 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
222+
" is invalid for program size %" ET_PRIsize_t,
222223
root_offset,
223224
program_data->size());
224225
}
@@ -235,8 +236,9 @@ Result<executorch_flatbuffer::ExecutionPlan*> get_execution_plan(
235236
ET_CHECK_OR_RETURN_ERROR(
236237
flatbuffer_program->version() <= kMaxSupportedSchemaVersion,
237238
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 "
239+
"Program schema version %" PRIu32
240+
" is newer than the highest this runtime supports (%" PRIu32
241+
"). Export the model with an older ExecuTorch, or update "
240242
"the runtime.",
241243
flatbuffer_program->version(),
242244
kMaxSupportedSchemaVersion);

‎runtime/executor/program_validation.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ validate_tensor(const executorch_flatbuffer::Tensor* tensor) {
4141
ET_LOG(
4242
Error,
4343
"Size must be non-negative, got %d at dimension %u",
44-
size,
44+
static_cast<int>(size),
4545
static_cast<unsigned>(i));
4646
return Error::InvalidProgram;
4747
}
@@ -199,7 +199,7 @@ validate_program(const executorch_flatbuffer::Program* program) {
199199
"TensorList item %u has out-of-bounds index %d (values size "
200200
"%u) in execution plan %u",
201201
static_cast<unsigned>(item_idx),
202-
evalue_index,
202+
static_cast<int>(evalue_index),
203203
static_cast<unsigned>(values->size()),
204204
static_cast<unsigned>(plan_idx));
205205
return Error::InvalidProgram;
@@ -213,7 +213,7 @@ validate_program(const executorch_flatbuffer::Program* program) {
213213
"TensorList item %u references null evalue at index %d in "
214214
"execution plan %u",
215215
static_cast<unsigned>(item_idx),
216-
evalue_index,
216+
static_cast<int>(evalue_index),
217217
static_cast<unsigned>(plan_idx));
218218
return Error::InvalidProgram;
219219
}
@@ -226,7 +226,7 @@ validate_program(const executorch_flatbuffer::Program* program) {
226226
"index %d in execution plan %u",
227227
static_cast<unsigned>(item_idx),
228228
static_cast<int>(referenced_value->val_type()),
229-
evalue_index,
229+
static_cast<int>(evalue_index),
230230
static_cast<unsigned>(plan_idx));
231231
return Error::InvalidProgram;
232232
}

0 commit comments

Comments
 (0)