LLVM  8.0.1
AMDGPUAsmPrinter.h
Go to the documentation of this file.
1 //===-- AMDGPUAsmPrinter.h - Print AMDGPU assembly code ---------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// AMDGPU Assembly printer class.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
16 #define LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
17 
18 #include "AMDGPU.h"
19 #include "AMDKernelCodeT.h"
21 #include "SIProgramInfo.h"
22 #include "llvm/ADT/StringRef.h"
25 #include <cstddef>
26 #include <cstdint>
27 #include <limits>
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 namespace llvm {
33 
34 class AMDGPUMachineFunction;
35 class AMDGPUTargetStreamer;
36 class MCOperand;
37 class GCNSubtarget;
38 
39 class AMDGPUAsmPrinter final : public AsmPrinter {
40 private:
41  // Track resource usage for callee functions.
42  struct SIFunctionResourceInfo {
43  // Track the number of explicitly used VGPRs. Special registers reserved at
44  // the end are tracked separately.
45  int32_t NumVGPR = 0;
46  int32_t NumExplicitSGPR = 0;
47  uint64_t PrivateSegmentSize = 0;
48  bool UsesVCC = false;
49  bool UsesFlatScratch = false;
50  bool HasDynamicallySizedStack = false;
51  bool HasRecursion = false;
52 
53  int32_t getTotalNumSGPRs(const GCNSubtarget &ST) const;
54  };
55 
56  SIProgramInfo CurrentProgramInfo;
58 
59  std::unique_ptr<AMDGPU::HSAMD::MetadataStreamer> HSAMetadataStream;
60  std::map<uint32_t, uint32_t> PALMetadataMap;
61 
62  uint64_t getFunctionCodeSize(const MachineFunction &MF) const;
63  SIFunctionResourceInfo analyzeResourceUsage(const MachineFunction &MF) const;
64 
65  void readPALMetadata(Module &M);
66  void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF);
67  void getAmdKernelCode(amd_kernel_code_t &Out, const SIProgramInfo &KernelInfo,
68  const MachineFunction &MF) const;
69  void findNumUsedRegistersSI(const MachineFunction &MF,
70  unsigned &NumSGPR,
71  unsigned &NumVGPR) const;
72 
73  /// Emit register usage information so that the GPU driver
74  /// can correctly setup the GPU state.
75  void EmitProgramInfoSI(const MachineFunction &MF,
76  const SIProgramInfo &KernelInfo);
77  void EmitPALMetadata(const MachineFunction &MF,
78  const SIProgramInfo &KernelInfo);
79  void emitCommonFunctionComments(uint32_t NumVGPR,
80  uint32_t NumSGPR,
81  uint64_t ScratchSize,
82  uint64_t CodeSize,
83  const AMDGPUMachineFunction* MFI);
84 
85  uint16_t getAmdhsaKernelCodeProperties(
86  const MachineFunction &MF) const;
87 
88  amdhsa::kernel_descriptor_t getAmdhsaKernelDescriptor(
89  const MachineFunction &MF,
90  const SIProgramInfo &PI) const;
91 
92 public:
94  std::unique_ptr<MCStreamer> Streamer);
95 
96  StringRef getPassName() const override;
97 
98  const MCSubtargetInfo* getSTI() const;
99 
101 
102  bool doFinalization(Module &M) override;
103  bool runOnMachineFunction(MachineFunction &MF) override;
104 
105  /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated
106  /// pseudo lowering.
107  bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
108 
109  /// Lower the specified LLVM Constant to an MCExpr.
110  /// The AsmPrinter::lowerConstantof does not know how to lower
111  /// addrspacecast, therefore they should be lowered by this function.
112  const MCExpr *lowerConstant(const Constant *CV) override;
113 
114  /// tblgen'erated driver function for lowering simple MI->MC pseudo
115  /// instructions.
117  const MachineInstr *MI);
118 
119  /// Implemented in AMDGPUMCInstLower.cpp
120  void EmitInstruction(const MachineInstr *MI) override;
121 
122  void EmitFunctionBodyStart() override;
123 
124  void EmitFunctionBodyEnd() override;
125 
126  void EmitFunctionEntryLabel() override;
127 
128  void EmitBasicBlockStart(const MachineBasicBlock &MBB) const override;
129 
130  void EmitGlobalVariable(const GlobalVariable *GV) override;
131 
132  void EmitStartOfAsmFile(Module &M) override;
133 
134  void EmitEndOfAsmFile(Module &M) override;
135 
137  const MachineBasicBlock *MBB) const override;
138 
139  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
140  unsigned AsmVariant, const char *ExtraCode,
141  raw_ostream &O) override;
142 
143 protected:
144  mutable std::vector<std::string> DisasmLines, HexLines;
145  mutable size_t DisasmLineMaxLen;
146 };
147 
148 } // end namespace llvm
149 
150 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
void EmitGlobalVariable(const GlobalVariable *GV) override
Emit the specified global variable to the .s file.
const MCExpr * lowerConstant(const Constant *CV) override
Lower the specified LLVM Constant to an MCExpr.
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:94
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
void EmitFunctionBodyEnd() override
Targets can override this to emit stuff after the last basic block in the function.
bool emitPseudoExpansionLowering(MCStreamer &OutStreamer, const MachineInstr *MI)
tblgen&#39;erated driver function for lowering simple MI->MC pseudo instructions.
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:97
const MCSubtargetInfo * getSTI() const
Track resource usage for kernels / entry functions.
Definition: SIProgramInfo.h:22
AMD Kernel Code Object (amd_kernel_code_t).
Defines struct to track resource usage for kernels and entry functions.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
AMDGPUAsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > Streamer)
void EmitInstruction(const MachineInstr *MI) override
Implemented in AMDGPUMCInstLower.cpp.
AMDGPU HSA Metadata Streamer.
bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const override
Return true if the basic block has exactly one predecessor and the control transfer mechanism between...
Streaming machine code generation interface.
Definition: MCStreamer.h:189
This is an important base class in LLVM.
Definition: Constant.h:42
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, unsigned AsmVariant, const char *ExtraCode, raw_ostream &O) override
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant...
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:82
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:79
void EmitEndOfAsmFile(Module &M) override
This virtual method can be overridden by targets that want to emit something at the end of their file...
void EmitBasicBlockStart(const MachineBasicBlock &MBB) const override
Targets can override this to emit stuff at the start of a basic block.
AMDGPUTargetStreamer * getTargetStreamer() const
AMDHSA kernel descriptor definitions.
std::vector< std::string > HexLines
unsigned getTotalNumSGPRs(const MCSubtargetInfo *STI)
MachineOperand class - Representation of each machine instruction operand.
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
std::vector< std::string > DisasmLines
void EmitFunctionEntryLabel() override
EmitFunctionEntryLabel - Emit the label that is the entrypoint for the function.
Representation of each machine instruction.
Definition: MachineInstr.h:64
void EmitFunctionBodyStart() override
Targets can override this to emit stuff before the first basic block in the function.
bool doFinalization(Module &M) override
Shut down the asmprinter.
Generic base class for all target subtargets.
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
void EmitStartOfAsmFile(Module &M) override
This virtual method can be overridden by targets that want to emit something at the start of their fi...
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const
Wrapper for MCInstLowering.lowerOperand() for the tblgen&#39;erated pseudo lowering.
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35