Skip to content

Commit e58fdb6

Browse files
authored
Arm backend: Fix warnings from cppcheck in runtime (#11639)
The runtime is written in C++ and had a bunch of cstyle castings and mixed operator tokens. This commit addresses these warnings. Signed-off-by: [email protected]
1 parent bda98dc commit e58fdb6

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

backends/arm/runtime/EthosUBackend.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class EthosUBackend final : public ::executorch::runtime::BackendInterface {
115115
ArrayRef<CompileSpec> compile_specs) const override {
116116
ET_LOG(Info, "EthosUBackend::init %p", processed->data());
117117

118-
char* data = (char*)processed->data();
118+
const char* data = static_cast<const char*>(processed->data());
119119
size_t size = processed->size();
120120

121121
// Verify format of vela_bin
@@ -160,15 +160,17 @@ class EthosUBackend final : public ::executorch::runtime::BackendInterface {
160160
// we might do it).
161161
EthosUBackendExecuteCallbacks CollectArm_CPU_Cycles;
162162

163-
ExecutionHandle* execution_handle = (ExecutionHandle*)input_handle;
163+
ExecutionHandle* execution_handle =
164+
static_cast<ExecutionHandle*>(input_handle);
164165
VelaHandles handles;
165166

166167
// Command stream - we know at this point it's aligned
167168
EXECUTORCH_PROF_START(
168169
event_tracer,
169170
event_tracer_local_scope,
170171
"+EthosUBackend::execute()processed_data");
171-
char* data = (char*)execution_handle->processed->data();
172+
const char* data =
173+
static_cast<const char*>(execution_handle->processed->data());
172174
EXECUTORCH_PROF_END(event_tracer, event_tracer_local_scope);
173175

174176
ET_LOG(Debug, "EthosUBackend::execute %p", data);
@@ -256,7 +258,7 @@ class EthosUBackend final : public ::executorch::runtime::BackendInterface {
256258
handles.inputs->io[i].elem_size == 2;
257259

258260
// Select a compatible copy routine
259-
if (both_char and permuted_input_shape) {
261+
if (both_char && permuted_input_shape) {
260262
EXECUTORCH_PROF_SCOPE(
261263
event_tracer,
262264
"+EthosUBackend::execute()handles.input.permute_CHW_to_HWC()");
@@ -267,7 +269,7 @@ class EthosUBackend final : public ::executorch::runtime::BackendInterface {
267269
tensor_in.size(1),
268270
tensor_in.size(2),
269271
tensor_in.size(3));
270-
} else if (both_char or both_int or both_short) {
272+
} else if (both_char || both_int || both_short) {
271273
EXECUTORCH_PROF_SCOPE(
272274
event_tracer, "+EthosUBackend::execute()handles.input.memcpy()");
273275
// Sizes match and elt size matches so memcpy
@@ -322,7 +324,7 @@ class EthosUBackend final : public ::executorch::runtime::BackendInterface {
322324
event_tracer, event_tracer_local_scope, "+EthosUBackend::execute()NPU");
323325
result = ethosu_invoke_v3(
324326
driver.get(),
325-
(void*)handles.cmd_data,
327+
static_cast<const void*>(handles.cmd_data),
326328
handles.cmd_data_size,
327329
bases,
328330
bases_size,
@@ -357,13 +359,14 @@ class EthosUBackend final : public ::executorch::runtime::BackendInterface {
357359
bool permuted_output_shape;
358360
ET_CHECK_OK_OR_RETURN_ERROR(check_requires_permute(
359361
i, tensor_out, &handles.outputs->io[i], &permuted_output_shape));
360-
if (tensor_out.scalar_type() == ScalarType::Char and
362+
if (tensor_out.scalar_type() == ScalarType::Char &&
361363
permuted_output_shape) {
362364
EXECUTORCH_PROF_SCOPE(
363365
event_tracer,
364366
"+EthosUBackend::execute()handles.output.permute_HWC_to_CHW()");
365367

366-
char* output_address = (char*)output_addr;
368+
const char* output_address = static_cast<const char*>(output_addr);
369+
367370
permute_HWC_to_CHW(
368371
output_address,
369372
tensor_out.mutable_data_ptr<char>(),
@@ -375,10 +378,11 @@ class EthosUBackend final : public ::executorch::runtime::BackendInterface {
375378
event_tracer, "+EthosUBackend::execute()handles.output.move()");
376379
for (int j = 0; j < tensor_out.numel(); j++) {
377380
if (tensor_out.scalar_type() == ScalarType::Char) {
378-
char* output_address = (char*)output_addr;
381+
const char* output_address = static_cast<const char*>(output_addr);
379382
tensor_out.mutable_data_ptr<char>()[j] = output_address[j];
380383
} else {
381-
int* output_address = (int*)output_addr;
384+
const int* output_address =
385+
reinterpret_cast<const int*>(output_addr);
382386
tensor_out.mutable_data_ptr<int>()[j] = output_address[j];
383387
}
384388
}
@@ -435,7 +439,7 @@ class EthosUBackend final : public ::executorch::runtime::BackendInterface {
435439
return Error::Ok;
436440
}
437441

438-
void permute_CHW_to_HWC(char* input, char* output, int C, int H, int W)
442+
void permute_CHW_to_HWC(const char* input, char* output, int C, int H, int W)
439443
const {
440444
for (int i = 0; i != H * W; ++i) {
441445
for (int j = 0; j < C; ++j) {
@@ -444,7 +448,7 @@ class EthosUBackend final : public ::executorch::runtime::BackendInterface {
444448
}
445449
}
446450

447-
void permute_HWC_to_CHW(char* input, char* output, int C, int H, int W)
451+
void permute_HWC_to_CHW(const char* input, char* output, int C, int H, int W)
448452
const {
449453
for (int i = 0; i != H * W; ++i) {
450454
for (int j = 0; j < C; ++j) {

backends/arm/runtime/VelaBinStream.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ bool vela_bin_validate(const char* data, int size) {
3131
// Check 16 byte alignment
3232
bool valid = true;
3333
if ((uintptr_t)data != next_mul_16((uintptr_t)data)) {
34-
ET_LOG(Error, "Vela bin ptr not aligned to 16 bytes: %p", (void*)data);
34+
ET_LOG(Error, "Vela bin ptr not aligned to 16 bytes: %p", data);
3535
valid = false;
3636
}
3737
if ((uintptr_t)foot != next_mul_16((uintptr_t)foot)) {
38-
ET_LOG(Error, "End of vela bin not aligned to 16 bytes: %p", (void*)foot);
38+
ET_LOG(Error, "End of vela bin not aligned to 16 bytes: %p", foot);
3939
valid = false;
4040
}
4141
// Check header and footer blocks are the right format
@@ -55,12 +55,13 @@ bool vela_bin_read(const char* data, VelaHandles* handles, int size) {
5555
const char* ptr = data;
5656

5757
while (ptr - data < size) {
58-
VelaBinBlock* b = (VelaBinBlock*)ptr;
58+
VelaBinBlock* b = reinterpret_cast<VelaBinBlock*>(const_cast<char*>(ptr));
5959
ptr += sizeof(VelaBinBlock) + next_mul_16(b->size);
6060

6161
if (!strncmp(b->name, "vela_bin_stream", strlen("vela_bin_stream"))) {
6262
// expect vela_bin_stream first
63-
if ((char*)b != (char*)data)
63+
if (reinterpret_cast<char*>(b) !=
64+
reinterpret_cast<char*>(const_cast<char*>(data)))
6465
return false;
6566
} else if (!strncmp(b->name, "cmd_data", strlen("cmd_data"))) {
6667
// This driver magic header confirms a valid command stream in binary
@@ -76,9 +77,9 @@ bool vela_bin_read(const char* data, VelaHandles* handles, int size) {
7677
reinterpret_cast<const uint32_t*>(b->data);
7778
handles->scratch_data_size = *scratch_size_ptr;
7879
} else if (!strncmp(b->name, "inputs", strlen("inputs"))) {
79-
handles->inputs = (VelaIOs*)b->data;
80+
handles->inputs = reinterpret_cast<VelaIOs*>(b->data);
8081
} else if (!strncmp(b->name, "outputs", strlen("outputs"))) {
81-
handles->outputs = (VelaIOs*)b->data;
82+
handles->outputs = reinterpret_cast<VelaIOs*>(b->data);
8283
} else if (!strncmp(
8384
b->name, "vela_end_stream", strlen("vela_end_stream"))) {
8485
// expect vela_end_stream last

0 commit comments

Comments
 (0)