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