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 MCOperand; 36 class GCNSubtarget; 37 38 class AMDGPUAsmPrinter final : public AsmPrinter { 39 private: 40 // Track resource usage for callee functions. 41 struct SIFunctionResourceInfo { 42 // Track the number of explicitly used VGPRs. Special registers reserved at 43 // the end are tracked separately. 44 int32_t NumVGPR = 0; 45 int32_t NumExplicitSGPR = 0; 46 uint64_t PrivateSegmentSize = 0; 47 bool UsesVCC = false; 48 bool UsesFlatScratch = false; 49 bool HasDynamicallySizedStack = false; 50 bool HasRecursion = false; 51 52 int32_t getTotalNumSGPRs(const GCNSubtarget &ST) const; 53 }; 54 55 SIProgramInfo CurrentProgramInfo; 56 DenseMap<const Function *, SIFunctionResourceInfo> CallGraphResourceInfo; 57 58 std::unique_ptr<AMDGPU::HSAMD::MetadataStreamer> HSAMetadataStream; 59 std::map<uint32_t, uint32_t> PALMetadataMap; 60 61 uint64_t getFunctionCodeSize(const MachineFunction &MF) const; 62 SIFunctionResourceInfo analyzeResourceUsage(const MachineFunction &MF) const; 63 64 void readPALMetadata(Module &M); 65 void getSIProgramInfo(SIProgramInfo &Out, const MachineFunction &MF); 66 void getAmdKernelCode(amd_kernel_code_t &Out, const SIProgramInfo &KernelInfo, 67 const MachineFunction &MF) const; 68 void findNumUsedRegistersSI(const MachineFunction &MF, 69 unsigned &NumSGPR, 70 unsigned &NumVGPR) const; 71 72 /// Emit register usage information so that the GPU driver 73 /// can correctly setup the GPU state. 74 void EmitProgramInfoSI(const MachineFunction &MF, 75 const SIProgramInfo &KernelInfo); 76 void EmitPALMetadata(const MachineFunction &MF, 77 const SIProgramInfo &KernelInfo); 78 void emitCommonFunctionComments(uint32_t NumVGPR, 79 uint32_t NumSGPR, 80 uint64_t ScratchSize, 81 uint64_t CodeSize, 82 const AMDGPUMachineFunction* MFI); 83 84 uint16_t getAmdhsaKernelCodeProperties( 85 const MachineFunction &MF) const; 86 87 amdhsa::kernel_descriptor_t getAmdhsaKernelDescriptor( 88 const MachineFunction &MF, 89 const SIProgramInfo &PI) const; 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 doFinalization(Module &M) override; 102 bool runOnMachineFunction(MachineFunction &MF) override; 103 104 /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated 105 /// pseudo lowering. 106 bool lowerOperand(const MachineOperand &MO, MCOperand &MCOp) const; 107 108 /// Lower the specified LLVM Constant to an MCExpr. 109 /// The AsmPrinter::lowerConstantof does not know how to lower 110 /// addrspacecast, therefore they should be lowered by this function. 111 const MCExpr *lowerConstant(const Constant *CV) override; 112 113 /// tblgen'erated driver function for lowering simple MI->MC pseudo 114 /// instructions. 115 bool emitPseudoExpansionLowering(MCStreamer &OutStreamer, 116 const MachineInstr *MI); 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 EmitFunctionEntryLabel() override; 126 127 void EmitBasicBlockStart(const MachineBasicBlock &MBB) const override; 128 129 void EmitGlobalVariable(const GlobalVariable *GV) override; 130 131 void EmitStartOfAsmFile(Module &M) override; 132 133 void EmitEndOfAsmFile(Module &M) override; 134 135 bool isBlockOnlyReachableByFallthrough( 136 const MachineBasicBlock *MBB) const override; 137 138 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 139 unsigned AsmVariant, const char *ExtraCode, 140 raw_ostream &O) override; 141 142 protected: 143 mutable std::vector<std::string> DisasmLines, HexLines; 144 mutable size_t DisasmLineMaxLen; 145 }; 146 147 } // end namespace llvm 148 149 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H 150