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