Home | History | Annotate | Download | only in aot
      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 // This file defines utilities to help "embed" protocol buffers into object
     17 // (".o") files.  These C++ binaries and shared objects can link in these .o to
     18 // get access to said protocol buffers at runtime.
     19 
     20 #ifndef TENSORFLOW_COMPILER_AOT_EMBEDDED_PROTOCOL_BUFFERS_H_
     21 #define TENSORFLOW_COMPILER_AOT_EMBEDDED_PROTOCOL_BUFFERS_H_
     22 
     23 #include "tensorflow/compiler/xla/statusor.h"
     24 #include "tensorflow/core/platform/protobuf.h"
     25 
     26 namespace tensorflow {
     27 namespace tfcompile {
     28 using xla::StatusOr;
     29 
     30 // Represents a protocol buffer embedded into an object file and describes a way
     31 // to access it at runtime.
     32 struct EmbeddedProtocolBuffer {
     33   // cpp_shim_expression is a C++ expression that creates an instance of said
     34   // protocol buffer when executed.
     35   string cpp_shim_expression;
     36 
     37   // cpp_variable_decl is an "extern C" array declaration that is used in
     38   // cpp_shim_expression.  It must be visible wherever cpp_shim_expression is
     39   // emitted.
     40   string cpp_variable_decl;
     41 
     42   // The contents of the object (".o") file the protocol buffer is embbed in.
     43   // This needs to be linked in to any program that wants to execute
     44   // cpp_variable_decl .
     45   string object_file_data;
     46 };
     47 
     48 // Creates an object file that contains `proto`.
     49 //
     50 // `proto` is allowed to be nullptr, in which case the generated C++ shim
     51 // expression is just `nullptr`, and the generated object file does not define
     52 // any symbols.
     53 //
     54 // `target_triple` is the target triple for the target architecture for the
     55 // generated object file.
     56 //
     57 // `symbol_prefix` is prefix that is guaranteed to be unique across the binary
     58 // or DSO the generated object file will be linked into.
     59 //
     60 // `qualified_cpp_protobuf_name` is a qualified ("qualified" as in C++
     61 // namespace qualified) protocol buffer name.  This needs is only used in
     62 // EmbeddedProtocolBuffer::cpp_shim_expression so relatively qualified
     63 // names are fine as long as they're valid wherever cpp_shim_expression
     64 // is emitted.
     65 StatusOr<EmbeddedProtocolBuffer> CreateEmbeddedProtocolBuffer(
     66     StringPiece target_triple, StringPiece symbol_prefix,
     67     StringPiece qualified_cpp_protobuf_name,
     68     const ::tensorflow::protobuf::MessageLite* proto);
     69 
     70 }  // namespace tfcompile
     71 }  // namespace tensorflow
     72 
     73 #endif  // TENSORFLOW_COMPILER_AOT_EMBEDDED_PROTOCOL_BUFFERS_H_
     74