• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_XLA_SERVICE_CPU_DISASSEMBLER_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_CPU_DISASSEMBLER_H_
18 
19 #include <memory>
20 #include <string>
21 
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
24 #include "llvm/MC/MCInstPrinter.h"
25 #include "llvm/MC/MCInstrAnalysis.h"
26 #include "llvm/MC/MCObjectFileInfo.h"
27 #include "llvm/MC/MCSubtargetInfo.h"
28 #include "llvm/Object/ObjectFile.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "tensorflow/compiler/xla/statusor.h"
31 #include "tensorflow/compiler/xla/types.h"
32 
33 namespace xla {
34 namespace cpu {
35 
36 struct DisassemblerResult {
DisassemblerResultDisassemblerResult37   DisassemblerResult(const string& text, size_t code_size_bytes)
38       : text(text), code_size_bytes(code_size_bytes) {}
39 
40   // The disassembled text sections of the object file.
41   string text;
42   // The total number of bytes of executable code in the object file.
43   uint64_t code_size_bytes;
44 };
45 
46 // Class for disassembling object files (and potentially other constructs) into
47 // x86/ARM assembly. Builds all the LLVM disassembly and instruction printing
48 // constructs from a given TargetMachine.
49 class Disassembler {
50  public:
51   explicit Disassembler(const llvm::TargetMachine& target_machine);
52 
53   // Returns a DisassemblerResult for the given object file, containing the
54   // disassembled code.
55   //
56   // If we couldn't retrieve a disassembler for this platform, an error status
57   // is returned.
58   StatusOr<DisassemblerResult> DisassembleObjectFile(
59       const llvm::object::ObjectFile& object_file) const;
60 
61  private:
62   const llvm::MCSubtargetInfo& subtarget_info_;
63   std::unique_ptr<llvm::MCObjectFileInfo> objfile_info_;
64   std::unique_ptr<llvm::MCContext> mc_context_;
65   std::unique_ptr<llvm::MCDisassembler> disassembler_;
66   std::unique_ptr<llvm::MCInstPrinter> inst_printer_;
67   std::unique_ptr<llvm::MCInstrAnalysis> inst_analysis_;
68 };
69 
70 }  // namespace cpu
71 }  // namespace xla
72 
73 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_CPU_DISASSEMBLER_H_
74