LLVM  12.0.0git
Go to the documentation of this file.
1 //===-- AMDGPUAsmPrinter.h - Print AMDGPU assembly code ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// AMDGPU Assembly printer class.
11 //
12 //===----------------------------------------------------------------------===//
17 #include "AMDGPU.h"
18 #include "AMDKernelCodeT.h"
20 #include "SIProgramInfo.h"
21 #include "llvm/ADT/StringRef.h"
24 #include <cstddef>
25 #include <cstdint>
26 #include <limits>
27 #include <memory>
28 #include <string>
29 #include <vector>
31 namespace llvm {
33 class AMDGPUMachineFunction;
34 class AMDGPUTargetStreamer;
35 class MCCodeEmitter;
36 class MCOperand;
37 class GCNSubtarget;
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 NumAGPR = 0;
47  int32_t NumExplicitSGPR = 0;
48  uint64_t PrivateSegmentSize = 0;
49  bool UsesVCC = false;
50  bool UsesFlatScratch = false;
51  bool HasDynamicallySizedStack = false;
52  bool HasRecursion = false;
54  int32_t getTotalNumSGPRs(const GCNSubtarget &ST) const;
55  int32_t getTotalNumVGPRs(const GCNSubtarget &ST) const;
56  };
58  SIProgramInfo CurrentProgramInfo;
61  std::unique_ptr<AMDGPU::HSAMD::MetadataStreamer> HSAMetadataStream;
63  MCCodeEmitter *DumpCodeInstEmitter = nullptr;
65  uint64_t getFunctionCodeSize(const MachineFunction &MF) const;
66  SIFunctionResourceInfo analyzeResourceUsage(const MachineFunction &MF) const;
68  void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF);
69  void getAmdKernelCode(amd_kernel_code_t &Out, const SIProgramInfo &KernelInfo,
70  const MachineFunction &MF) const;
71  void findNumUsedRegistersSI(const MachineFunction &MF,
72  unsigned &NumSGPR,
73  unsigned &NumVGPR) const;
75  /// Emit register usage information so that the GPU driver
76  /// can correctly setup the GPU state.
77  void EmitProgramInfoSI(const MachineFunction &MF,
78  const SIProgramInfo &KernelInfo);
79  void EmitPALMetadata(const MachineFunction &MF,
80  const SIProgramInfo &KernelInfo);
81  void emitCommonFunctionComments(uint32_t NumVGPR,
82  Optional<uint32_t> NumAGPR,
83  uint32_t TotalNumVGPR,
84  uint32_t NumSGPR,
85  uint64_t ScratchSize,
86  uint64_t CodeSize,
87  const AMDGPUMachineFunction* MFI);
89  uint16_t getAmdhsaKernelCodeProperties(
90  const MachineFunction &MF) const;
92  amdhsa::kernel_descriptor_t getAmdhsaKernelDescriptor(
93  const MachineFunction &MF,
94  const SIProgramInfo &PI) const;
96 public:
98  std::unique_ptr<MCStreamer> Streamer);
100  StringRef getPassName() const override;
102  const MCSubtargetInfo* getGlobalSTI() const;
106  bool doFinalization(Module &M) override;
107  bool runOnMachineFunction(MachineFunction &MF) override;
109  /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated
110  /// pseudo lowering.
111  bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
113  /// Lower the specified LLVM Constant to an MCExpr.
114  /// The AsmPrinter::lowerConstantof does not know how to lower
115  /// addrspacecast, therefore they should be lowered by this function.
116  const MCExpr *lowerConstant(const Constant *CV) override;
118  /// tblgen'erated driver function for lowering simple MI->MC pseudo
119  /// instructions.
121  const MachineInstr *MI);
123  /// Implemented in AMDGPUMCInstLower.cpp
124  void emitInstruction(const MachineInstr *MI) override;
126  void emitFunctionBodyStart() override;
128  void emitFunctionBodyEnd() override;
130  void emitFunctionEntryLabel() override;
132  void emitBasicBlockStart(const MachineBasicBlock &MBB) override;
134  void emitGlobalVariable(const GlobalVariable *GV) override;
136  void emitStartOfAsmFile(Module &M) override;
138  void emitEndOfAsmFile(Module &M) override;
141  const MachineBasicBlock *MBB) const override;
143  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
144  const char *ExtraCode, raw_ostream &O) override;
146 protected:
147  std::vector<std::string> DisasmLines, HexLines;
149 };
151 } // end namespace llvm
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:96
This class represents lattice values for constants.
Definition: AllocatorList.h:23
const MCSubtargetInfo * getGlobalSTI() const
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:67
bool emitPseudoExpansionLowering(MCStreamer &OutStreamer, const MachineInstr *MI)
tblgen&#39;erated driver function for lowering simple MI->MC pseudo instructions.
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &O) override
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant...
void emitStartOfAsmFile(Module &M) override
This virtual method can be overridden by targets that want to emit something at the start of their fi...
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:99
Track resource usage for kernels / entry functions.
Definition: SIProgramInfo.h:21
MachineBasicBlock & MBB
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:35
void emitGlobalVariable(const GlobalVariable *GV) override
Emit the specified global variable to the .s file.
AMDGPUAsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > Streamer)
unsigned getTotalNumVGPRs(const MCSubtargetInfo *STI)
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:196
This is an important base class in LLVM.
Definition: Constant.h:41
void emitInstruction(const MachineInstr *MI) override
Implemented in AMDGPUMCInstLower.cpp.
void emitFunctionBodyStart() override
Targets can override this to emit stuff before the first basic block in the function.
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:21
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:84
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:81
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:62
bool doFinalization(Module &M) override
Shut down the asmprinter.
Generic base class for all target subtargets.
void emitFunctionBodyEnd() override
Targets can override this to emit stuff after the last basic block in the function.
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
void emitEndOfAsmFile(Module &M) override
This virtual method can be overridden by targets that want to emit something at the end of their file...
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:65
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
void emitBasicBlockStart(const MachineBasicBlock &MBB) override
Targets can override this to emit stuff at the start of a basic block.
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:34