Skip to content

Commit c85be2c

Browse files
Ninja91meta-codesync[bot]
authored andcommitted
Remove extern "C" wrapping and fix format specifiers for ARM embedded builds (pytorch#18000)
Summary: Pull Request resolved: pytorch#18000 Remove redundant `extern "C"` blocks wrapping CMSIS-NN header includes and fix `-Wformat` errors in format specifiers. These changes are required for the CC pipeline's FVP benchmark runner to compile on ARM embedded targets (Cortex-M55/M85 with MVE). ## Context The `extern "C"` wrapping around CMSIS-NN headers causes ARM embedded builds targeting MVE-capable processors to fail. CMSIS-NN's `arm_nn_math_types.h` temporarily closes its inner `extern "C"` before including `arm_mve.h`, but the outer `extern "C"` from the op files remains active, forcing `arm_mve.h` into C linkage where C++ function overloading is illegal. Additionally, format specifiers (`%hhd` for `ScalarType`, `%d`/`%ld` for `int64_t`) cause `-Wformat` errors treated as build failures on ARM toolchains. Changes: 1. Removed `extern "C"` wrapping from all 13 op .cpp files and `cmsis_scratch_buffer_context.h` 2. Consolidated CMSIS-NN includes in `cortex_m_ops_common.h` — added `#include "arm_nnfunctions.h"` (without `extern "C"`) so op files get it transitively 3. Added `#include <cinttypes>` for `PRIi64` macro 4. Fixed `%hhd` → `%d` with `static_cast<int>` for `ScalarType` values 5. Fixed `%d`/`%ld` → `PRIi64` for `int64_t` values Differential Revision: D95739935
1 parent 122fdef commit c85be2c

15 files changed

Lines changed: 18 additions & 78 deletions

backends/cortex_m/ops/cmsis_scratch_buffer_context.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
*/
88
#pragma once
99

10-
#include "cortex_m_ops_common.h"
11-
extern "C" {
1210
#include "arm_nnfunctions.h"
13-
}
11+
#include "cortex_m_ops_common.h"
1412

1513
namespace cortex_m {
1614
namespace native {

backends/cortex_m/ops/cortex_m_ops_common.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
#include <executorch/kernels/portable/cpu/util/kernel_ops_util.h>
1717
#include <executorch/runtime/platform/assert.h>
1818

19+
#include <cinttypes>
1920
#include <limits>
2021
#include <optional>
2122

22-
extern "C" {
2323
#include "arm_nn_types.h"
24-
}
24+
#include "arm_nnfunctions.h"
2525

2626
using Tensor = torch::executor::Tensor;
2727
using ScalarType = executorch::aten::ScalarType;
@@ -47,19 +47,19 @@ inline void validate_cmsis_nn_tensor_requirements(
4747
// Basic dtype validation
4848
ET_CHECK_MSG(
4949
input1.scalar_type() == expected_dtype,
50-
"Input1 dtype must be %hhd, got %hhd",
51-
expected_dtype,
52-
input1.scalar_type());
50+
"Input1 dtype must be %d, got %d",
51+
static_cast<int>(expected_dtype),
52+
static_cast<int>(input1.scalar_type()));
5353
ET_CHECK_MSG(
5454
input2.scalar_type() == expected_dtype,
55-
"Input2 dtype must be %hhd, got %hhd",
56-
expected_dtype,
57-
input2.scalar_type());
55+
"Input2 dtype must be %d, got %d",
56+
static_cast<int>(expected_dtype),
57+
static_cast<int>(input2.scalar_type()));
5858
ET_CHECK_MSG(
5959
output.scalar_type() == expected_dtype,
60-
"Output dtype must be %hhd, got %hhd",
61-
expected_dtype,
62-
output.scalar_type());
60+
"Output dtype must be %d, got %d",
61+
static_cast<int>(expected_dtype),
62+
static_cast<int>(output.scalar_type()));
6363
if (require_same_sizes) {
6464
ET_CHECK_MSG(
6565
input1.sizes() == input2.sizes(),
@@ -78,16 +78,17 @@ inline void validate_single_quant_params(
7878
const int64_t multiplier,
7979
const int64_t shift,
8080
const char* param_name) {
81+
(void)zero_point;
8182
ET_CHECK_MSG(
8283
multiplier >= std::numeric_limits<int32_t>::min() &&
8384
multiplier <= std::numeric_limits<int32_t>::max(),
84-
"%s multiplier must be in int32 range [Value: %d]",
85+
"%s multiplier must be in int32 range [Value: %" PRIi64 "]",
8586
param_name,
8687
multiplier);
8788

8889
ET_CHECK_MSG(
8990
shift >= -31 && shift <= 31,
90-
"%s shift must be in range [-31, 31] [Value: %d]",
91+
"%s shift must be in range [-31, 31] [Value: %" PRIi64 "]",
9192
param_name,
9293
shift);
9394
}
@@ -172,7 +173,7 @@ inline bool check_int32_within_range(
172173
value > std::numeric_limits<int32_t>::max()) {
173174
ET_LOG(
174175
Error,
175-
"%s: %s value (%ld) exceeds int32_t range",
176+
"%s: %s value (%" PRIi64 ") exceeds int32_t range",
176177
op_name,
177178
value_name,
178179
value);
@@ -354,14 +355,14 @@ inline bool validate_per_channel_quant_params(
354355
if (multipliers[i] <= ARM_NN_Q31_MIN || multipliers[i] > ARM_NN_Q31_MAX) {
355356
ET_LOG(
356357
Error,
357-
"weight_multiplier[%d] out of CMSIS-NN range: %d",
358+
"weight_multiplier[%d] out of CMSIS-NN range: %" PRIi64,
358359
i,
359360
multipliers[i]);
360361
return false;
361362
}
362363
// Shift: {-31, 30} for arm_nn_requantize
363364
if (shifts[i] < -31 || shifts[i] > 30) {
364-
ET_LOG(Error, "weight_shift[%d] out of range: %d", i, shifts[i]);
365+
ET_LOG(Error, "weight_shift[%d] out of range: %" PRIi64, i, shifts[i]);
365366
return false;
366367
}
367368
}

backends/cortex_m/ops/op_maximum.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77

88
#include "cortex_m_ops_common.h"
99

10-
// Include CMSIS-NN headers with C linkage
11-
extern "C" {
12-
#include "arm_nnfunctions.h"
13-
}
14-
1510
namespace cortex_m {
1611
namespace native {
1712

backends/cortex_m/ops/op_minimum.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99

1010
#include "cortex_m_ops_common.h"
1111

12-
// Include CMSIS-NN headers with C linkage
13-
extern "C" {
14-
#include "arm_nnfunctions.h"
15-
}
16-
1712
namespace cortex_m {
1813
namespace native {
1914

backends/cortex_m/ops/op_pad.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88

99
#include "cortex_m_ops_common.h"
1010

11-
extern "C" {
12-
#include "arm_nnfunctions.h"
13-
}
14-
1511
namespace cortex_m {
1612
namespace native {
1713

backends/cortex_m/ops/op_quantized_add.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99

1010
#include "cortex_m_ops_common.h"
1111

12-
// Include CMSIS-NN headers with C linkage
13-
extern "C" {
14-
#include "arm_nnfunctions.h"
15-
}
16-
1712
namespace cortex_m {
1813
namespace native {
1914
using KernelRuntimeContext = torch::executor::KernelRuntimeContext;

backends/cortex_m/ops/op_quantized_avg_pool2d.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77

88
#include "cortex_m_ops_common.h"
99

10-
extern "C" {
11-
#include "arm_nnfunctions.h"
12-
}
13-
1410
namespace cortex_m {
1511
namespace native {
1612

backends/cortex_m/ops/op_quantized_conv2d.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77

88
#include "cortex_m_ops_common.h"
99

10-
extern "C" {
11-
#include "arm_nnfunctions.h"
12-
}
13-
1410
namespace cortex_m {
1511
namespace native {
1612

backends/cortex_m/ops/op_quantized_depthwise_conv2d.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77

88
#include "cortex_m_ops_common.h"
99

10-
extern "C" {
11-
#include "arm_nnfunctions.h"
12-
}
13-
1410
namespace cortex_m {
1511
namespace native {
1612

backends/cortex_m/ops/op_quantized_linear.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99

1010
#include "cortex_m_ops_common.h"
1111

12-
extern "C" {
13-
#include "arm_nnfunctions.h"
14-
}
15-
1612
namespace cortex_m {
1713
namespace native {
1814
using KernelRuntimeContext = torch::executor::KernelRuntimeContext;

0 commit comments

Comments
 (0)