Home | History | Annotate | Download | only in aot
      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 #ifndef TENSORFLOW_COMPILER_AOT_CODEGEN_H_
     17 #define TENSORFLOW_COMPILER_AOT_CODEGEN_H_
     18 
     19 #include <string>
     20 #include <vector>
     21 
     22 #include "tensorflow/compiler/aot/compile.h"
     23 #include "tensorflow/compiler/tf2xla/tf2xla.pb.h"
     24 #include "tensorflow/core/lib/core/stringpiece.h"
     25 
     26 namespace tensorflow {
     27 namespace tfcompile {
     28 
     29 // CodegenOpts specifies code generation options for the generated header file
     30 // and the generated metadata object file.
     31 struct CodegenOpts {
     32   // The name of the generated C++ class, wrapping the generated function.
     33   string class_name;
     34 
     35   // Target triple for the architecture we're targeting.
     36   string target_triple;
     37 
     38   // Namespaces specifies a list of C++ namespaces to add to the generated
     39   // header.  If empty, all symbols will be in the global namespace.
     40   std::vector<string> namespaces;
     41 
     42   // If true, generate name-to-index data for Lookup{Arg,Result}Index methods.
     43   bool gen_name_to_index = false;
     44 
     45   // If true, generate program shape data for the ProgramShape method.
     46   bool gen_program_shape = false;
     47 };
     48 
     49 // Describes a generated metadata object file.
     50 struct MetadataResult {
     51   // These are top level "extern C" declarations that are expected to be visible
     52   // wherever program_shape_access_shim is emitted.
     53   std::vector<string> header_variable_decls;
     54 
     55   // program_shape_access_shim is a C++ expression that constructs the
     56   // xla::ProgramShape instance for the CompileResult passed to
     57   // GenerateMetadata.
     58   string program_shape_access_shim;
     59 
     60   // The contents of the object (".o") file.
     61   string object_file_data;
     62 };
     63 
     64 // Generates a metadata object file according to `opts` and `compile_result`.
     65 // The generated object file is returned via `metadata_result`.
     66 Status GenerateMetadata(const CodegenOpts& opts,
     67                         const CompileResult& compile_result,
     68                         MetadataResult* metadata_result);
     69 
     70 // GenerateHeader uses the meta-information from compile_result to generate a
     71 // C++ header giving access to the function in the generated object file.  The
     72 // header includes API usage documentation.
     73 //
     74 // metadata_result is an instance of MetadataResult obtained by a previous
     75 // invocation to GenerateMetadata.
     76 Status GenerateHeader(const CodegenOpts& opts, const tf2xla::Config& config,
     77                       const CompileResult& compile_result,
     78                       const MetadataResult& metadata_result, string* header);
     79 
     80 // ParseCppClass parses `cpp_class` into its `class_name` and `namespaces`
     81 // components.  The syntax is [[<optional_namespace>::],...]<class_name>.  This
     82 // mirrors the C++ syntax for referring to a class, where multiple namespaces
     83 // may precede the class name, separated by double-colons.
     84 Status ParseCppClass(const string& cpp_class, string* class_name,
     85                      std::vector<string>* namespaces);
     86 
     87 // ValidateCppIdent returns OK iff ident is a valid C++ identifier.  The msg is
     88 // appended to error messages.
     89 Status ValidateCppIdent(StringPiece ident, StringPiece msg);
     90 
     91 }  // namespace tfcompile
     92 }  // namespace tensorflow
     93 
     94 #endif  // TENSORFLOW_COMPILER_AOT_CODEGEN_H_
     95