Skip to content

Commit e0e015c

Browse files
committed
Merge branch 'metascroy-patch-7' into mlx-delegate
2 parents 443f8b6 + 84e941d commit e0e015c

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

kernels/optimized/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,36 @@ if(NOT EXECUTORCH_ROOT)
2121
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
2222
endif()
2323

24+
# Apply pocketfft patch for ASAN-compatible aligned_alloc on POSIX systems. The
25+
# portable aligned_alloc in pocketfft stores metadata at ptr[-1], which
26+
# conflicts with ASAN's heap redzone and causes intermittent bus errors. This
27+
# patch uses posix_memalign instead, which is ASAN-compatible.
28+
set(POCKETFFT_DIR "${EXECUTORCH_ROOT}/third-party/pocketfft")
29+
set(POCKETFFT_PATCH
30+
"${CMAKE_CURRENT_SOURCE_DIR}/patches/pocketfft_aligned_alloc.patch"
31+
)
32+
if(EXISTS "${POCKETFFT_PATCH}" AND EXISTS "${POCKETFFT_DIR}")
33+
execute_process(
34+
COMMAND git apply --check ${POCKETFFT_PATCH}
35+
WORKING_DIRECTORY ${POCKETFFT_DIR}
36+
RESULT_VARIABLE PATCH_CHECK_RESULT
37+
ERROR_QUIET
38+
)
39+
if(PATCH_CHECK_RESULT EQUAL 0)
40+
message(STATUS "Applying pocketfft aligned_alloc patch...")
41+
execute_process(
42+
COMMAND git apply ${POCKETFFT_PATCH}
43+
WORKING_DIRECTORY ${POCKETFFT_DIR}
44+
RESULT_VARIABLE PATCH_RESULT
45+
)
46+
if(NOT PATCH_RESULT EQUAL 0)
47+
message(WARNING "Failed to apply pocketfft patch")
48+
endif()
49+
else()
50+
message(STATUS "pocketfft patch already applied or not applicable")
51+
endif()
52+
endif()
53+
2454
set(_common_compile_options
2555
$<$<CXX_COMPILER_ID:MSVC>:/wd4996>
2656
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated-declarations>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
diff --git a/pocketfft_hdronly.h b/pocketfft_hdronly.h
2+
index 57db7d7..e831ac0 100644
3+
--- a/pocketfft_hdronly.h
4+
+++ b/pocketfft_hdronly.h
5+
@@ -162,8 +162,20 @@ template<> struct VLEN<double> { static constexpr size_t val=2; };
6+
// std::aligned_alloc is a bit cursed ... it doesn't exist on MacOS < 10.15
7+
// and in musl, and other OSes seem to have even more peculiarities.
8+
// Let's unconditionally work around it for now.
9+
-# if 0
10+
-//#if (__cplusplus >= 201703L) && (!defined(__MINGW32__)) && (!defined(_MSC_VER)) && (__MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_15)
11+
+#if defined(__APPLE__) || defined(__unix__)
12+
+// Use posix_memalign on POSIX systems - it is ASAN-compatible.
13+
+// The portable aligned_alloc below stores metadata at ptr[-1], which conflicts
14+
+// with ASAN's heap redzone and causes intermittent bus errors.
15+
+inline void *aligned_alloc(size_t align, size_t size)
16+
+ {
17+
+ void *ptr = nullptr;
18+
+ if (posix_memalign(&ptr, align, size) != 0)
19+
+ throw std::bad_alloc();
20+
+ return ptr;
21+
+ }
22+
+inline void aligned_dealloc(void *ptr)
23+
+ { free(ptr); }
24+
+#elif (__cplusplus >= 201703L) && (!defined(__MINGW32__)) && (!defined(_MSC_VER))
25+
inline void *aligned_alloc(size_t align, size_t size)
26+
{
27+
// aligned_alloc() requires that the requested size is a multiple of "align"
28+
@@ -173,7 +185,7 @@ inline void *aligned_alloc(size_t align, size_t size)
29+
}
30+
inline void aligned_dealloc(void *ptr)
31+
{ free(ptr); }
32+
-#else // portable emulation
33+
+#else // portable emulation (NOT ASAN-compatible - stores metadata at ptr[-1])
34+
inline void *aligned_alloc(size_t align, size_t size)
35+
{
36+
align = std::max(align, alignof(max_align_t));

0 commit comments

Comments
 (0)