1 // Copyright 2019 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5
6 #pragma once
7
8 #include <stddef.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #if defined(_MSC_VER)
13 #include <malloc.h>
14 #elif !defined(__GNUC__)
15 #include <alloca.h>
16 #endif
17
18 #include <xnnpack.h>
19 #include <xnnpack/common.h>
20 #include <xnnpack/params.h>
21
22
23 #if XNN_ARCH_WASM
24 #define XNN_ALLOCATION_ALIGNMENT 4
25 #elif XNN_ARCH_X86 || XNN_ARCH_X86_64
26 #if XNN_PLATFORM_MOBILE
27 #define XNN_ALLOCATION_ALIGNMENT 32
28 #else
29 #define XNN_ALLOCATION_ALIGNMENT 64
30 #endif
31 #else
32 #define XNN_ALLOCATION_ALIGNMENT 16
33 #endif
34
35 XNN_INTERNAL extern const struct xnn_allocator xnn_default_allocator;
36
xnn_allocate_memory(size_t memory_size)37 inline static void* xnn_allocate_memory(size_t memory_size) {
38 return xnn_params.allocator.allocate(xnn_params.allocator.context, memory_size);
39 }
40
xnn_allocate_zero_memory(size_t memory_size)41 inline static void* xnn_allocate_zero_memory(size_t memory_size) {
42 void* memory_pointer = xnn_params.allocator.allocate(xnn_params.allocator.context, memory_size);
43 if (memory_pointer != NULL) {
44 memset(memory_pointer, 0, memory_size);
45 }
46 return memory_pointer;
47 }
48
xnn_reallocate_memory(void * memory_pointer,size_t memory_size)49 inline static void* xnn_reallocate_memory(void* memory_pointer, size_t memory_size) {
50 return xnn_params.allocator.reallocate(xnn_params.allocator.context, memory_pointer, memory_size);
51 }
52
xnn_release_memory(void * memory_pointer)53 inline static void xnn_release_memory(void* memory_pointer) {
54 xnn_params.allocator.deallocate(xnn_params.allocator.context, memory_pointer);
55 }
56
xnn_allocate_simd_memory(size_t memory_size)57 inline static void* xnn_allocate_simd_memory(size_t memory_size) {
58 return xnn_params.allocator.aligned_allocate(xnn_params.allocator.context, XNN_ALLOCATION_ALIGNMENT, memory_size);
59 }
60
xnn_allocate_zero_simd_memory(size_t memory_size)61 inline static void* xnn_allocate_zero_simd_memory(size_t memory_size) {
62 void* memory_pointer = xnn_params.allocator.aligned_allocate(
63 xnn_params.allocator.context, XNN_ALLOCATION_ALIGNMENT, memory_size);
64 if (memory_pointer != NULL) {
65 memset(memory_pointer, 0, memory_size);
66 }
67 return memory_pointer;
68 }
69
xnn_release_simd_memory(void * memory_pointer)70 inline static void xnn_release_simd_memory(void* memory_pointer) {
71 xnn_params.allocator.aligned_deallocate(xnn_params.allocator.context, memory_pointer);
72 }
73
74 #if defined(__GNUC__) && defined(__BIGGEST_ALIGNMENT__) && (__BIGGEST_ALIGNMENT__ >= XNN_ALLOCATION_ALIGNMENT)
75 #define XNN_SIMD_ALLOCA(size) __builtin_alloca((size))
76 #elif (defined(__clang_major__) && (__clang_major__ >= 4)) || \
77 (defined(__GNUC__) && (__GNUC__ >= 5 || __GNUC__ == 4 && __GNUC_MINOR__ >= 7) && !defined(__INTEL_COMPILER))
78 #define XNN_SIMD_ALLOCA(size) __builtin_alloca_with_align((size), XNN_ALLOCATION_ALIGNMENT)
79 #elif defined(__GNUC__)
80 #define XNN_SIMD_ALLOCA(size) \
81 ((void*) ((((uintptr_t) __builtin_alloca((size) + XNN_ALLOCATION_ALIGNMENT)) | (XNN_ALLOCATION_ALIGNMENT - 1)) + 1))
82 #elif defined(_MSC_VER)
83 #define XNN_SIMD_ALLOCA(size) \
84 ((void*) ((((uintptr_t) _alloca((size) + XNN_ALLOCATION_ALIGNMENT)) | (XNN_ALLOCATION_ALIGNMENT - 1)) + 1))
85 #else
86 #define XNN_SIMD_ALLOCA(size) \
87 ((void*) ((((uintptr_t) alloca((size) + XNN_ALLOCATION_ALIGNMENT)) | (XNN_ALLOCATION_ALIGNMENT - 1)) + 1))
88 #endif
89
90 #define XNN_DEFAULT_CODE_BUFFER_SIZE 16384 // Default size for buffer to hold all generated microkernels, 16kb.
91 #define XNN_DEFAULT_MICROKERNEL_SIZE 4096 // Default size required for generating one microkernel, 4kb.
92 #define XNN_DEFAULT_WEIGHTS_BUFFER_SIZE 1048576 // Default size for buffer to hold repacked weights, 1MB.
93
94 #ifdef __cplusplus
95 extern "C" {
96 #endif
97
98 struct xnn_code_buffer {
99 // Pointer to allocated, externally managed memory.
100 void* start;
101 // Actual size of instructions (bytes). It is only safe to access code within
102 // this size.
103 size_t size;
104 // Maximum capacity of the buffer pointer to by `code`. This is the size of
105 // the currently mapped memory.
106 size_t capacity;
107 };
108
109 // Allocates a code region and associates it with `buf`.
110 enum xnn_status xnn_allocate_code_memory(struct xnn_code_buffer* buf, size_t size);
111 // Finalize buffer, users won't need to call this directly, called by Assembler.
112 #if XNN_PLATFORM_JIT
113 enum xnn_status xnn_finalize_code_memory(struct xnn_code_buffer* buf);
114 #endif
115 // Ensure that buf has at least n bytes free (i.e. buf->capacity - buf->size >= n), grows if not.
116 enum xnn_status xnn_reserve_code_memory(struct xnn_code_buffer* buf, size_t n);
117 // Free all memory associated with `buf`.
118 enum xnn_status xnn_release_code_memory(struct xnn_code_buffer* buf);
119
120 // Buffer to hold repacked weights.
121 struct xnn_weights_buffer {
122 // Pointer to allocated memory for weights.
123 void* start;
124 // Size of weights.
125 size_t size;
126 // Maximum capacity of this buffer pointed to by `code`. This is the size of the allcoated memory.
127 size_t capacity;
128 };
129
130 // Allocates a weights region and associates it with `buf`.
131 enum xnn_status xnn_allocate_weights_memory(struct xnn_weights_buffer* buf, size_t size);
132 // Free all memory associated with `buf`.
133 enum xnn_status xnn_release_weights_memory(struct xnn_weights_buffer* buf);
134 // Ensure that buf has at least n bytes free (i.e. buf->capacity - buf->size >= n), grows if not.
135 enum xnn_status xnn_reserve_weights_memory(struct xnn_weights_buffer* buf, size_t n);
136 // Releases unused memory in `buf`, and sets used memory to read-only. The address of allocated memory (`buf->start`)
137 // is fixed after this call. This should only be called after all the weights have been written.
138 enum xnn_status xnn_finalize_weights_memory(struct xnn_weights_buffer* buf);
139
140 #ifdef __cplusplus
141 } // extern "C"
142 #endif
143