1 //===-- AMDGPUAsmPrinter.h - Print AMDGPU assembly code ---------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// AMDGPU Assembly printer class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
15 #define LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H
17 #include "SIProgramInfo.h"
18 #include "llvm/CodeGen/AsmPrinter.h"
20 struct amd_kernel_code_t;
24 class AMDGPUMachineFunction;
25 struct AMDGPUResourceUsageAnalysis;
26 class AMDGPUTargetStreamer;
32 class MetadataStreamer;
37 struct kernel_descriptor_t;
40 class AMDGPUAsmPrinter final : public AsmPrinter {
42 void initializeTargetID(const Module &M);
44 AMDGPUResourceUsageAnalysis *ResourceUsage;
46 SIProgramInfo CurrentProgramInfo;
48 std::unique_ptr<AMDGPU::HSAMD::MetadataStreamer> HSAMetadataStream;
50 MCCodeEmitter *DumpCodeInstEmitter = nullptr;
52 uint64_t getFunctionCodeSize(const MachineFunction &MF) const;
54 void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF);
55 void getAmdKernelCode(amd_kernel_code_t &Out, const SIProgramInfo &KernelInfo,
56 const MachineFunction &MF) const;
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 std::optional<uint32_t> NumAGPR,
67 uint32_t TotalNumVGPR, uint32_t NumSGPR,
68 uint64_t ScratchSize, uint64_t CodeSize,
69 const AMDGPUMachineFunction *MFI);
70 void emitResourceUsageRemarks(const MachineFunction &MF,
71 const SIProgramInfo &CurrentProgramInfo,
72 bool isModuleEntryFunction, bool hasMAIInsts);
74 uint16_t getAmdhsaKernelCodeProperties(
75 const MachineFunction &MF) const;
77 amdhsa::kernel_descriptor_t getAmdhsaKernelDescriptor(
78 const MachineFunction &MF,
79 const SIProgramInfo &PI) const;
81 void initTargetStreamer(Module &M);
84 explicit AMDGPUAsmPrinter(TargetMachine &TM,
85 std::unique_ptr<MCStreamer> Streamer);
87 StringRef getPassName() const override;
89 const MCSubtargetInfo* getGlobalSTI() const;
91 AMDGPUTargetStreamer* getTargetStreamer() const;
93 bool doFinalization(Module &M) override;
94 bool runOnMachineFunction(MachineFunction &MF) override;
96 /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated
98 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const;
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;
105 /// tblgen'erated driver function for lowering simple MI->MC pseudo
107 bool emitPseudoExpansionLowering(MCStreamer &OutStreamer,
108 const MachineInstr *MI);
110 /// Implemented in AMDGPUMCInstLower.cpp
111 void emitInstruction(const MachineInstr *MI) override;
113 void emitFunctionBodyStart() override;
115 void emitFunctionBodyEnd() override;
117 void emitFunctionEntryLabel() override;
119 void emitBasicBlockStart(const MachineBasicBlock &MBB) override;
121 void emitGlobalVariable(const GlobalVariable *GV) override;
123 void emitStartOfAsmFile(Module &M) override;
125 void emitEndOfAsmFile(Module &M) override;
127 bool isBlockOnlyReachableByFallthrough(
128 const MachineBasicBlock *MBB) const override;
130 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
131 const char *ExtraCode, raw_ostream &O) override;
134 void getAnalysisUsage(AnalysisUsage &AU) const override;
136 std::vector<std::string> DisasmLines, HexLines;
137 size_t DisasmLineMaxLen;
138 bool IsTargetStreamerInitialized;
141 } // end namespace llvm
143 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H