LLVM  14.0.0git
AMDGPUAsmPrinter.h
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 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
15 #define LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
16 
17 #include "SIProgramInfo.h"
19 
20 struct amd_kernel_code_t;
21 
22 namespace llvm {
23 
24 class AMDGPUMachineFunction;
25 struct AMDGPUResourceUsageAnalysis;
26 class AMDGPUTargetStreamer;
27 class MCCodeEmitter;
28 class MCOperand;
29 
30 namespace AMDGPU {
31 namespace HSAMD {
32 class MetadataStreamer;
33 }
34 } // namespace AMDGPU
35 
36 namespace amdhsa {
37 struct kernel_descriptor_t;
38 }
39 
40 class AMDGPUAsmPrinter final : public AsmPrinter {
41 private:
42  void initializeTargetID(const Module &M);
43 
44  AMDGPUResourceUsageAnalysis *ResourceUsage;
45 
46  SIProgramInfo CurrentProgramInfo;
47 
48  std::unique_ptr<AMDGPU::HSAMD::MetadataStreamer> HSAMetadataStream;
49 
50  MCCodeEmitter *DumpCodeInstEmitter = nullptr;
51 
52  uint64_t getFunctionCodeSize(const MachineFunction &MF) const;
53 
54  void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF);
55  void getAmdKernelCode(amd_kernel_code_t &Out, const SIProgramInfo &KernelInfo,
56  const MachineFunction &MF) const;
57  void findNumUsedRegistersSI(const MachineFunction &MF,
58  unsigned &NumSGPR,
59  unsigned &NumVGPR) const;
60 
61  /// Emit register usage information so that the GPU driver
62  /// can correctly setup the GPU state.
63  void EmitProgramInfoSI(const MachineFunction &MF,
64  const SIProgramInfo &KernelInfo);
65  void EmitPALMetadata(const MachineFunction &MF,
66  const SIProgramInfo &KernelInfo);
67  void emitPALFunctionMetadata(const MachineFunction &MF);
68  void emitCommonFunctionComments(uint32_t NumVGPR,
69  Optional<uint32_t> NumAGPR,
70  uint32_t TotalNumVGPR,
71  uint32_t NumSGPR,
72  uint64_t ScratchSize,
73  uint64_t CodeSize,
74  const AMDGPUMachineFunction* MFI);
75 
76  uint16_t getAmdhsaKernelCodeProperties(
77  const MachineFunction &MF) const;
78 
79  amdhsa::kernel_descriptor_t getAmdhsaKernelDescriptor(
80  const MachineFunction &MF,
81  const SIProgramInfo &PI) const;
82 
83 public:
85  std::unique_ptr<MCStreamer> Streamer);
86 
87  StringRef getPassName() const override;
88 
89  const MCSubtargetInfo* getGlobalSTI() const;
90 
92 
93  bool doFinalization(Module &M) override;
94  bool runOnMachineFunction(MachineFunction &MF) override;
95 
96  /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated
97  /// pseudo lowering.
98  bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
99 
100  /// Lower the specified LLVM Constant to an MCExpr.
101  /// The AsmPrinter::lowerConstantof does not know how to lower
102  /// addrspacecast, therefore they should be lowered by this function.
103  const MCExpr *lowerConstant(const Constant *CV) override;
104 
105  /// tblgen'erated driver function for lowering simple MI->MC pseudo
106  /// instructions.
108  const MachineInstr *MI);
109 
110  /// Implemented in AMDGPUMCInstLower.cpp
111  void emitInstruction(const MachineInstr *MI) override;
112 
113  void emitFunctionBodyStart() override;
114 
115  void emitFunctionBodyEnd() override;
116 
117  void emitFunctionEntryLabel() override;
118 
119  void emitBasicBlockStart(const MachineBasicBlock &MBB) override;
120 
121  void emitGlobalVariable(const GlobalVariable *GV) override;
122 
123  void emitStartOfAsmFile(Module &M) override;
124 
125  void emitEndOfAsmFile(Module &M) override;
126 
128  const MachineBasicBlock *MBB) const override;
129 
130  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
131  const char *ExtraCode, raw_ostream &O) override;
132 
133 protected:
134  void getAnalysisUsage(AnalysisUsage &AU) const override;
135 
136  std::vector<std::string> DisasmLines, HexLines;
138 };
139 
140 } // end namespace llvm
141 
142 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
llvm::AMDGPUAsmPrinter::emitBasicBlockStart
void emitBasicBlockStart(const MachineBasicBlock &MBB) override
Targets can override this to emit stuff at the start of a basic block.
Definition: AMDGPUAsmPrinter.cpp:285
AsmPrinter.h
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:105
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::AMDGPUAsmPrinter::AMDGPUAsmPrinter
AMDGPUAsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > Streamer)
Definition: AMDGPUAsmPrinter.cpp:83
llvm::AMDGPUAsmPrinter::emitFunctionBodyStart
void emitFunctionBodyStart() override
Targets can override this to emit stuff before the first basic block in the function.
Definition: AMDGPUAsmPrinter.cpp:176
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::AMDGPUAsmPrinter::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
Record analysis usage.
Definition: AMDGPUAsmPrinter.cpp:1138
llvm::AMDGPUAsmPrinter::isBlockOnlyReachableByFallthrough
bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const override
Return true if the basic block has exactly one predecessor and the control transfer mechanism between...
Definition: AMDGPUAsmPrinter.cpp:162
llvm::Optional< uint32_t >
llvm::AMDGPUAsmPrinter
Definition: AMDGPUAsmPrinter.h:40
llvm::AMDGPUAsmPrinter::lowerOperand
bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const
Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated pseudo lowering.
Definition: AMDGPUMCInstLower.cpp:160
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:199
llvm::AMDGPUAsmPrinter::emitInstruction
void emitInstruction(const MachineInstr *MI) override
Implemented in AMDGPUMCInstLower.cpp.
Definition: AMDGPUMCInstLower.cpp:173
llvm::AsmPrinter::OutStreamer
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:97
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:49
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
SIProgramInfo.h
llvm::AMDGPUAsmPrinter::PrintAsmOperand
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.
Definition: AMDGPUAsmPrinter.cpp:1098
llvm::AMDGPUAsmPrinter::emitStartOfAsmFile
void emitStartOfAsmFile(Module &M) override
This virtual method can be overridden by targets that want to emit something at the start of their fi...
Definition: AMDGPUAsmPrinter.cpp:111
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:197
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
uint64_t
llvm::AMDGPUAsmPrinter::emitEndOfAsmFile
void emitEndOfAsmFile(Module &M) override
This virtual method can be overridden by targets that want to emit something at the end of their file...
Definition: AMDGPUAsmPrinter.cpp:143
llvm::AMDGPUAsmPrinter::getTargetStreamer
AMDGPUTargetStreamer * getTargetStreamer() const
Definition: AMDGPUAsmPrinter.cpp:105
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:79
llvm::AMDGPUAsmPrinter::DisasmLines
std::vector< std::string > DisasmLines
Definition: AMDGPUAsmPrinter.h:136
llvm::AMDGPUAsmPrinter::getGlobalSTI
const MCSubtargetInfo * getGlobalSTI() const
Definition: AMDGPUAsmPrinter.cpp:101
llvm::AsmPrinter::MF
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:100
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::MachineFunction
Definition: MachineFunction.h:234
llvm::AMDGPUAsmPrinter::lowerConstant
const MCExpr * lowerConstant(const Constant *CV) override
Lower the specified LLVM Constant to an MCExpr.
Definition: AMDGPUMCInstLower.cpp:167
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::AMDGPUAsmPrinter::emitFunctionBodyEnd
void emitFunctionBodyEnd() override
Targets can override this to emit stuff after the last basic block in the function.
Definition: AMDGPUAsmPrinter.cpp:221
uint32_t
llvm::AMDGPUTargetStreamer
Definition: AMDGPUTargetStreamer.h:34
amd_kernel_code_t
AMD Kernel Code Object (amd_kernel_code_t).
Definition: AMDKernelCodeT.h:526
llvm::AMDGPUAsmPrinter::DisasmLineMaxLen
size_t DisasmLineMaxLen
Definition: AMDGPUAsmPrinter.h:137
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
uint16_t
llvm::MCCodeEmitter
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:21
llvm::amdhsa::kernel_descriptor_t
Definition: AMDHSAKernelDescriptor.h:165
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:82
llvm::AMDGPUAsmPrinter::getPassName
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
Definition: AMDGPUAsmPrinter.cpp:97
llvm::AMDGPUAsmPrinter::doFinalization
bool doFinalization(Module &M) override
Shut down the asmprinter.
Definition: AMDGPUAsmPrinter.cpp:332
llvm::SIProgramInfo
Track resource usage for kernels / entry functions.
Definition: SIProgramInfo.h:25
llvm::AMDGPUAsmPrinter::emitPseudoExpansionLowering
bool emitPseudoExpansionLowering(MCStreamer &OutStreamer, const MachineInstr *MI)
tblgen'erated driver function for lowering simple MI->MC pseudo instructions.
llvm::AsmPrinter::TM
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:85
llvm::AMDGPUResourceUsageAnalysis
Definition: AMDGPUResourceUsageAnalysis.h:27
llvm::AMDGPUAsmPrinter::emitFunctionEntryLabel
void emitFunctionEntryLabel() override
EmitFunctionEntryLabel - Emit the label that is the entrypoint for the function.
Definition: AMDGPUAsmPrinter.cpp:260
llvm::MCOperand
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
llvm::AMDGPUAsmPrinter::runOnMachineFunction
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
Definition: AMDGPUAsmPrinter.cpp:437
llvm::AMDGPUAsmPrinter::HexLines
std::vector< std::string > HexLines
Definition: AMDGPUAsmPrinter.h:136
llvm::AMDGPUMachineFunction
Definition: AMDGPUMachineFunction.h:20
llvm::AMDGPUAsmPrinter::emitGlobalVariable
void emitGlobalVariable(const GlobalVariable *GV) override
Emit the specified global variable to the .s file.
Definition: AMDGPUAsmPrinter.cpp:297
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:75
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35