xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h (revision 5fb307d29b364982acbde82cbf77db3cae486f8c)
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"
18 #include "llvm/CodeGen/AsmPrinter.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   unsigned CodeObjectVersion;
43   void initializeTargetID(const Module &M);
44 
45   AMDGPUResourceUsageAnalysis *ResourceUsage;
46 
47   SIProgramInfo CurrentProgramInfo;
48 
49   std::unique_ptr<AMDGPU::HSAMD::MetadataStreamer> HSAMetadataStream;
50 
51   MCCodeEmitter *DumpCodeInstEmitter = nullptr;
52 
53   uint64_t getFunctionCodeSize(const MachineFunction &MF) const;
54 
55   void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF);
56   void getAmdKernelCode(amd_kernel_code_t &Out, const SIProgramInfo &KernelInfo,
57                         const MachineFunction &MF) const;
58 
59   /// Emit register usage information so that the GPU driver
60   /// can correctly setup the GPU state.
61   void EmitProgramInfoSI(const MachineFunction &MF,
62                          const SIProgramInfo &KernelInfo);
63   void EmitPALMetadata(const MachineFunction &MF,
64                        const SIProgramInfo &KernelInfo);
65   void emitPALFunctionMetadata(const MachineFunction &MF);
66   void emitCommonFunctionComments(uint32_t NumVGPR,
67                                   std::optional<uint32_t> NumAGPR,
68                                   uint32_t TotalNumVGPR, uint32_t NumSGPR,
69                                   uint64_t ScratchSize, uint64_t CodeSize,
70                                   const AMDGPUMachineFunction *MFI);
71   void emitResourceUsageRemarks(const MachineFunction &MF,
72                                 const SIProgramInfo &CurrentProgramInfo,
73                                 bool isModuleEntryFunction, bool hasMAIInsts);
74 
75   uint16_t getAmdhsaKernelCodeProperties(
76       const MachineFunction &MF) const;
77 
78   amdhsa::kernel_descriptor_t getAmdhsaKernelDescriptor(
79       const MachineFunction &MF,
80       const SIProgramInfo &PI) const;
81 
82   void initTargetStreamer(Module &M);
83 
84 public:
85   explicit AMDGPUAsmPrinter(TargetMachine &TM,
86                             std::unique_ptr<MCStreamer> Streamer);
87 
88   StringRef getPassName() const override;
89 
90   const MCSubtargetInfo* getGlobalSTI() const;
91 
92   AMDGPUTargetStreamer* getTargetStreamer() const;
93 
94   bool doInitialization(Module &M) override;
95   bool doFinalization(Module &M) override;
96   bool runOnMachineFunction(MachineFunction &MF) override;
97 
98   /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated
99   /// pseudo lowering.
100   bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
101 
102   /// Lower the specified LLVM Constant to an MCExpr.
103   /// The AsmPrinter::lowerConstantof does not know how to lower
104   /// addrspacecast, therefore they should be lowered by this function.
105   const MCExpr *lowerConstant(const Constant *CV) override;
106 
107   /// tblgen'erated driver function for lowering simple MI->MC pseudo
108   /// instructions.
109   bool emitPseudoExpansionLowering(MCStreamer &OutStreamer,
110                                    const MachineInstr *MI);
111 
112   /// Implemented in AMDGPUMCInstLower.cpp
113   void emitInstruction(const MachineInstr *MI) override;
114 
115   void emitFunctionBodyStart() override;
116 
117   void emitFunctionBodyEnd() override;
118 
119   void emitFunctionEntryLabel() override;
120 
121   void emitBasicBlockStart(const MachineBasicBlock &MBB) override;
122 
123   void emitGlobalVariable(const GlobalVariable *GV) override;
124 
125   void emitStartOfAsmFile(Module &M) override;
126 
127   void emitEndOfAsmFile(Module &M) override;
128 
129   bool isBlockOnlyReachableByFallthrough(
130     const MachineBasicBlock *MBB) const override;
131 
132   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
133                        const char *ExtraCode, raw_ostream &O) override;
134 
135 protected:
136   void getAnalysisUsage(AnalysisUsage &AU) const override;
137 
138   std::vector<std::string> DisasmLines, HexLines;
139   size_t DisasmLineMaxLen;
140   bool IsTargetStreamerInitialized;
141 };
142 
143 } // end namespace llvm
144 
145 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
146