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 "AMDGPU.h" 18 #include "AMDKernelCodeT.h" 19 #include "AMDGPUHSAMetadataStreamer.h" 20 #include "SIProgramInfo.h" 21 #include "llvm/ADT/StringRef.h" 22 #include "llvm/CodeGen/AsmPrinter.h" 23 #include "llvm/Support/AMDHSAKernelDescriptor.h" 24 #include <cstddef> 25 #include <cstdint> 26 #include <limits> 27 #include <memory> 28 #include <string> 29 #include <vector> 30 31 namespace llvm { 32 33 class AMDGPUMachineFunction; 34 class AMDGPUTargetStreamer; 35 class MCCodeEmitter; 36 class MCOperand; 37 class GCNSubtarget; 38 39 class AMDGPUAsmPrinter final : public AsmPrinter { 40 private: 41 // Track resource usage for callee functions. 42 struct SIFunctionResourceInfo { 43 // Track the number of explicitly used VGPRs. Special registers reserved at 44 // the end are tracked separately. 45 int32_t NumVGPR = 0; 46 int32_t NumAGPR = 0; 47 int32_t NumExplicitSGPR = 0; 48 uint64_t PrivateSegmentSize = 0; 49 bool UsesVCC = false; 50 bool UsesFlatScratch = false; 51 bool HasDynamicallySizedStack = false; 52 bool HasRecursion = false; 53 54 int32_t getTotalNumSGPRs(const GCNSubtarget &ST) const; 55 int32_t getTotalNumVGPRs(const GCNSubtarget &ST) const; 56 }; 57 58 SIProgramInfo CurrentProgramInfo; 59 DenseMap<const Function *, SIFunctionResourceInfo> CallGraphResourceInfo; 60 61 std::unique_ptr<AMDGPU::HSAMD::MetadataStreamer> HSAMetadataStream; 62 63 MCCodeEmitter *DumpCodeInstEmitter = nullptr; 64 65 uint64_t getFunctionCodeSize(const MachineFunction &MF) const; 66 SIFunctionResourceInfo analyzeResourceUsage(const MachineFunction &MF) const; 67 68 void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF); 69 void getAmdKernelCode(amd_kernel_code_t &Out, const SIProgramInfo &KernelInfo, 70 const MachineFunction &MF) const; 71 void findNumUsedRegistersSI(const MachineFunction &MF, 72 unsigned &NumSGPR, 73 unsigned &NumVGPR) const; 74 75 /// Emit register usage information so that the GPU driver 76 /// can correctly setup the GPU state. 77 void EmitProgramInfoSI(const MachineFunction &MF, 78 const SIProgramInfo &KernelInfo); 79 void EmitPALMetadata(const MachineFunction &MF, 80 const SIProgramInfo &KernelInfo); 81 void emitPALFunctionMetadata(const MachineFunction &MF); 82 void emitCommonFunctionComments(uint32_t NumVGPR, 83 Optional<uint32_t> NumAGPR, 84 uint32_t TotalNumVGPR, 85 uint32_t NumSGPR, 86 uint64_t ScratchSize, 87 uint64_t CodeSize, 88 const AMDGPUMachineFunction* MFI); 89 90 uint16_t getAmdhsaKernelCodeProperties( 91 const MachineFunction &MF) const; 92 93 amdhsa::kernel_descriptor_t getAmdhsaKernelDescriptor( 94 const MachineFunction &MF, 95 const SIProgramInfo &PI) const; 96 97 public: 98 explicit AMDGPUAsmPrinter(TargetMachine &TM, 99 std::unique_ptr<MCStreamer> Streamer); 100 101 StringRef getPassName() const override; 102 103 const MCSubtargetInfo* getGlobalSTI() const; 104 105 AMDGPUTargetStreamer* getTargetStreamer() const; 106 107 bool doFinalization(Module &M) override; 108 bool runOnMachineFunction(MachineFunction &MF) override; 109 110 /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated 111 /// pseudo lowering. 112 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const; 113 114 /// Lower the specified LLVM Constant to an MCExpr. 115 /// The AsmPrinter::lowerConstantof does not know how to lower 116 /// addrspacecast, therefore they should be lowered by this function. 117 const MCExpr *lowerConstant(const Constant *CV) override; 118 119 /// tblgen'erated driver function for lowering simple MI->MC pseudo 120 /// instructions. 121 bool emitPseudoExpansionLowering(MCStreamer &OutStreamer, 122 const MachineInstr *MI); 123 124 /// Implemented in AMDGPUMCInstLower.cpp 125 void emitInstruction(const MachineInstr *MI) override; 126 127 void emitFunctionBodyStart() override; 128 129 void emitFunctionBodyEnd() override; 130 131 void emitFunctionEntryLabel() override; 132 133 void emitBasicBlockStart(const MachineBasicBlock &MBB) override; 134 135 void emitGlobalVariable(const GlobalVariable *GV) override; 136 137 void emitStartOfAsmFile(Module &M) override; 138 139 void emitEndOfAsmFile(Module &M) override; 140 141 bool isBlockOnlyReachableByFallthrough( 142 const MachineBasicBlock *MBB) const override; 143 144 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 145 const char *ExtraCode, raw_ostream &O) override; 146 147 protected: 148 std::vector<std::string> DisasmLines, HexLines; 149 size_t DisasmLineMaxLen; 150 }; 151 152 } // end namespace llvm 153 154 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H 155