1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #include "tensorflow/compiler/aot/runtime.h" 17 18 #include <stdlib.h> 19 20 #include "tensorflow/core/platform/dynamic_annotations.h" 21 22 namespace tensorflow { 23 namespace tfcompile { 24 namespace runtime { 25 26 namespace { 27 28 // Inline memory allocation routines here, because depending on '//base' brings 29 // in libraries which use c++ streams, which adds considerable code size on 30 // android. 31 inline void* aligned_malloc(size_t size, int minimum_alignment) { 32 #if defined(__ANDROID__) || defined(OS_ANDROID) || defined(OS_CYGWIN) 33 return memalign(minimum_alignment, size); 34 #elif defined(COMPILER_MSVC) 35 return _aligned_malloc(size, minimum_alignment); 36 #else // !__ANDROID__ && !OS_ANDROID && !OS_CYGWIN 37 void* ptr = nullptr; 38 // posix_memalign requires that the requested alignment be at least 39 // sizeof(void*). In this case, fall back on malloc which should return memory 40 // aligned to at least the size of a pointer. 41 const int required_alignment = sizeof(void*); 42 if (minimum_alignment < required_alignment) return malloc(size); 43 if (posix_memalign(&ptr, minimum_alignment, size) != 0) 44 return nullptr; 45 else 46 return ptr; 47 #endif 48 } 49 50 inline void aligned_free(void* aligned_memory) { 51 #if defined(COMPILER_MSVC) 52 _aligned_free(aligned_memory); 53 #else 54 free(aligned_memory); 55 #endif 56 } 57 58 size_t align_to(size_t n, size_t align) { 59 return (((n - 1) / align) + 1) * align; 60 } 61 62 } // namespace 63 64 size_t aligned_buffer_bytes(const intptr_t* sizes, size_t n) { 65 size_t total = 0; 66 for (size_t i = 0; i < n; ++i) { 67 if (sizes[i] != -1) { 68 total += align_to(sizes[i], kAlign); 69 } 70 } 71 return total; 72 } 73 74 void* MallocContiguousBuffers(const intptr_t* sizes, size_t n, void** bufs, 75 bool annotate_initialized) { 76 const size_t total = aligned_buffer_bytes(sizes, n); 77 void* contiguous = nullptr; 78 if (total > 0) { 79 contiguous = aligned_malloc(total, kAlign); 80 if (annotate_initialized) { 81 // Since the memory for temp buffers is written to by JITed code, msan has 82 // no way of knowing the memory was initialized, so explicitly mark it. 83 TF_ANNOTATE_MEMORY_IS_INITIALIZED(contiguous, total); 84 } 85 } 86 uintptr_t pos = reinterpret_cast<uintptr_t>(contiguous); 87 for (size_t i = 0; i < n; ++i) { 88 if (sizes[i] == -1) { 89 bufs[i] = nullptr; 90 } else { 91 bufs[i] = reinterpret_cast<void*>(pos); 92 pos += align_to(sizes[i], kAlign); 93 } 94 } 95 return contiguous; 96 } 97 98 void FreeContiguous(void* contiguous) { 99 if (contiguous != nullptr) { 100 aligned_free(contiguous); 101 } 102 } 103 104 } // namespace runtime 105 } // namespace tfcompile 106 } // namespace tensorflow 107