1 /* Copyright 2018 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/xla/service/cpu/target_machine_features.h" 17 #include "tensorflow/core/platform/logging.h" 18 19 namespace xla { 20 namespace cpu { 21 22 llvm::TargetTransformInfo* LLVMTargetMachineFeatures::GetTargetTransformInfoFor( 23 const llvm::Function& function) const { 24 auto it = target_transform_info_cache_.find(&function); 25 if (it == target_transform_info_cache_.end()) { 26 auto emplace_result = target_transform_info_cache_.emplace( 27 &function, target_machine_->getTargetTransformInfo(function)); 28 CHECK(emplace_result.second); 29 it = emplace_result.first; 30 } 31 32 return &it->second; 33 } 34 35 int64 LLVMTargetMachineFeatures::minimum_alignment_for_allocation( 36 int64 size_bytes) const { 37 // GLibc malloc returns a pointer with alignment 8 on 32-bit platforms and 16 38 // on 64-bit platforms. TCMalloc returns a pointer with alignment 8 for 39 // allocations smaller than kMallocAlignmentThreshold bytes and at least 40 // alignment 16 for allocations greater than or equal to 41 // kMallocAlignmentThreshold bytes. N.B. We could improve on this lower bound 42 // by explicitly allocating the memory with posix_memalign. This is 43 // complicated by our desire to allow parameter buffers created by clients to 44 // be consumed directly by the JIT. 45 if (size_bytes == 0) { 46 // No need to align empty buffers. 47 return 1; 48 } 49 50 const int64 kMallocAlignmentThreshold = 512; 51 52 int pointer_size = target_machine_->getPointerSize(0); 53 int buffer_alignment = 54 size_bytes >= kMallocAlignmentThreshold ? 2 * pointer_size : pointer_size; 55 DCHECK_GT(buffer_alignment, 0); 56 57 return buffer_alignment; 58 } 59 60 } // namespace cpu 61 } // namespace xla 62