1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3Licensed under the Apache License, Version 2.0 (the "License"); 4you may not use this file except in compliance with the License. 5You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9Unless required by applicable law or agreed to in writing, software 10distributed under the License is distributed on an "AS IS" BASIS, 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12See the License for the specific language governing permissions and 13limitations under the License. 14==============================================================================*/ 15 16#ifndef TENSORFLOW_COMPILER_XLA_SERVICE_CPU_EXTERNAL_CONSTANT_POOL_H_ 17#define TENSORFLOW_COMPILER_XLA_SERVICE_CPU_EXTERNAL_CONSTANT_POOL_H_ 18 19#include <memory> 20 21#include "tensorflow/compiler/xla/literal_util.h" 22#include "tensorflow/core/lib/gtl/flatmap.h" 23#include "tensorflow/core/platform/mem.h" 24 25namespace xla { 26namespace cpu { 27// An ExternalConstantPool maintains a set of constants kept external to 28// generated LLVM IR. These constants are accessed from the IR via globals with 29// extern linkage. This current incarnation of ExternalConstantPool only 30// supports the JIT CPU backend; the AOT backend is not supported. 31// 32// Implementation-wise, this is a simple wrapper around a map of strings to byte 33// buffers. This simply implementation works in a JIT scenario. This class 34// will have to become smarter if we decide to support external constant pools 35// on AOT compiles in the future. 36class ExternalConstantPool { 37 public: 38 // Inserts a buffer with the contents of `literal` into the constant pool with 39 // the name `name`. It is an error to try to insert two constants with the 40 // same `name` into the same constant pool. The buffer for literal is aligned 41 // to `aligment` bytes, and `alignment` must be a power of 2. 42 // 43 // The constant pool copies out the contents of `literal` into a buffer it 44 // owns -- it does not keep pointers to `literal`, or to memory owned by 45 // `literal`. 46 void Insert(string name, const Literal& literal, int64 alignment); 47 48 // Find the constant with name `name` in this constant pool. If there isn't 49 // such constant, return nullptr. 50 const uint8* Find(const string& name); 51 52 private: 53 // We need to `AlignedFree` pointers allocated into `entries_` since we 54 // allocate them with `AlignedMalloc`. 55 struct FreeDeleter { 56 void operator()(void* ptr) { tensorflow::port::AlignedFree(ptr); } 57 }; 58 59 tensorflow::gtl::FlatMap<string, std::unique_ptr<uint8, FreeDeleter>> 60 entries_; 61}; 62} // namespace cpu 63} // namespace xla 64 65#endif // TENSORFLOW_COMPILER_XLA_SERVICE_CPU_EXTERNAL_CONSTANT_POOL_H_ 66