xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 "AMDGPUMCResourceInfo.h"
18 #include "SIProgramInfo.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 
21 namespace llvm {
22 
23 class AMDGPUMachineFunction;
24 class AMDGPUResourceUsageAnalysis;
25 class AMDGPUTargetStreamer;
26 class MCCodeEmitter;
27 class MCOperand;
28 class MCResourceInfo;
29 
30 namespace AMDGPU {
31 struct MCKernelDescriptor;
32 struct AMDGPUMCKernelCodeT;
33 namespace HSAMD {
34 class MetadataStreamer;
35 }
36 } // namespace AMDGPU
37 
38 class AMDGPUAsmPrinter final : public AsmPrinter {
39 public:
40   static char ID;
41 
42 private:
43   unsigned CodeObjectVersion;
44   void initializeTargetID(const Module &M);
45 
46   const AMDGPUResourceUsageAnalysisWrapperPass::FunctionResourceInfo
47       *ResourceUsage;
48 
49   MCResourceInfo RI;
50 
51   SIProgramInfo CurrentProgramInfo;
52 
53   std::unique_ptr<AMDGPU::HSAMD::MetadataStreamer> HSAMetadataStream;
54 
55   MCCodeEmitter *DumpCodeInstEmitter = nullptr;
56 
57   void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF);
58   void getAmdKernelCode(AMDGPU::AMDGPUMCKernelCodeT &Out,
59                         const SIProgramInfo &KernelInfo,
60                         const MachineFunction &MF) const;
61 
62   /// Emit register usage information so that the GPU driver
63   /// can correctly setup the GPU state.
64   void EmitProgramInfoSI(const MachineFunction &MF,
65                          const SIProgramInfo &KernelInfo);
66   void EmitPALMetadata(const MachineFunction &MF,
67                        const SIProgramInfo &KernelInfo);
68   void emitPALFunctionMetadata(const MachineFunction &MF);
69   void emitCommonFunctionComments(const MCExpr *NumVGPR, const MCExpr *NumAGPR,
70                                   const MCExpr *TotalNumVGPR,
71                                   const MCExpr *NumSGPR,
72                                   const MCExpr *ScratchSize, uint64_t CodeSize,
73                                   const AMDGPUMachineFunction *MFI);
74   void emitResourceUsageRemarks(const MachineFunction &MF,
75                                 const SIProgramInfo &CurrentProgramInfo,
76                                 bool isModuleEntryFunction, bool hasMAIInsts);
77 
78   const MCExpr *getAmdhsaKernelCodeProperties(const MachineFunction &MF) const;
79 
80   AMDGPU::MCKernelDescriptor
81   getAmdhsaKernelDescriptor(const MachineFunction &MF,
82                             const SIProgramInfo &PI) const;
83 
84   void initTargetStreamer(Module &M);
85 
86   SmallString<128> getMCExprStr(const MCExpr *Value);
87 
88   /// Attempts to replace the validation that is missed in getSIProgramInfo due
89   /// to MCExpr being unknown. Invoked during doFinalization such that the
90   /// MCResourceInfo symbols are known.
91   void validateMCResourceInfo(Function &F);
92 
93 public:
94   explicit AMDGPUAsmPrinter(TargetMachine &TM,
95                             std::unique_ptr<MCStreamer> Streamer);
96 
97   StringRef getPassName() const override;
98 
99   const MCSubtargetInfo* getGlobalSTI() const;
100 
101   AMDGPUTargetStreamer* getTargetStreamer() const;
102 
103   bool doInitialization(Module &M) override;
104   bool doFinalization(Module &M) override;
105   bool runOnMachineFunction(MachineFunction &MF) override;
106 
107   /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated
108   /// pseudo lowering.
109   bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
110 
111   /// Lower the specified LLVM Constant to an MCExpr.
112   /// The AsmPrinter::lowerConstantof does not know how to lower
113   /// addrspacecast, therefore they should be lowered by this function.
114   const MCExpr *lowerConstant(const Constant *CV, const Constant *BaseCV,
115                               uint64_t Offset) override;
116 
117   /// tblgen'erated driver function for lowering simple MI->MC pseudo
118   /// instructions.
119   bool lowerPseudoInstExpansion(const MachineInstr *MI, MCInst &Inst);
120 
121   /// Implemented in AMDGPUMCInstLower.cpp
122   void emitInstruction(const MachineInstr *MI) override;
123 
124   void emitFunctionBodyStart() override;
125 
126   void emitFunctionBodyEnd() override;
127 
128   void emitImplicitDef(const MachineInstr *MI) const override;
129 
130   void emitFunctionEntryLabel() override;
131 
132   void emitBasicBlockStart(const MachineBasicBlock &MBB) override;
133 
134   void emitGlobalVariable(const GlobalVariable *GV) override;
135 
136   void emitStartOfAsmFile(Module &M) override;
137 
138   void emitEndOfAsmFile(Module &M) override;
139 
140   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
141                        const char *ExtraCode, raw_ostream &O) override;
142 
143 protected:
144   void getAnalysisUsage(AnalysisUsage &AU) const override;
145 
146   std::vector<std::string> DisasmLines, HexLines;
147   size_t DisasmLineMaxLen;
148   bool IsTargetStreamerInitialized;
149 };
150 
151 } // end namespace llvm
152 
153 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
154