99#include < executorch/backends/xnnpack/runtime/XNNWeightsCache.h>
1010#include < executorch/runtime/core/error.h>
1111#include < executorch/runtime/core/memory_allocator.h>
12- #ifndef _WIN32
12+ /*
13+ * The file-backed weights cache relies on POSIX file APIs (open/flock/
14+ * ftruncate) plus mmap. Windows lacks them, and so do bare-metal targets such
15+ * as Zephyr, which have no filesystem to back the cache with in the first
16+ * place. Those builds use the heap path only.
17+ */
18+ #if defined(_WIN32) || defined(__ZEPHYR__)
19+ #define ET_XNN_NO_FILE_BACKED_CACHE 1
20+ #endif
21+
22+ #ifndef ET_XNN_NO_FILE_BACKED_CACHE
1323#include < fcntl.h>
1424#include < sys/file.h>
1525#include < sys/mman.h>
@@ -48,7 +58,7 @@ XNNWeightsCache::XNNWeightsCache() {
4858}
4959
5060XNNWeightsCache::~XNNWeightsCache () {
51- #ifndef _WIN32
61+ #ifndef ET_XNN_NO_FILE_BACKED_CACHE
5262 for (auto & region : mmap_regions_) {
5363 if (region.addr != nullptr && region.addr != MAP_FAILED ) {
5464 munmap (region.addr , region.size );
@@ -76,7 +86,7 @@ static T read_le(const uint8_t* src) {
7686 return value;
7787}
7888
79- #ifndef _WIN32
89+ #ifndef ET_XNN_NO_FILE_BACKED_CACHE
8090// Open the cache file and take an advisory exclusive lock. Returns the
8191// fd, or -1 if open/flock failed (logs the failure). The caller decides
8292// how to recover (typically: skip the mmap path for this init).
@@ -135,7 +145,7 @@ Error XNNWeightsCache::initialize_for_runtime(
135145 named_data_map_ = named_data_map;
136146 is_finalized_ = false ;
137147
138- #ifndef _WIN32
148+ #ifndef ET_XNN_NO_FILE_BACKED_CACHE
139149 if (packed_cache_path_.empty () || packed_file_fd_ >= 0 ) {
140150 return Error::Ok;
141151 }
@@ -212,7 +222,7 @@ Result<std::vector<std::string>> XNNWeightsCache::finalize_for_runtime() {
212222 }
213223 }
214224
215- #ifndef _WIN32
225+ #ifndef ET_XNN_NO_FILE_BACKED_CACHE
216226 // Synchronous flush for newly added regions. MS_SYNC blocks until the
217227 // dirty pages are written to disk and marked clean
218228 if (mmap_regions_.size () > mmap_regions_synced_) {
@@ -258,7 +268,7 @@ Result<const uint8_t*> XNNWeightsCache::load_unpacked_data(
258268
259269void XNNWeightsCache::release_entry (void * packed_data_ptr) {
260270 packed_pointer_to_container_.erase (packed_data_ptr);
261- #ifndef _WIN32
271+ #ifndef ET_XNN_NO_FILE_BACKED_CACHE
262272 // Per-entry file-backed mmap region: munmap to release VM. The
263273 // packed_data_ptrs_ slot is nulled by the caller so existing offsets
264274 // stay valid.
@@ -276,7 +286,7 @@ void XNNWeightsCache::release_entry(void* packed_data_ptr) {
276286}
277287
278288void XNNWeightsCache::full_unload () {
279- #ifndef _WIN32
289+ #ifndef ET_XNN_NO_FILE_BACKED_CACHE
280290 for (auto & region : mmap_regions_) {
281291 if (region.addr != nullptr && region.addr != MAP_FAILED ) {
282292 munmap (region.addr , region.size );
@@ -376,7 +386,7 @@ size_t XNNWeightsCache::look_up(
376386}
377387
378388void * XNNWeightsCache::reserve_space (XNNWeightsCache* context, size_t n) {
379- #ifndef _WIN32
389+ #ifndef ET_XNN_NO_FILE_BACKED_CACHE
380390 if (context->last_lookup_unnamed_ || context->loaded_from_disk_ ) {
381391 return context->reserve_space_heap (n);
382392 }
@@ -538,7 +548,7 @@ void XNNWeightsCache::set_packed_cache_path(const std::string& path) {
538548}
539549
540550Error XNNWeightsCache::save_packed_index () {
541- #ifndef _WIN32
551+ #ifndef ET_XNN_NO_FILE_BACKED_CACHE
542552 if (packed_file_fd_ < 0 ) {
543553 return Error::Ok;
544554 }
@@ -638,7 +648,7 @@ Error XNNWeightsCache::save_packed_index() {
638648}
639649
640650bool XNNWeightsCache::load_packed_cache () {
641- #ifndef _WIN32
651+ #ifndef ET_XNN_NO_FILE_BACKED_CACHE
642652 int fd = open (packed_cache_path_.c_str (), O_RDONLY );
643653 if (fd < 0 ) {
644654 return false ;
0 commit comments