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
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"
33 class AMDGPUMachineFunction
;
34 class AMDGPUTargetStreamer
;
39 class AMDGPUAsmPrinter final
: public AsmPrinter
{
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.
46 int32_t NumExplicitSGPR
= 0;
47 uint64_t PrivateSegmentSize
= 0;
49 bool UsesFlatScratch
= false;
50 bool HasDynamicallySizedStack
= false;
51 bool HasRecursion
= false;
53 int32_t getTotalNumSGPRs(const GCNSubtarget
&ST
) const;
56 SIProgramInfo CurrentProgramInfo
;
57 DenseMap
<const Function
*, SIFunctionResourceInfo
> CallGraphResourceInfo
;
59 std::unique_ptr
<AMDGPU::HSAMD::MetadataStreamer
> HSAMetadataStream
;
61 MCCodeEmitter
*DumpCodeInstEmitter
= nullptr;
63 uint64_t getFunctionCodeSize(const MachineFunction
&MF
) const;
64 SIFunctionResourceInfo
analyzeResourceUsage(const MachineFunction
&MF
) const;
66 void getSIProgramInfo(SIProgramInfo
&Out
, const MachineFunction
&MF
);
67 void getAmdKernelCode(amd_kernel_code_t
&Out
, const SIProgramInfo
&KernelInfo
,
68 const MachineFunction
&MF
) const;
69 void findNumUsedRegistersSI(const MachineFunction
&MF
,
71 unsigned &NumVGPR
) const;
73 /// Emit register usage information so that the GPU driver
74 /// can correctly setup the GPU state.
75 void EmitProgramInfoSI(const MachineFunction
&MF
,
76 const SIProgramInfo
&KernelInfo
);
77 void EmitPALMetadata(const MachineFunction
&MF
,
78 const SIProgramInfo
&KernelInfo
);
79 void emitCommonFunctionComments(uint32_t NumVGPR
,
83 const AMDGPUMachineFunction
* MFI
);
85 uint16_t getAmdhsaKernelCodeProperties(
86 const MachineFunction
&MF
) const;
88 amdhsa::kernel_descriptor_t
getAmdhsaKernelDescriptor(
89 const MachineFunction
&MF
,
90 const SIProgramInfo
&PI
) const;
93 explicit AMDGPUAsmPrinter(TargetMachine
&TM
,
94 std::unique_ptr
<MCStreamer
> Streamer
);
96 StringRef
getPassName() const override
;
98 const MCSubtargetInfo
* getGlobalSTI() const;
100 AMDGPUTargetStreamer
* getTargetStreamer() const;
102 bool doFinalization(Module
&M
) override
;
103 bool runOnMachineFunction(MachineFunction
&MF
) override
;
105 /// Wrapper for MCInstLowering.lowerOperand() for the tblgen'erated
107 bool lowerOperand(const MachineOperand
&MO
, MCOperand
&MCOp
) const;
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
;
114 /// tblgen'erated driver function for lowering simple MI->MC pseudo
116 bool emitPseudoExpansionLowering(MCStreamer
&OutStreamer
,
117 const MachineInstr
*MI
);
119 /// Implemented in AMDGPUMCInstLower.cpp
120 void EmitInstruction(const MachineInstr
*MI
) override
;
122 void EmitFunctionBodyStart() override
;
124 void EmitFunctionBodyEnd() override
;
126 void EmitFunctionEntryLabel() override
;
128 void EmitBasicBlockStart(const MachineBasicBlock
&MBB
) override
;
130 void EmitGlobalVariable(const GlobalVariable
*GV
) override
;
132 void EmitStartOfAsmFile(Module
&M
) override
;
134 void EmitEndOfAsmFile(Module
&M
) override
;
136 bool isBlockOnlyReachableByFallthrough(
137 const MachineBasicBlock
*MBB
) const override
;
139 bool PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
140 const char *ExtraCode
, raw_ostream
&O
) override
;
143 std::vector
<std::string
> DisasmLines
, HexLines
;
144 size_t DisasmLineMaxLen
;
147 } // end namespace llvm
149 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUASMPRINTER_H