Skip to content

Commit e262d6d

Browse files
committed
xnnpack: work around Zephyr clamp macro collision
Zephyr's <zephyr/sys/util.h> unconditionally defines a function-like macro named "clamp" in the global namespace (in C mode) since its Sept 2025 rename of Z_CLAMP -> clamp. XNNPACK uses "clamp" as an identifier for a file-local static helper in src/operators/fully-connected-nc.c: static float clamp(const float value, const float minimum, const float maximum) { ... } The Zephyr macro expands over this function definition and breaks the XNNPACK build. Add a small prelude header (zephyr/include/xnnpack_zephyr_prelude.h) that force-includes util.h and immediately #undef's clamp. The util.h include guard then prevents any later transitive include (via pthread.h etc.) from re-defining the macro. The prelude is wired via -include on the xnnpack-base INTERFACE target, propagating to all XNNPACK component targets. Only clamp is undef'd; min/max have C++ guards upstream and do not collide with XNNPACK's current C code. Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
1 parent 0b50b01 commit e262d6d

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

zephyr/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,15 @@ if(CONFIG_EXECUTORCH)
188188
add_dependencies(${_xnn_dep} zephyr_generated_headers)
189189
endif()
190190
endforeach()
191+
# Force-include a prelude that undefines Zephyr's global-namespace
192+
# "clamp" macro, which collides with XNNPACK's file-local clamp()
193+
# helper (see include/xnnpack_zephyr_prelude.h). Applied via the
194+
# xnnpack-base INTERFACE target which is linked by all XNNPACK
195+
# component targets.
196+
if(TARGET xnnpack-base)
197+
target_compile_options(xnnpack-base INTERFACE
198+
-include ${CMAKE_CURRENT_LIST_DIR}/include/xnnpack_zephyr_prelude.h)
199+
endif()
191200
# pthreadpool uses cpuinfo for core count detection when available,
192201
# avoiding the need for sysconf(_SC_NPROCESSORS_ONLN).
193202
if(TARGET pthreadpool)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2026 BayLibre SAS
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Prelude header force-included into every XNNPACK source file when
6+
* building for Zephyr.
7+
*
8+
* Zephyr's <zephyr/sys/util.h> defines a function-like macro named
9+
* "clamp" in the global namespace (since commit 37717b229f5, Sept 2025,
10+
* which renamed Z_CLAMP -> clamp). XNNPACK uses "clamp" as an identifier
11+
* for a file-local static helper function (src/operators/fully-
12+
* connected-nc.c:45), which causes the macro to expand over the
13+
* function definition and break the build.
14+
*
15+
* By including util.h here and immediately undef'ing clamp, the
16+
* include guard on util.h prevents any later transitive include from
17+
* re-defining the macro, and XNNPACK's clamp() function compiles
18+
* cleanly.
19+
*
20+
* Only clamp is undef'd; min/max are left alone since they have C++
21+
* guards upstream and currently do not collide with XNNPACK's C code.
22+
*/
23+
24+
#ifdef __ZEPHYR__
25+
#include <zephyr/sys/util.h>
26+
#undef clamp
27+
#endif

0 commit comments

Comments
 (0)