Konstantin Zhuravlyov | 30f03b3 | 2018-06-27 05:36:03 +0000 | [diff] [blame] | 1 | //===-- AMDGPUAsmPrinter.cpp - AMDGPU assembly printer -------------------===// |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | /// \file |
| 10 | /// |
| 11 | /// The AMDGPUAsmPrinter is used to print both assembly string and also binary |
| 12 | /// code. When passed an MCAsmStreamer it prints assembly and when passed |
| 13 | /// an MCObjectStreamer it outputs binary code. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | // |
| 17 | |
| 18 | #include "AMDGPUAsmPrinter.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 19 | #include "AMDGPU.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 20 | #include "AMDGPUSubtarget.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 21 | #include "AMDGPUTargetMachine.h" |
Richard Trieu | c0bd7bd | 2019-05-11 00:03:35 +0000 | [diff] [blame] | 22 | #include "MCTargetDesc/AMDGPUInstPrinter.h" |
Tom Stellard | 44b30b4 | 2018-05-22 02:03:23 +0000 | [diff] [blame] | 23 | #include "MCTargetDesc/AMDGPUMCTargetDesc.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 24 | #include "MCTargetDesc/AMDGPUTargetStreamer.h" |
Tom Stellard | c501501 | 2018-05-24 20:02:01 +0000 | [diff] [blame] | 25 | #include "R600AsmPrinter.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 26 | #include "R600Defines.h" |
| 27 | #include "R600MachineFunctionInfo.h" |
| 28 | #include "R600RegisterInfo.h" |
| 29 | #include "SIDefines.h" |
Matt Arsenault | a9720c6 | 2016-06-20 17:51:32 +0000 | [diff] [blame] | 30 | #include "SIInstrInfo.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 31 | #include "SIMachineFunctionInfo.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 32 | #include "SIRegisterInfo.h" |
Richard Trieu | 8ce2ee9 | 2019-05-14 21:54:37 +0000 | [diff] [blame] | 33 | #include "TargetInfo/AMDGPUTargetInfo.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 34 | #include "Utils/AMDGPUBaseInfo.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 35 | #include "llvm/BinaryFormat/ELF.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Matt Arsenault | ff98241 | 2016-06-20 18:13:04 +0000 | [diff] [blame] | 37 | #include "llvm/IR/DiagnosticInfo.h" |
Tim Renouf | 33cb8f5 | 2019-05-14 16:17:14 +0000 | [diff] [blame] | 38 | #include "llvm/MC/MCAssembler.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 39 | #include "llvm/MC/MCContext.h" |
| 40 | #include "llvm/MC/MCSectionELF.h" |
| 41 | #include "llvm/MC/MCStreamer.h" |
Konstantin Zhuravlyov | c3beb6a | 2017-10-11 22:41:09 +0000 | [diff] [blame] | 42 | #include "llvm/Support/AMDGPUMetadata.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 43 | #include "llvm/Support/MathExtras.h" |
Konstantin Zhuravlyov | 71e43ee | 2018-09-12 18:50:47 +0000 | [diff] [blame] | 44 | #include "llvm/Support/TargetParser.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 45 | #include "llvm/Support/TargetRegistry.h" |
David Blaikie | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 46 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 47 | |
| 48 | using namespace llvm; |
Konstantin Zhuravlyov | c3beb6a | 2017-10-11 22:41:09 +0000 | [diff] [blame] | 49 | using namespace llvm::AMDGPU; |
Scott Linder | f5b36e5 | 2018-12-12 19:39:27 +0000 | [diff] [blame] | 50 | using namespace llvm::AMDGPU::HSAMD; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 51 | |
| 52 | // TODO: This should get the default rounding mode from the kernel. We just set |
| 53 | // the default here, but this could change if the OpenCL rounding mode pragmas |
| 54 | // are used. |
| 55 | // |
| 56 | // The denormal mode here should match what is reported by the OpenCL runtime |
| 57 | // for the CL_FP_DENORM bit from CL_DEVICE_{HALF|SINGLE|DOUBLE}_FP_CONFIG, but |
| 58 | // can also be override to flush with the -cl-denorms-are-zero compiler flag. |
| 59 | // |
| 60 | // AMD OpenCL only sets flush none and reports CL_FP_DENORM for double |
| 61 | // precision, and leaves single precision to flush all and does not report |
| 62 | // CL_FP_DENORM for CL_DEVICE_SINGLE_FP_CONFIG. Mesa's OpenCL currently reports |
| 63 | // CL_FP_DENORM for both. |
| 64 | // |
| 65 | // FIXME: It seems some instructions do not support single precision denormals |
| 66 | // regardless of the mode (exp_*_f32, rcp_*_f32, rsq_*_f32, rsq_*f32, sqrt_f32, |
| 67 | // and sin_f32, cos_f32 on most parts). |
| 68 | |
| 69 | // We want to use these instructions, and using fp32 denormals also causes |
| 70 | // instructions to run at the double precision rate for the device so it's |
| 71 | // probably best to just report no single precision denormals. |
| 72 | static uint32_t getFPMode(const MachineFunction &F) { |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 73 | const GCNSubtarget& ST = F.getSubtarget<GCNSubtarget>(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 74 | // TODO: Is there any real use for the flush in only / flush out only modes? |
| 75 | |
| 76 | uint32_t FP32Denormals = |
| 77 | ST.hasFP32Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT; |
| 78 | |
| 79 | uint32_t FP64Denormals = |
| 80 | ST.hasFP64Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT; |
| 81 | |
| 82 | return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) | |
| 83 | FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) | |
| 84 | FP_DENORM_MODE_SP(FP32Denormals) | |
| 85 | FP_DENORM_MODE_DP(FP64Denormals); |
| 86 | } |
| 87 | |
| 88 | static AsmPrinter * |
| 89 | createAMDGPUAsmPrinterPass(TargetMachine &tm, |
| 90 | std::unique_ptr<MCStreamer> &&Streamer) { |
| 91 | return new AMDGPUAsmPrinter(tm, std::move(Streamer)); |
| 92 | } |
| 93 | |
| 94 | extern "C" void LLVMInitializeAMDGPUAsmPrinter() { |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 95 | TargetRegistry::RegisterAsmPrinter(getTheAMDGPUTarget(), |
Tom Stellard | c501501 | 2018-05-24 20:02:01 +0000 | [diff] [blame] | 96 | llvm::createR600AsmPrinterPass); |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 97 | TargetRegistry::RegisterAsmPrinter(getTheGCNTarget(), |
| 98 | createAMDGPUAsmPrinterPass); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM, |
| 102 | std::unique_ptr<MCStreamer> Streamer) |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 103 | : AsmPrinter(TM, std::move(Streamer)) { |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 104 | if (IsaInfo::hasCodeObjectV3(getGlobalSTI())) |
Scott Linder | f5b36e5 | 2018-12-12 19:39:27 +0000 | [diff] [blame] | 105 | HSAMetadataStream.reset(new MetadataStreamerV3()); |
| 106 | else |
| 107 | HSAMetadataStream.reset(new MetadataStreamerV2()); |
Matt Arsenault | 0da6350 | 2018-08-31 05:49:54 +0000 | [diff] [blame] | 108 | } |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 109 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 110 | StringRef AMDGPUAsmPrinter::getPassName() const { |
Matt Arsenault | f9245b7 | 2016-07-22 17:01:25 +0000 | [diff] [blame] | 111 | return "AMDGPU Assembly Printer"; |
| 112 | } |
| 113 | |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 114 | const MCSubtargetInfo *AMDGPUAsmPrinter::getGlobalSTI() const { |
Konstantin Zhuravlyov | 7498cd6 | 2017-03-22 22:32:22 +0000 | [diff] [blame] | 115 | return TM.getMCSubtargetInfo(); |
| 116 | } |
| 117 | |
Konstantin Zhuravlyov | 8c18f5b | 2017-10-14 22:16:26 +0000 | [diff] [blame] | 118 | AMDGPUTargetStreamer* AMDGPUAsmPrinter::getTargetStreamer() const { |
| 119 | if (!OutStreamer) |
| 120 | return nullptr; |
| 121 | return static_cast<AMDGPUTargetStreamer*>(OutStreamer->getTargetStreamer()); |
Konstantin Zhuravlyov | 7498cd6 | 2017-03-22 22:32:22 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Tom Stellard | f421837 | 2016-01-12 17:18:17 +0000 | [diff] [blame] | 124 | void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) { |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 125 | if (IsaInfo::hasCodeObjectV3(getGlobalSTI())) { |
Konstantin Zhuravlyov | 94dfcc2e | 2018-10-15 20:37:47 +0000 | [diff] [blame] | 126 | std::string ExpectedTarget; |
| 127 | raw_string_ostream ExpectedTargetOS(ExpectedTarget); |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 128 | IsaInfo::streamIsaVersion(getGlobalSTI(), ExpectedTargetOS); |
Konstantin Zhuravlyov | 94dfcc2e | 2018-10-15 20:37:47 +0000 | [diff] [blame] | 129 | |
| 130 | getTargetStreamer()->EmitDirectiveAMDGCNTarget(ExpectedTarget); |
Konstantin Zhuravlyov | 94dfcc2e | 2018-10-15 20:37:47 +0000 | [diff] [blame] | 131 | } |
Konstantin Zhuravlyov | 00f2cb1 | 2018-06-12 18:02:46 +0000 | [diff] [blame] | 132 | |
Konstantin Zhuravlyov | eda425e | 2017-10-14 15:59:07 +0000 | [diff] [blame] | 133 | if (TM.getTargetTriple().getOS() != Triple::AMDHSA && |
| 134 | TM.getTargetTriple().getOS() != Triple::AMDPAL) |
| 135 | return; |
| 136 | |
| 137 | if (TM.getTargetTriple().getOS() == Triple::AMDHSA) |
Scott Linder | f5b36e5 | 2018-12-12 19:39:27 +0000 | [diff] [blame] | 138 | HSAMetadataStream->begin(M); |
Konstantin Zhuravlyov | eda425e | 2017-10-14 15:59:07 +0000 | [diff] [blame] | 139 | |
| 140 | if (TM.getTargetTriple().getOS() == Triple::AMDPAL) |
Tim Renouf | d737b55 | 2019-03-20 17:42:00 +0000 | [diff] [blame] | 141 | getTargetStreamer()->getPALMetadata()->readFromIR(M); |
Konstantin Zhuravlyov | eda425e | 2017-10-14 15:59:07 +0000 | [diff] [blame] | 142 | |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 143 | if (IsaInfo::hasCodeObjectV3(getGlobalSTI())) |
Scott Linder | f5b36e5 | 2018-12-12 19:39:27 +0000 | [diff] [blame] | 144 | return; |
| 145 | |
Konstantin Zhuravlyov | eda425e | 2017-10-14 15:59:07 +0000 | [diff] [blame] | 146 | // HSA emits NT_AMDGPU_HSA_CODE_OBJECT_VERSION for code objects v2. |
| 147 | if (TM.getTargetTriple().getOS() == Triple::AMDHSA) |
Konstantin Zhuravlyov | 8c18f5b | 2017-10-14 22:16:26 +0000 | [diff] [blame] | 148 | getTargetStreamer()->EmitDirectiveHSACodeObjectVersion(2, 1); |
Konstantin Zhuravlyov | eda425e | 2017-10-14 15:59:07 +0000 | [diff] [blame] | 149 | |
| 150 | // HSA and PAL emit NT_AMDGPU_HSA_ISA for code objects v2. |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 151 | IsaVersion Version = getIsaVersion(getGlobalSTI()->getCPU()); |
Konstantin Zhuravlyov | 8c18f5b | 2017-10-14 22:16:26 +0000 | [diff] [blame] | 152 | getTargetStreamer()->EmitDirectiveHSACodeObjectISA( |
Konstantin Zhuravlyov | 71e43ee | 2018-09-12 18:50:47 +0000 | [diff] [blame] | 153 | Version.Major, Version.Minor, Version.Stepping, "AMD", "AMDGPU"); |
Konstantin Zhuravlyov | 7498cd6 | 2017-03-22 22:32:22 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void AMDGPUAsmPrinter::EmitEndOfAsmFile(Module &M) { |
Konstantin Zhuravlyov | 8c18f5b | 2017-10-14 22:16:26 +0000 | [diff] [blame] | 157 | // Following code requires TargetStreamer to be present. |
| 158 | if (!getTargetStreamer()) |
| 159 | return; |
| 160 | |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 161 | if (!IsaInfo::hasCodeObjectV3(getGlobalSTI())) { |
Scott Linder | f5b36e5 | 2018-12-12 19:39:27 +0000 | [diff] [blame] | 162 | // Emit ISA Version (NT_AMD_AMDGPU_ISA). |
| 163 | std::string ISAVersionString; |
| 164 | raw_string_ostream ISAVersionStream(ISAVersionString); |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 165 | IsaInfo::streamIsaVersion(getGlobalSTI(), ISAVersionStream); |
Scott Linder | f5b36e5 | 2018-12-12 19:39:27 +0000 | [diff] [blame] | 166 | getTargetStreamer()->EmitISAVersion(ISAVersionStream.str()); |
| 167 | } |
Konstantin Zhuravlyov | 9c05b2b | 2017-10-14 15:40:33 +0000 | [diff] [blame] | 168 | |
| 169 | // Emit HSA Metadata (NT_AMD_AMDGPU_HSA_METADATA). |
| 170 | if (TM.getTargetTriple().getOS() == Triple::AMDHSA) { |
Scott Linder | f5b36e5 | 2018-12-12 19:39:27 +0000 | [diff] [blame] | 171 | HSAMetadataStream->end(); |
| 172 | bool Success = HSAMetadataStream->emitTo(*getTargetStreamer()); |
| 173 | (void)Success; |
| 174 | assert(Success && "Malformed HSA Metadata"); |
Konstantin Zhuravlyov | 9c05b2b | 2017-10-14 15:40:33 +0000 | [diff] [blame] | 175 | } |
Tom Stellard | f421837 | 2016-01-12 17:18:17 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Matt Arsenault | 6bc43d8 | 2016-10-06 16:20:41 +0000 | [diff] [blame] | 178 | bool AMDGPUAsmPrinter::isBlockOnlyReachableByFallthrough( |
| 179 | const MachineBasicBlock *MBB) const { |
| 180 | if (!AsmPrinter::isBlockOnlyReachableByFallthrough(MBB)) |
| 181 | return false; |
| 182 | |
| 183 | if (MBB->empty()) |
| 184 | return true; |
| 185 | |
| 186 | // If this is a block implementing a long branch, an expression relative to |
| 187 | // the start of the block is needed. to the start of the block. |
| 188 | // XXX - Is there a smarter way to check this? |
| 189 | return (MBB->back().getOpcode() != AMDGPU::S_SETPC_B64); |
| 190 | } |
| 191 | |
Tom Stellard | f151a45 | 2015-06-26 21:14:58 +0000 | [diff] [blame] | 192 | void AMDGPUAsmPrinter::EmitFunctionBodyStart() { |
Konstantin Zhuravlyov | 00f2cb1 | 2018-06-12 18:02:46 +0000 | [diff] [blame] | 193 | const SIMachineFunctionInfo &MFI = *MF->getInfo<SIMachineFunctionInfo>(); |
| 194 | if (!MFI.isEntryFunction()) |
| 195 | return; |
Matt Arsenault | 021a218 | 2017-04-19 19:38:10 +0000 | [diff] [blame] | 196 | |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 197 | const GCNSubtarget &STM = MF->getSubtarget<GCNSubtarget>(); |
Matt Arsenault | 4bec7d4 | 2018-07-20 09:05:08 +0000 | [diff] [blame] | 198 | const Function &F = MF->getFunction(); |
Konstantin Zhuravlyov | aa067cb | 2018-10-04 21:02:16 +0000 | [diff] [blame] | 199 | if (!STM.hasCodeObjectV3() && STM.isAmdHsaOrMesa(F) && |
Matt Arsenault | 4bec7d4 | 2018-07-20 09:05:08 +0000 | [diff] [blame] | 200 | (F.getCallingConv() == CallingConv::AMDGPU_KERNEL || |
| 201 | F.getCallingConv() == CallingConv::SPIR_KERNEL)) { |
| 202 | amd_kernel_code_t KernelCode; |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 203 | getAmdKernelCode(KernelCode, CurrentProgramInfo, *MF); |
Konstantin Zhuravlyov | 8c18f5b | 2017-10-14 22:16:26 +0000 | [diff] [blame] | 204 | getTargetStreamer()->EmitAMDKernelCodeT(KernelCode); |
Tom Stellard | f151a45 | 2015-06-26 21:14:58 +0000 | [diff] [blame] | 205 | } |
Konstantin Zhuravlyov | 7498cd6 | 2017-03-22 22:32:22 +0000 | [diff] [blame] | 206 | |
Scott Linder | f5b36e5 | 2018-12-12 19:39:27 +0000 | [diff] [blame] | 207 | if (STM.isAmdHsaOS()) |
| 208 | HSAMetadataStream->emitKernel(*MF, CurrentProgramInfo); |
Tim Renouf | 33cb8f5 | 2019-05-14 16:17:14 +0000 | [diff] [blame] | 209 | |
| 210 | DumpCodeInstEmitter = nullptr; |
| 211 | if (STM.dumpCode()) { |
| 212 | // For -dumpcode, get the assembler out of the streamer, even if it does |
| 213 | // not really want to let us have it. This only works with -filetype=obj. |
| 214 | bool SaveFlag = OutStreamer->getUseAssemblerInfoForParsing(); |
| 215 | OutStreamer->setUseAssemblerInfoForParsing(true); |
| 216 | MCAssembler *Assembler = OutStreamer->getAssemblerPtr(); |
| 217 | OutStreamer->setUseAssemblerInfoForParsing(SaveFlag); |
| 218 | if (Assembler) |
| 219 | DumpCodeInstEmitter = Assembler->getEmitterPtr(); |
| 220 | } |
Tom Stellard | f151a45 | 2015-06-26 21:14:58 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Konstantin Zhuravlyov | 00f2cb1 | 2018-06-12 18:02:46 +0000 | [diff] [blame] | 223 | void AMDGPUAsmPrinter::EmitFunctionBodyEnd() { |
| 224 | const SIMachineFunctionInfo &MFI = *MF->getInfo<SIMachineFunctionInfo>(); |
| 225 | if (!MFI.isEntryFunction()) |
| 226 | return; |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 227 | |
| 228 | if (!IsaInfo::hasCodeObjectV3(getGlobalSTI()) || |
Konstantin Zhuravlyov | 00f2cb1 | 2018-06-12 18:02:46 +0000 | [diff] [blame] | 229 | TM.getTargetTriple().getOS() != Triple::AMDHSA) |
| 230 | return; |
| 231 | |
Konstantin Zhuravlyov | ce25bc3 | 2018-06-12 18:33:51 +0000 | [diff] [blame] | 232 | auto &Streamer = getTargetStreamer()->getStreamer(); |
| 233 | auto &Context = Streamer.getContext(); |
| 234 | auto &ObjectFileInfo = *Context.getObjectFileInfo(); |
| 235 | auto &ReadOnlySection = *ObjectFileInfo.getReadOnlySection(); |
| 236 | |
| 237 | Streamer.PushSection(); |
| 238 | Streamer.SwitchSection(&ReadOnlySection); |
| 239 | |
| 240 | // CP microcode requires the kernel descriptor to be allocated on 64 byte |
| 241 | // alignment. |
| 242 | Streamer.EmitValueToAlignment(64, 0, 1, 0); |
| 243 | if (ReadOnlySection.getAlignment() < 64) |
| 244 | ReadOnlySection.setAlignment(64); |
| 245 | |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 246 | const MCSubtargetInfo &STI = MF->getSubtarget(); |
| 247 | |
Konstantin Zhuravlyov | 00f2cb1 | 2018-06-12 18:02:46 +0000 | [diff] [blame] | 248 | SmallString<128> KernelName; |
| 249 | getNameWithPrefix(KernelName, &MF->getFunction()); |
| 250 | getTargetStreamer()->EmitAmdhsaKernelDescriptor( |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 251 | STI, KernelName, getAmdhsaKernelDescriptor(*MF, CurrentProgramInfo), |
Scott Linder | 1e8c2c7 | 2018-06-21 19:38:56 +0000 | [diff] [blame] | 252 | CurrentProgramInfo.NumVGPRsForWavesPerEU, |
| 253 | CurrentProgramInfo.NumSGPRsForWavesPerEU - |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 254 | IsaInfo::getNumExtraSGPRs(&STI, |
Scott Linder | 1e8c2c7 | 2018-06-21 19:38:56 +0000 | [diff] [blame] | 255 | CurrentProgramInfo.VCCUsed, |
| 256 | CurrentProgramInfo.FlatUsed), |
| 257 | CurrentProgramInfo.VCCUsed, CurrentProgramInfo.FlatUsed, |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 258 | hasXNACK(STI)); |
Konstantin Zhuravlyov | ce25bc3 | 2018-06-12 18:33:51 +0000 | [diff] [blame] | 259 | |
| 260 | Streamer.PopSection(); |
Konstantin Zhuravlyov | 00f2cb1 | 2018-06-12 18:02:46 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Tom Stellard | 1e1b05d | 2015-11-06 11:45:14 +0000 | [diff] [blame] | 263 | void AMDGPUAsmPrinter::EmitFunctionEntryLabel() { |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 264 | if (IsaInfo::hasCodeObjectV3(getGlobalSTI()) && |
Konstantin Zhuravlyov | 00f2cb1 | 2018-06-12 18:02:46 +0000 | [diff] [blame] | 265 | TM.getTargetTriple().getOS() == Triple::AMDHSA) { |
| 266 | AsmPrinter::EmitFunctionEntryLabel(); |
| 267 | return; |
| 268 | } |
| 269 | |
Tom Stellard | 1e1b05d | 2015-11-06 11:45:14 +0000 | [diff] [blame] | 270 | const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 271 | const GCNSubtarget &STM = MF->getSubtarget<GCNSubtarget>(); |
Konstantin Zhuravlyov | aa067cb | 2018-10-04 21:02:16 +0000 | [diff] [blame] | 272 | if (MFI->isEntryFunction() && STM.isAmdHsaOrMesa(MF->getFunction())) { |
Tom Stellard | 1b9748c | 2016-09-26 17:29:25 +0000 | [diff] [blame] | 273 | SmallString<128> SymbolName; |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 274 | getNameWithPrefix(SymbolName, &MF->getFunction()), |
Konstantin Zhuravlyov | 8c18f5b | 2017-10-14 22:16:26 +0000 | [diff] [blame] | 275 | getTargetStreamer()->EmitAMDGPUSymbolType( |
Konstantin Zhuravlyov | 7498cd6 | 2017-03-22 22:32:22 +0000 | [diff] [blame] | 276 | SymbolName, ELF::STT_AMDGPU_HSA_KERNEL); |
Tom Stellard | 1e1b05d | 2015-11-06 11:45:14 +0000 | [diff] [blame] | 277 | } |
Tim Renouf | 33cb8f5 | 2019-05-14 16:17:14 +0000 | [diff] [blame] | 278 | if (DumpCodeInstEmitter) { |
Tim Renouf | cead41d | 2017-12-08 14:09:34 +0000 | [diff] [blame] | 279 | // Disassemble function name label to text. |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 280 | DisasmLines.push_back(MF->getName().str() + ":"); |
Tim Renouf | cead41d | 2017-12-08 14:09:34 +0000 | [diff] [blame] | 281 | DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLines.back().size()); |
| 282 | HexLines.push_back(""); |
| 283 | } |
Tom Stellard | 1e1b05d | 2015-11-06 11:45:14 +0000 | [diff] [blame] | 284 | |
| 285 | AsmPrinter::EmitFunctionEntryLabel(); |
| 286 | } |
| 287 | |
Tim Renouf | cead41d | 2017-12-08 14:09:34 +0000 | [diff] [blame] | 288 | void AMDGPUAsmPrinter::EmitBasicBlockStart(const MachineBasicBlock &MBB) const { |
Tim Renouf | 33cb8f5 | 2019-05-14 16:17:14 +0000 | [diff] [blame] | 289 | if (DumpCodeInstEmitter && !isBlockOnlyReachableByFallthrough(&MBB)) { |
Tim Renouf | cead41d | 2017-12-08 14:09:34 +0000 | [diff] [blame] | 290 | // Write a line for the basic block label if it is not only fallthrough. |
| 291 | DisasmLines.push_back( |
| 292 | (Twine("BB") + Twine(getFunctionNumber()) |
| 293 | + "_" + Twine(MBB.getNumber()) + ":").str()); |
| 294 | DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLines.back().size()); |
| 295 | HexLines.push_back(""); |
| 296 | } |
| 297 | AsmPrinter::EmitBasicBlockStart(MBB); |
| 298 | } |
| 299 | |
Tom Stellard | e3b5aea | 2015-12-02 17:00:42 +0000 | [diff] [blame] | 300 | void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) { |
| 301 | |
Tom Stellard | 00f2f91 | 2015-12-02 19:47:57 +0000 | [diff] [blame] | 302 | // Group segment variables aren't emitted in HSA. |
Konstantin Zhuravlyov | 435151a | 2017-11-01 19:12:38 +0000 | [diff] [blame] | 303 | if (AMDGPU::isGroupSegment(GV)) |
Tom Stellard | 00f2f91 | 2015-12-02 19:47:57 +0000 | [diff] [blame] | 304 | return; |
| 305 | |
Tom Stellard | fcfaea4 | 2016-05-05 17:03:33 +0000 | [diff] [blame] | 306 | AsmPrinter::EmitGlobalVariable(GV); |
Tom Stellard | e3b5aea | 2015-12-02 17:00:42 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 309 | bool AMDGPUAsmPrinter::doFinalization(Module &M) { |
| 310 | CallGraphResourceInfo.clear(); |
Stanislav Mekhanoshin | 41bbe10 | 2019-05-03 21:26:39 +0000 | [diff] [blame] | 311 | |
| 312 | if (AMDGPU::isGFX10(*getGlobalSTI())) { |
| 313 | OutStreamer->SwitchSection(getObjFileLowering().getTextSection()); |
| 314 | getTargetStreamer()->EmitCodeEnd(); |
| 315 | } |
| 316 | |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 317 | return AsmPrinter::doFinalization(M); |
| 318 | } |
| 319 | |
| 320 | // Print comments that apply to both callable functions and entry points. |
| 321 | void AMDGPUAsmPrinter::emitCommonFunctionComments( |
| 322 | uint32_t NumVGPR, |
| 323 | uint32_t NumSGPR, |
Matt Arsenault | 9ba465a | 2017-11-14 20:33:14 +0000 | [diff] [blame] | 324 | uint64_t ScratchSize, |
Stanislav Mekhanoshin | 1c53842 | 2018-05-25 17:25:12 +0000 | [diff] [blame] | 325 | uint64_t CodeSize, |
| 326 | const AMDGPUMachineFunction *MFI) { |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 327 | OutStreamer->emitRawComment(" codeLenInByte = " + Twine(CodeSize), false); |
| 328 | OutStreamer->emitRawComment(" NumSgprs: " + Twine(NumSGPR), false); |
| 329 | OutStreamer->emitRawComment(" NumVgprs: " + Twine(NumVGPR), false); |
| 330 | OutStreamer->emitRawComment(" ScratchSize: " + Twine(ScratchSize), false); |
Stanislav Mekhanoshin | 1c53842 | 2018-05-25 17:25:12 +0000 | [diff] [blame] | 331 | OutStreamer->emitRawComment(" MemoryBound: " + Twine(MFI->isMemoryBound()), |
| 332 | false); |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Konstantin Zhuravlyov | 00f2cb1 | 2018-06-12 18:02:46 +0000 | [diff] [blame] | 335 | uint16_t AMDGPUAsmPrinter::getAmdhsaKernelCodeProperties( |
| 336 | const MachineFunction &MF) const { |
| 337 | const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>(); |
| 338 | uint16_t KernelCodeProperties = 0; |
| 339 | |
| 340 | if (MFI.hasPrivateSegmentBuffer()) { |
| 341 | KernelCodeProperties |= |
| 342 | amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER; |
| 343 | } |
| 344 | if (MFI.hasDispatchPtr()) { |
| 345 | KernelCodeProperties |= |
| 346 | amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR; |
| 347 | } |
| 348 | if (MFI.hasQueuePtr()) { |
| 349 | KernelCodeProperties |= |
| 350 | amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR; |
| 351 | } |
| 352 | if (MFI.hasKernargSegmentPtr()) { |
| 353 | KernelCodeProperties |= |
| 354 | amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR; |
| 355 | } |
| 356 | if (MFI.hasDispatchID()) { |
| 357 | KernelCodeProperties |= |
| 358 | amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID; |
| 359 | } |
| 360 | if (MFI.hasFlatScratchInit()) { |
| 361 | KernelCodeProperties |= |
| 362 | amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT; |
| 363 | } |
Konstantin Zhuravlyov | 00f2cb1 | 2018-06-12 18:02:46 +0000 | [diff] [blame] | 364 | |
| 365 | return KernelCodeProperties; |
| 366 | } |
| 367 | |
| 368 | amdhsa::kernel_descriptor_t AMDGPUAsmPrinter::getAmdhsaKernelDescriptor( |
| 369 | const MachineFunction &MF, |
| 370 | const SIProgramInfo &PI) const { |
| 371 | amdhsa::kernel_descriptor_t KernelDescriptor; |
| 372 | memset(&KernelDescriptor, 0x0, sizeof(KernelDescriptor)); |
| 373 | |
| 374 | assert(isUInt<32>(PI.ScratchSize)); |
| 375 | assert(isUInt<32>(PI.ComputePGMRSrc1)); |
| 376 | assert(isUInt<32>(PI.ComputePGMRSrc2)); |
| 377 | |
| 378 | KernelDescriptor.group_segment_fixed_size = PI.LDSSize; |
| 379 | KernelDescriptor.private_segment_fixed_size = PI.ScratchSize; |
| 380 | KernelDescriptor.compute_pgm_rsrc1 = PI.ComputePGMRSrc1; |
| 381 | KernelDescriptor.compute_pgm_rsrc2 = PI.ComputePGMRSrc2; |
| 382 | KernelDescriptor.kernel_code_properties = getAmdhsaKernelCodeProperties(MF); |
| 383 | |
| 384 | return KernelDescriptor; |
| 385 | } |
| 386 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 387 | bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 388 | CurrentProgramInfo = SIProgramInfo(); |
| 389 | |
Matt Arsenault | 6cb7b8a | 2017-04-19 17:42:39 +0000 | [diff] [blame] | 390 | const AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 391 | |
| 392 | // The starting address of all shader programs must be 256 bytes aligned. |
Matt Arsenault | 6cb7b8a | 2017-04-19 17:42:39 +0000 | [diff] [blame] | 393 | // Regular functions just need the basic required instruction alignment. |
| 394 | MF.setAlignment(MFI->isEntryFunction() ? 8 : 2); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 395 | |
| 396 | SetupMachineFunction(MF); |
| 397 | |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 398 | const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>(); |
Konstantin Zhuravlyov | 67a6d54 | 2017-01-06 17:02:10 +0000 | [diff] [blame] | 399 | MCContext &Context = getObjFileLowering().getContext(); |
Tim Renouf | 807ecc3 | 2018-02-06 13:39:38 +0000 | [diff] [blame] | 400 | // FIXME: This should be an explicit check for Mesa. |
| 401 | if (!STM.isAmdHsaOS() && !STM.isAmdPalOS()) { |
Konstantin Zhuravlyov | 67a6d54 | 2017-01-06 17:02:10 +0000 | [diff] [blame] | 402 | MCSectionELF *ConfigSection = |
| 403 | Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0); |
| 404 | OutStreamer->SwitchSection(ConfigSection); |
| 405 | } |
| 406 | |
Tom Stellard | c501501 | 2018-05-24 20:02:01 +0000 | [diff] [blame] | 407 | if (MFI->isEntryFunction()) { |
| 408 | getSIProgramInfo(CurrentProgramInfo, MF); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 409 | } else { |
Tom Stellard | c501501 | 2018-05-24 20:02:01 +0000 | [diff] [blame] | 410 | auto I = CallGraphResourceInfo.insert( |
| 411 | std::make_pair(&MF.getFunction(), SIFunctionResourceInfo())); |
| 412 | SIFunctionResourceInfo &Info = I.first->second; |
| 413 | assert(I.second && "should only be called once per function"); |
| 414 | Info = analyzeResourceUsage(MF); |
| 415 | } |
| 416 | |
| 417 | if (STM.isAmdPalOS()) |
| 418 | EmitPALMetadata(MF, CurrentProgramInfo); |
| 419 | else if (!STM.isAmdHsaOS()) { |
| 420 | EmitProgramInfoSI(MF, CurrentProgramInfo); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | DisasmLines.clear(); |
| 424 | HexLines.clear(); |
| 425 | DisasmLineMaxLen = 0; |
| 426 | |
| 427 | EmitFunctionBody(); |
| 428 | |
| 429 | if (isVerbose()) { |
| 430 | MCSectionELF *CommentSection = |
| 431 | Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0); |
| 432 | OutStreamer->SwitchSection(CommentSection); |
| 433 | |
Tom Stellard | c501501 | 2018-05-24 20:02:01 +0000 | [diff] [blame] | 434 | if (!MFI->isEntryFunction()) { |
| 435 | OutStreamer->emitRawComment(" Function info:", false); |
| 436 | SIFunctionResourceInfo &Info = CallGraphResourceInfo[&MF.getFunction()]; |
| 437 | emitCommonFunctionComments( |
| 438 | Info.NumVGPR, |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 439 | Info.getTotalNumSGPRs(MF.getSubtarget<GCNSubtarget>()), |
Tom Stellard | c501501 | 2018-05-24 20:02:01 +0000 | [diff] [blame] | 440 | Info.PrivateSegmentSize, |
Stanislav Mekhanoshin | 1c53842 | 2018-05-25 17:25:12 +0000 | [diff] [blame] | 441 | getFunctionCodeSize(MF), MFI); |
Tom Stellard | c501501 | 2018-05-24 20:02:01 +0000 | [diff] [blame] | 442 | return false; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 443 | } |
Tom Stellard | c501501 | 2018-05-24 20:02:01 +0000 | [diff] [blame] | 444 | |
| 445 | OutStreamer->emitRawComment(" Kernel info:", false); |
| 446 | emitCommonFunctionComments(CurrentProgramInfo.NumVGPR, |
| 447 | CurrentProgramInfo.NumSGPR, |
| 448 | CurrentProgramInfo.ScratchSize, |
Stanislav Mekhanoshin | 1c53842 | 2018-05-25 17:25:12 +0000 | [diff] [blame] | 449 | getFunctionCodeSize(MF), MFI); |
Tom Stellard | c501501 | 2018-05-24 20:02:01 +0000 | [diff] [blame] | 450 | |
| 451 | OutStreamer->emitRawComment( |
| 452 | " FloatMode: " + Twine(CurrentProgramInfo.FloatMode), false); |
| 453 | OutStreamer->emitRawComment( |
| 454 | " IeeeMode: " + Twine(CurrentProgramInfo.IEEEMode), false); |
| 455 | OutStreamer->emitRawComment( |
| 456 | " LDSByteSize: " + Twine(CurrentProgramInfo.LDSSize) + |
| 457 | " bytes/workgroup (compile time only)", false); |
| 458 | |
| 459 | OutStreamer->emitRawComment( |
| 460 | " SGPRBlocks: " + Twine(CurrentProgramInfo.SGPRBlocks), false); |
| 461 | OutStreamer->emitRawComment( |
| 462 | " VGPRBlocks: " + Twine(CurrentProgramInfo.VGPRBlocks), false); |
| 463 | |
| 464 | OutStreamer->emitRawComment( |
| 465 | " NumSGPRsForWavesPerEU: " + |
| 466 | Twine(CurrentProgramInfo.NumSGPRsForWavesPerEU), false); |
| 467 | OutStreamer->emitRawComment( |
| 468 | " NumVGPRsForWavesPerEU: " + |
| 469 | Twine(CurrentProgramInfo.NumVGPRsForWavesPerEU), false); |
| 470 | |
| 471 | OutStreamer->emitRawComment( |
Stanislav Mekhanoshin | 1c53842 | 2018-05-25 17:25:12 +0000 | [diff] [blame] | 472 | " WaveLimiterHint : " + Twine(MFI->needsWaveLimiter()), false); |
| 473 | |
Tom Stellard | c501501 | 2018-05-24 20:02:01 +0000 | [diff] [blame] | 474 | OutStreamer->emitRawComment( |
| 475 | " COMPUTE_PGM_RSRC2:USER_SGPR: " + |
| 476 | Twine(G_00B84C_USER_SGPR(CurrentProgramInfo.ComputePGMRSrc2)), false); |
| 477 | OutStreamer->emitRawComment( |
| 478 | " COMPUTE_PGM_RSRC2:TRAP_HANDLER: " + |
| 479 | Twine(G_00B84C_TRAP_HANDLER(CurrentProgramInfo.ComputePGMRSrc2)), false); |
| 480 | OutStreamer->emitRawComment( |
| 481 | " COMPUTE_PGM_RSRC2:TGID_X_EN: " + |
| 482 | Twine(G_00B84C_TGID_X_EN(CurrentProgramInfo.ComputePGMRSrc2)), false); |
| 483 | OutStreamer->emitRawComment( |
| 484 | " COMPUTE_PGM_RSRC2:TGID_Y_EN: " + |
| 485 | Twine(G_00B84C_TGID_Y_EN(CurrentProgramInfo.ComputePGMRSrc2)), false); |
| 486 | OutStreamer->emitRawComment( |
| 487 | " COMPUTE_PGM_RSRC2:TGID_Z_EN: " + |
| 488 | Twine(G_00B84C_TGID_Z_EN(CurrentProgramInfo.ComputePGMRSrc2)), false); |
| 489 | OutStreamer->emitRawComment( |
| 490 | " COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " + |
| 491 | Twine(G_00B84C_TIDIG_COMP_CNT(CurrentProgramInfo.ComputePGMRSrc2)), |
| 492 | false); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Tim Renouf | 33cb8f5 | 2019-05-14 16:17:14 +0000 | [diff] [blame] | 495 | if (DumpCodeInstEmitter) { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 496 | |
| 497 | OutStreamer->SwitchSection( |
| 498 | Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0)); |
| 499 | |
| 500 | for (size_t i = 0; i < DisasmLines.size(); ++i) { |
Tim Renouf | cead41d | 2017-12-08 14:09:34 +0000 | [diff] [blame] | 501 | std::string Comment = "\n"; |
| 502 | if (!HexLines[i].empty()) { |
| 503 | Comment = std::string(DisasmLineMaxLen - DisasmLines[i].size(), ' '); |
| 504 | Comment += " ; " + HexLines[i] + "\n"; |
| 505 | } |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 506 | |
| 507 | OutStreamer->EmitBytes(StringRef(DisasmLines[i])); |
| 508 | OutStreamer->EmitBytes(StringRef(Comment)); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | return false; |
| 513 | } |
| 514 | |
Matt Arsenault | a3566f2 | 2017-04-17 19:48:30 +0000 | [diff] [blame] | 515 | uint64_t AMDGPUAsmPrinter::getFunctionCodeSize(const MachineFunction &MF) const { |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 516 | const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>(); |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 517 | const SIInstrInfo *TII = STM.getInstrInfo(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 518 | |
Matt Arsenault | a3566f2 | 2017-04-17 19:48:30 +0000 | [diff] [blame] | 519 | uint64_t CodeSize = 0; |
| 520 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 521 | for (const MachineBasicBlock &MBB : MF) { |
| 522 | for (const MachineInstr &MI : MBB) { |
| 523 | // TODO: CodeSize should account for multiple functions. |
Matt Arsenault | c574686 | 2015-08-12 09:04:44 +0000 | [diff] [blame] | 524 | |
| 525 | // TODO: Should we count size of debug info? |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 526 | if (MI.isDebugInstr()) |
Matt Arsenault | c574686 | 2015-08-12 09:04:44 +0000 | [diff] [blame] | 527 | continue; |
| 528 | |
Matt Arsenault | a3566f2 | 2017-04-17 19:48:30 +0000 | [diff] [blame] | 529 | CodeSize += TII->getInstSizeInBytes(MI); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Matt Arsenault | a3566f2 | 2017-04-17 19:48:30 +0000 | [diff] [blame] | 533 | return CodeSize; |
| 534 | } |
| 535 | |
| 536 | static bool hasAnyNonFlatUseOfReg(const MachineRegisterInfo &MRI, |
| 537 | const SIInstrInfo &TII, |
| 538 | unsigned Reg) { |
| 539 | for (const MachineOperand &UseOp : MRI.reg_operands(Reg)) { |
| 540 | if (!UseOp.isImplicit() || !TII.isFLAT(*UseOp.getParent())) |
| 541 | return true; |
| 542 | } |
| 543 | |
| 544 | return false; |
| 545 | } |
| 546 | |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 547 | int32_t AMDGPUAsmPrinter::SIFunctionResourceInfo::getTotalNumSGPRs( |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 548 | const GCNSubtarget &ST) const { |
Konstantin Zhuravlyov | 71e43ee | 2018-09-12 18:50:47 +0000 | [diff] [blame] | 549 | return NumExplicitSGPR + IsaInfo::getNumExtraSGPRs(&ST, |
Scott Linder | 1e8c2c7 | 2018-06-21 19:38:56 +0000 | [diff] [blame] | 550 | UsesVCC, UsesFlatScratch); |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | AMDGPUAsmPrinter::SIFunctionResourceInfo AMDGPUAsmPrinter::analyzeResourceUsage( |
| 554 | const MachineFunction &MF) const { |
| 555 | SIFunctionResourceInfo Info; |
| 556 | |
| 557 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 558 | const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 559 | const MachineFrameInfo &FrameInfo = MF.getFrameInfo(); |
| 560 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 561 | const SIInstrInfo *TII = ST.getInstrInfo(); |
| 562 | const SIRegisterInfo &TRI = TII->getRegisterInfo(); |
| 563 | |
| 564 | Info.UsesFlatScratch = MRI.isPhysRegUsed(AMDGPU::FLAT_SCR_LO) || |
| 565 | MRI.isPhysRegUsed(AMDGPU::FLAT_SCR_HI); |
| 566 | |
| 567 | // Even if FLAT_SCRATCH is implicitly used, it has no effect if flat |
| 568 | // instructions aren't used to access the scratch buffer. Inline assembly may |
| 569 | // need it though. |
| 570 | // |
| 571 | // If we only have implicit uses of flat_scr on flat instructions, it is not |
| 572 | // really needed. |
| 573 | if (Info.UsesFlatScratch && !MFI->hasFlatScratchInit() && |
| 574 | (!hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR) && |
| 575 | !hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR_LO) && |
| 576 | !hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR_HI))) { |
| 577 | Info.UsesFlatScratch = false; |
| 578 | } |
| 579 | |
| 580 | Info.HasDynamicallySizedStack = FrameInfo.hasVarSizedObjects(); |
| 581 | Info.PrivateSegmentSize = FrameInfo.getStackSize(); |
Matt Arsenault | 03ae399 | 2018-03-29 21:30:06 +0000 | [diff] [blame] | 582 | if (MFI->isStackRealigned()) |
| 583 | Info.PrivateSegmentSize += FrameInfo.getMaxAlignment(); |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 584 | |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 585 | |
Matt Arsenault | 3416b8c | 2017-06-01 15:05:15 +0000 | [diff] [blame] | 586 | Info.UsesVCC = MRI.isPhysRegUsed(AMDGPU::VCC_LO) || |
| 587 | MRI.isPhysRegUsed(AMDGPU::VCC_HI); |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 588 | |
Matt Arsenault | 3416b8c | 2017-06-01 15:05:15 +0000 | [diff] [blame] | 589 | // If there are no calls, MachineRegisterInfo can tell us the used register |
| 590 | // count easily. |
Matt Arsenault | 22cdb61 | 2017-09-05 18:36:36 +0000 | [diff] [blame] | 591 | // A tail call isn't considered a call for MachineFrameInfo's purposes. |
| 592 | if (!FrameInfo.hasCalls() && !FrameInfo.hasTailCall()) { |
Matt Arsenault | 2738ede | 2017-08-02 17:15:01 +0000 | [diff] [blame] | 593 | MCPhysReg HighestVGPRReg = AMDGPU::NoRegister; |
| 594 | for (MCPhysReg Reg : reverse(AMDGPU::VGPR_32RegClass.getRegisters())) { |
| 595 | if (MRI.isPhysRegUsed(Reg)) { |
| 596 | HighestVGPRReg = Reg; |
| 597 | break; |
| 598 | } |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 599 | } |
Matt Arsenault | 2738ede | 2017-08-02 17:15:01 +0000 | [diff] [blame] | 600 | |
| 601 | MCPhysReg HighestSGPRReg = AMDGPU::NoRegister; |
| 602 | for (MCPhysReg Reg : reverse(AMDGPU::SGPR_32RegClass.getRegisters())) { |
| 603 | if (MRI.isPhysRegUsed(Reg)) { |
| 604 | HighestSGPRReg = Reg; |
| 605 | break; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | // We found the maximum register index. They start at 0, so add one to get the |
| 610 | // number of registers. |
| 611 | Info.NumVGPR = HighestVGPRReg == AMDGPU::NoRegister ? 0 : |
| 612 | TRI.getHWRegIndex(HighestVGPRReg) + 1; |
| 613 | Info.NumExplicitSGPR = HighestSGPRReg == AMDGPU::NoRegister ? 0 : |
| 614 | TRI.getHWRegIndex(HighestSGPRReg) + 1; |
| 615 | |
| 616 | return Info; |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 619 | int32_t MaxVGPR = -1; |
| 620 | int32_t MaxSGPR = -1; |
Matt Arsenault | 9ba465a | 2017-11-14 20:33:14 +0000 | [diff] [blame] | 621 | uint64_t CalleeFrameSize = 0; |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 622 | |
| 623 | for (const MachineBasicBlock &MBB : MF) { |
| 624 | for (const MachineInstr &MI : MBB) { |
| 625 | // TODO: Check regmasks? Do they occur anywhere except calls? |
| 626 | for (const MachineOperand &MO : MI.operands()) { |
| 627 | unsigned Width = 0; |
| 628 | bool IsSGPR = false; |
| 629 | |
| 630 | if (!MO.isReg()) |
| 631 | continue; |
| 632 | |
| 633 | unsigned Reg = MO.getReg(); |
| 634 | switch (Reg) { |
| 635 | case AMDGPU::EXEC: |
| 636 | case AMDGPU::EXEC_LO: |
| 637 | case AMDGPU::EXEC_HI: |
| 638 | case AMDGPU::SCC: |
| 639 | case AMDGPU::M0: |
| 640 | case AMDGPU::SRC_SHARED_BASE: |
| 641 | case AMDGPU::SRC_SHARED_LIMIT: |
| 642 | case AMDGPU::SRC_PRIVATE_BASE: |
| 643 | case AMDGPU::SRC_PRIVATE_LIMIT: |
Stanislav Mekhanoshin | 33d806a | 2019-04-24 17:28:30 +0000 | [diff] [blame] | 644 | case AMDGPU::SGPR_NULL: |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 645 | continue; |
| 646 | |
Dmitry Preobrazhensky | 137976f | 2019-03-20 15:40:52 +0000 | [diff] [blame] | 647 | case AMDGPU::SRC_POPS_EXITING_WAVE_ID: |
| 648 | llvm_unreachable("src_pops_exiting_wave_id should not be used"); |
| 649 | |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 650 | case AMDGPU::NoRegister: |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 651 | assert(MI.isDebugInstr()); |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 652 | continue; |
| 653 | |
| 654 | case AMDGPU::VCC: |
| 655 | case AMDGPU::VCC_LO: |
| 656 | case AMDGPU::VCC_HI: |
| 657 | Info.UsesVCC = true; |
| 658 | continue; |
| 659 | |
| 660 | case AMDGPU::FLAT_SCR: |
| 661 | case AMDGPU::FLAT_SCR_LO: |
| 662 | case AMDGPU::FLAT_SCR_HI: |
| 663 | continue; |
| 664 | |
Dmitry Preobrazhensky | 3afbd82 | 2018-01-10 14:22:19 +0000 | [diff] [blame] | 665 | case AMDGPU::XNACK_MASK: |
| 666 | case AMDGPU::XNACK_MASK_LO: |
| 667 | case AMDGPU::XNACK_MASK_HI: |
| 668 | llvm_unreachable("xnack_mask registers should not be used"); |
| 669 | |
Dmitry Preobrazhensky | 942c273 | 2019-02-08 14:57:37 +0000 | [diff] [blame] | 670 | case AMDGPU::LDS_DIRECT: |
| 671 | llvm_unreachable("lds_direct register should not be used"); |
| 672 | |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 673 | case AMDGPU::TBA: |
| 674 | case AMDGPU::TBA_LO: |
| 675 | case AMDGPU::TBA_HI: |
| 676 | case AMDGPU::TMA: |
| 677 | case AMDGPU::TMA_LO: |
| 678 | case AMDGPU::TMA_HI: |
| 679 | llvm_unreachable("trap handler registers should not be used"); |
| 680 | |
Dmitry Preobrazhensky | 9111f35 | 2019-06-03 13:51:24 +0000 | [diff] [blame^] | 681 | case AMDGPU::SRC_VCCZ: |
| 682 | llvm_unreachable("src_vccz register should not be used"); |
| 683 | |
| 684 | case AMDGPU::SRC_EXECZ: |
| 685 | llvm_unreachable("src_execz register should not be used"); |
| 686 | |
| 687 | case AMDGPU::SRC_SCC: |
| 688 | llvm_unreachable("src_scc register should not be used"); |
| 689 | |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 690 | default: |
| 691 | break; |
| 692 | } |
| 693 | |
| 694 | if (AMDGPU::SReg_32RegClass.contains(Reg)) { |
| 695 | assert(!AMDGPU::TTMP_32RegClass.contains(Reg) && |
| 696 | "trap handler registers should not be used"); |
| 697 | IsSGPR = true; |
| 698 | Width = 1; |
| 699 | } else if (AMDGPU::VGPR_32RegClass.contains(Reg)) { |
| 700 | IsSGPR = false; |
| 701 | Width = 1; |
| 702 | } else if (AMDGPU::SReg_64RegClass.contains(Reg)) { |
| 703 | assert(!AMDGPU::TTMP_64RegClass.contains(Reg) && |
| 704 | "trap handler registers should not be used"); |
| 705 | IsSGPR = true; |
| 706 | Width = 2; |
| 707 | } else if (AMDGPU::VReg_64RegClass.contains(Reg)) { |
| 708 | IsSGPR = false; |
| 709 | Width = 2; |
| 710 | } else if (AMDGPU::VReg_96RegClass.contains(Reg)) { |
| 711 | IsSGPR = false; |
| 712 | Width = 3; |
| 713 | } else if (AMDGPU::SReg_128RegClass.contains(Reg)) { |
Dmitry Preobrazhensky | 2713495 | 2017-12-22 15:18:06 +0000 | [diff] [blame] | 714 | assert(!AMDGPU::TTMP_128RegClass.contains(Reg) && |
| 715 | "trap handler registers should not be used"); |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 716 | IsSGPR = true; |
| 717 | Width = 4; |
| 718 | } else if (AMDGPU::VReg_128RegClass.contains(Reg)) { |
| 719 | IsSGPR = false; |
| 720 | Width = 4; |
| 721 | } else if (AMDGPU::SReg_256RegClass.contains(Reg)) { |
Dmitry Preobrazhensky | 2713495 | 2017-12-22 15:18:06 +0000 | [diff] [blame] | 722 | assert(!AMDGPU::TTMP_256RegClass.contains(Reg) && |
| 723 | "trap handler registers should not be used"); |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 724 | IsSGPR = true; |
| 725 | Width = 8; |
| 726 | } else if (AMDGPU::VReg_256RegClass.contains(Reg)) { |
| 727 | IsSGPR = false; |
| 728 | Width = 8; |
| 729 | } else if (AMDGPU::SReg_512RegClass.contains(Reg)) { |
Dmitry Preobrazhensky | 2713495 | 2017-12-22 15:18:06 +0000 | [diff] [blame] | 730 | assert(!AMDGPU::TTMP_512RegClass.contains(Reg) && |
| 731 | "trap handler registers should not be used"); |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 732 | IsSGPR = true; |
| 733 | Width = 16; |
| 734 | } else if (AMDGPU::VReg_512RegClass.contains(Reg)) { |
| 735 | IsSGPR = false; |
| 736 | Width = 16; |
Matt Arsenault | 101abd2 | 2019-04-15 20:51:12 +0000 | [diff] [blame] | 737 | } else if (AMDGPU::SReg_96RegClass.contains(Reg)) { |
| 738 | IsSGPR = true; |
| 739 | Width = 3; |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 740 | } else { |
| 741 | llvm_unreachable("Unknown register class"); |
| 742 | } |
| 743 | unsigned HWReg = TRI.getHWRegIndex(Reg); |
| 744 | int MaxUsed = HWReg + Width - 1; |
| 745 | if (IsSGPR) { |
| 746 | MaxSGPR = MaxUsed > MaxSGPR ? MaxUsed : MaxSGPR; |
| 747 | } else { |
| 748 | MaxVGPR = MaxUsed > MaxVGPR ? MaxUsed : MaxVGPR; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | if (MI.isCall()) { |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 753 | // Pseudo used just to encode the underlying global. Is there a better |
| 754 | // way to track this? |
Matt Arsenault | 71bcbd4 | 2017-08-11 20:42:08 +0000 | [diff] [blame] | 755 | |
| 756 | const MachineOperand *CalleeOp |
| 757 | = TII->getNamedOperand(MI, AMDGPU::OpName::callee); |
| 758 | const Function *Callee = cast<Function>(CalleeOp->getGlobal()); |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 759 | if (Callee->isDeclaration()) { |
| 760 | // If this is a call to an external function, we can't do much. Make |
| 761 | // conservative guesses. |
| 762 | |
| 763 | // 48 SGPRs - vcc, - flat_scr, -xnack |
Scott Linder | 1e8c2c7 | 2018-06-21 19:38:56 +0000 | [diff] [blame] | 764 | int MaxSGPRGuess = |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 765 | 47 - IsaInfo::getNumExtraSGPRs(&ST, true, ST.hasFlatAddressSpace()); |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 766 | MaxSGPR = std::max(MaxSGPR, MaxSGPRGuess); |
| 767 | MaxVGPR = std::max(MaxVGPR, 23); |
| 768 | |
Matt Arsenault | 9ba465a | 2017-11-14 20:33:14 +0000 | [diff] [blame] | 769 | CalleeFrameSize = std::max(CalleeFrameSize, UINT64_C(16384)); |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 770 | Info.UsesVCC = true; |
| 771 | Info.UsesFlatScratch = ST.hasFlatAddressSpace(); |
| 772 | Info.HasDynamicallySizedStack = true; |
| 773 | } else { |
| 774 | // We force CodeGen to run in SCC order, so the callee's register |
| 775 | // usage etc. should be the cumulative usage of all callees. |
Matt Arsenault | aa03bcd | 2019-02-28 00:28:44 +0000 | [diff] [blame] | 776 | |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 777 | auto I = CallGraphResourceInfo.find(Callee); |
Matt Arsenault | aa03bcd | 2019-02-28 00:28:44 +0000 | [diff] [blame] | 778 | if (I == CallGraphResourceInfo.end()) { |
| 779 | // Avoid crashing on undefined behavior with an illegal call to a |
| 780 | // kernel. If a callsite's calling convention doesn't match the |
| 781 | // function's, it's undefined behavior. If the callsite calling |
| 782 | // convention does match, that would have errored earlier. |
| 783 | // FIXME: The verifier shouldn't allow this. |
| 784 | if (AMDGPU::isEntryFunctionCC(Callee->getCallingConv())) |
| 785 | report_fatal_error("invalid call to entry function"); |
| 786 | |
| 787 | llvm_unreachable("callee should have been handled before caller"); |
| 788 | } |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 789 | |
| 790 | MaxSGPR = std::max(I->second.NumExplicitSGPR - 1, MaxSGPR); |
| 791 | MaxVGPR = std::max(I->second.NumVGPR - 1, MaxVGPR); |
| 792 | CalleeFrameSize |
| 793 | = std::max(I->second.PrivateSegmentSize, CalleeFrameSize); |
| 794 | Info.UsesVCC |= I->second.UsesVCC; |
| 795 | Info.UsesFlatScratch |= I->second.UsesFlatScratch; |
| 796 | Info.HasDynamicallySizedStack |= I->second.HasDynamicallySizedStack; |
| 797 | Info.HasRecursion |= I->second.HasRecursion; |
| 798 | } |
| 799 | |
| 800 | if (!Callee->doesNotRecurse()) |
| 801 | Info.HasRecursion = true; |
| 802 | } |
Matt Arsenault | 3416b8c | 2017-06-01 15:05:15 +0000 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
Matt Arsenault | 6ed7b9b | 2017-08-02 01:31:28 +0000 | [diff] [blame] | 806 | Info.NumExplicitSGPR = MaxSGPR + 1; |
| 807 | Info.NumVGPR = MaxVGPR + 1; |
| 808 | Info.PrivateSegmentSize += CalleeFrameSize; |
Matt Arsenault | 3416b8c | 2017-06-01 15:05:15 +0000 | [diff] [blame] | 809 | |
| 810 | return Info; |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo, |
| 814 | const MachineFunction &MF) { |
| 815 | SIFunctionResourceInfo Info = analyzeResourceUsage(MF); |
| 816 | |
| 817 | ProgInfo.NumVGPR = Info.NumVGPR; |
| 818 | ProgInfo.NumSGPR = Info.NumExplicitSGPR; |
| 819 | ProgInfo.ScratchSize = Info.PrivateSegmentSize; |
| 820 | ProgInfo.VCCUsed = Info.UsesVCC; |
| 821 | ProgInfo.FlatUsed = Info.UsesFlatScratch; |
| 822 | ProgInfo.DynamicCallStack = Info.HasDynamicallySizedStack || Info.HasRecursion; |
| 823 | |
Matt Arsenault | 9ba465a | 2017-11-14 20:33:14 +0000 | [diff] [blame] | 824 | if (!isUInt<32>(ProgInfo.ScratchSize)) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 825 | DiagnosticInfoStackSize DiagStackSize(MF.getFunction(), |
Matt Arsenault | 9ba465a | 2017-11-14 20:33:14 +0000 | [diff] [blame] | 826 | ProgInfo.ScratchSize, DS_Error); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 827 | MF.getFunction().getContext().diagnose(DiagStackSize); |
Matt Arsenault | 9ba465a | 2017-11-14 20:33:14 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 830 | const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>(); |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 831 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 832 | |
Scott Linder | 1e8c2c7 | 2018-06-21 19:38:56 +0000 | [diff] [blame] | 833 | // TODO(scott.linder): The calculations related to SGPR/VGPR blocks are |
| 834 | // duplicated in part in AMDGPUAsmParser::calculateGPRBlocks, and could be |
| 835 | // unified. |
| 836 | unsigned ExtraSGPRs = IsaInfo::getNumExtraSGPRs( |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 837 | &STM, ProgInfo.VCCUsed, ProgInfo.FlatUsed); |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 838 | |
Marek Olsak | 91f22fb | 2016-12-09 19:49:40 +0000 | [diff] [blame] | 839 | // Check the addressable register limit before we add ExtraSGPRs. |
| 840 | if (STM.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS && |
| 841 | !STM.hasSGPRInitBug()) { |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 842 | unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs(); |
Matt Arsenault | a3566f2 | 2017-04-17 19:48:30 +0000 | [diff] [blame] | 843 | if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) { |
Marek Olsak | 91f22fb | 2016-12-09 19:49:40 +0000 | [diff] [blame] | 844 | // This can happen due to a compiler bug or when using inline asm. |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 845 | LLVMContext &Ctx = MF.getFunction().getContext(); |
| 846 | DiagnosticInfoResourceLimit Diag(MF.getFunction(), |
Marek Olsak | 91f22fb | 2016-12-09 19:49:40 +0000 | [diff] [blame] | 847 | "addressable scalar registers", |
Matt Arsenault | a3566f2 | 2017-04-17 19:48:30 +0000 | [diff] [blame] | 848 | ProgInfo.NumSGPR, DS_Error, |
Konstantin Zhuravlyov | 9f89ede | 2017-02-08 14:05:23 +0000 | [diff] [blame] | 849 | DK_ResourceLimit, |
| 850 | MaxAddressableNumSGPRs); |
Marek Olsak | 91f22fb | 2016-12-09 19:49:40 +0000 | [diff] [blame] | 851 | Ctx.diagnose(Diag); |
Matt Arsenault | a3566f2 | 2017-04-17 19:48:30 +0000 | [diff] [blame] | 852 | ProgInfo.NumSGPR = MaxAddressableNumSGPRs - 1; |
Marek Olsak | 91f22fb | 2016-12-09 19:49:40 +0000 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
Konstantin Zhuravlyov | 1d65026 | 2016-09-06 20:22:28 +0000 | [diff] [blame] | 856 | // Account for extra SGPRs and VGPRs reserved for debugger use. |
Matt Arsenault | a3566f2 | 2017-04-17 19:48:30 +0000 | [diff] [blame] | 857 | ProgInfo.NumSGPR += ExtraSGPRs; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 858 | |
Tim Renouf | fd8d4af | 2018-04-11 17:18:36 +0000 | [diff] [blame] | 859 | // Ensure there are enough SGPRs and VGPRs for wave dispatch, where wave |
| 860 | // dispatch registers are function args. |
| 861 | unsigned WaveDispatchNumSGPR = 0, WaveDispatchNumVGPR = 0; |
| 862 | for (auto &Arg : MF.getFunction().args()) { |
| 863 | unsigned NumRegs = (Arg.getType()->getPrimitiveSizeInBits() + 31) / 32; |
| 864 | if (Arg.hasAttribute(Attribute::InReg)) |
| 865 | WaveDispatchNumSGPR += NumRegs; |
| 866 | else |
| 867 | WaveDispatchNumVGPR += NumRegs; |
| 868 | } |
| 869 | ProgInfo.NumSGPR = std::max(ProgInfo.NumSGPR, WaveDispatchNumSGPR); |
| 870 | ProgInfo.NumVGPR = std::max(ProgInfo.NumVGPR, WaveDispatchNumVGPR); |
| 871 | |
Konstantin Zhuravlyov | 1d65026 | 2016-09-06 20:22:28 +0000 | [diff] [blame] | 872 | // Adjust number of registers used to meet default/requested minimum/maximum |
| 873 | // number of waves per execution unit request. |
| 874 | ProgInfo.NumSGPRsForWavesPerEU = std::max( |
Matt Arsenault | a3566f2 | 2017-04-17 19:48:30 +0000 | [diff] [blame] | 875 | std::max(ProgInfo.NumSGPR, 1u), STM.getMinNumSGPRs(MFI->getMaxWavesPerEU())); |
Konstantin Zhuravlyov | 1d65026 | 2016-09-06 20:22:28 +0000 | [diff] [blame] | 876 | ProgInfo.NumVGPRsForWavesPerEU = std::max( |
Matt Arsenault | a3566f2 | 2017-04-17 19:48:30 +0000 | [diff] [blame] | 877 | std::max(ProgInfo.NumVGPR, 1u), STM.getMinNumVGPRs(MFI->getMaxWavesPerEU())); |
Konstantin Zhuravlyov | 1d65026 | 2016-09-06 20:22:28 +0000 | [diff] [blame] | 878 | |
Marek Olsak | 91f22fb | 2016-12-09 19:49:40 +0000 | [diff] [blame] | 879 | if (STM.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS || |
| 880 | STM.hasSGPRInitBug()) { |
Konstantin Zhuravlyov | 9f89ede | 2017-02-08 14:05:23 +0000 | [diff] [blame] | 881 | unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs(); |
| 882 | if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) { |
| 883 | // This can happen due to a compiler bug or when using inline asm to use |
| 884 | // the registers which are usually reserved for vcc etc. |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 885 | LLVMContext &Ctx = MF.getFunction().getContext(); |
| 886 | DiagnosticInfoResourceLimit Diag(MF.getFunction(), |
Marek Olsak | 91f22fb | 2016-12-09 19:49:40 +0000 | [diff] [blame] | 887 | "scalar registers", |
| 888 | ProgInfo.NumSGPR, DS_Error, |
Konstantin Zhuravlyov | 9f89ede | 2017-02-08 14:05:23 +0000 | [diff] [blame] | 889 | DK_ResourceLimit, |
| 890 | MaxAddressableNumSGPRs); |
Marek Olsak | 91f22fb | 2016-12-09 19:49:40 +0000 | [diff] [blame] | 891 | Ctx.diagnose(Diag); |
Konstantin Zhuravlyov | 9f89ede | 2017-02-08 14:05:23 +0000 | [diff] [blame] | 892 | ProgInfo.NumSGPR = MaxAddressableNumSGPRs; |
| 893 | ProgInfo.NumSGPRsForWavesPerEU = MaxAddressableNumSGPRs; |
Marek Olsak | 91f22fb | 2016-12-09 19:49:40 +0000 | [diff] [blame] | 894 | } |
Matt Arsenault | 4eae301 | 2016-10-28 20:31:47 +0000 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | if (STM.hasSGPRInitBug()) { |
Konstantin Zhuravlyov | 9f89ede | 2017-02-08 14:05:23 +0000 | [diff] [blame] | 898 | ProgInfo.NumSGPR = |
| 899 | AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG; |
| 900 | ProgInfo.NumSGPRsForWavesPerEU = |
| 901 | AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Matt Arsenault | 161e2b4 | 2017-04-18 20:59:40 +0000 | [diff] [blame] | 904 | if (MFI->getNumUserSGPRs() > STM.getMaxNumUserSGPRs()) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 905 | LLVMContext &Ctx = MF.getFunction().getContext(); |
| 906 | DiagnosticInfoResourceLimit Diag(MF.getFunction(), "user SGPRs", |
Matt Arsenault | 161e2b4 | 2017-04-18 20:59:40 +0000 | [diff] [blame] | 907 | MFI->getNumUserSGPRs(), DS_Error); |
Matt Arsenault | ff98241 | 2016-06-20 18:13:04 +0000 | [diff] [blame] | 908 | Ctx.diagnose(Diag); |
Matt Arsenault | 41003af | 2015-11-30 21:16:07 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Matt Arsenault | 52ef401 | 2016-07-26 16:45:58 +0000 | [diff] [blame] | 911 | if (MFI->getLDSSize() > static_cast<unsigned>(STM.getLocalMemorySize())) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 912 | LLVMContext &Ctx = MF.getFunction().getContext(); |
| 913 | DiagnosticInfoResourceLimit Diag(MF.getFunction(), "local memory", |
Matt Arsenault | 52ef401 | 2016-07-26 16:45:58 +0000 | [diff] [blame] | 914 | MFI->getLDSSize(), DS_Error); |
Matt Arsenault | ff98241 | 2016-06-20 18:13:04 +0000 | [diff] [blame] | 915 | Ctx.diagnose(Diag); |
Matt Arsenault | 1c4d0ef | 2016-04-28 19:37:35 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Scott Linder | 1e8c2c7 | 2018-06-21 19:38:56 +0000 | [diff] [blame] | 918 | ProgInfo.SGPRBlocks = IsaInfo::getNumSGPRBlocks( |
David Stuttard | be3d7ba | 2018-11-19 15:44:20 +0000 | [diff] [blame] | 919 | &STM, ProgInfo.NumSGPRsForWavesPerEU); |
Scott Linder | 1e8c2c7 | 2018-06-21 19:38:56 +0000 | [diff] [blame] | 920 | ProgInfo.VGPRBlocks = IsaInfo::getNumVGPRBlocks( |
David Stuttard | be3d7ba | 2018-11-19 15:44:20 +0000 | [diff] [blame] | 921 | &STM, ProgInfo.NumVGPRsForWavesPerEU); |
Konstantin Zhuravlyov | e03b1d7 | 2017-02-08 13:02:33 +0000 | [diff] [blame] | 922 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 923 | // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode |
| 924 | // register. |
| 925 | ProgInfo.FloatMode = getFPMode(MF); |
| 926 | |
Matt Arsenault | 055e4dc | 2019-03-29 19:14:54 +0000 | [diff] [blame] | 927 | const SIModeRegisterDefaults Mode = MFI->getMode(); |
| 928 | ProgInfo.IEEEMode = Mode.IEEE; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 929 | |
Matt Arsenault | 7293f98 | 2016-01-28 20:53:35 +0000 | [diff] [blame] | 930 | // Make clamp modifier on NaN input returns 0. |
Matt Arsenault | 055e4dc | 2019-03-29 19:14:54 +0000 | [diff] [blame] | 931 | ProgInfo.DX10Clamp = Mode.DX10Clamp; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 932 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 933 | unsigned LDSAlignShift; |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 934 | if (STM.getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 935 | // LDS is allocated in 64 dword blocks. |
| 936 | LDSAlignShift = 8; |
| 937 | } else { |
| 938 | // LDS is allocated in 128 dword blocks. |
| 939 | LDSAlignShift = 9; |
| 940 | } |
| 941 | |
Konstantin Zhuravlyov | 1d65026 | 2016-09-06 20:22:28 +0000 | [diff] [blame] | 942 | unsigned LDSSpillSize = |
Matt Arsenault | 161e2b4 | 2017-04-18 20:59:40 +0000 | [diff] [blame] | 943 | MFI->getLDSWaveSpillSize() * MFI->getMaxFlatWorkGroupSize(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 944 | |
Matt Arsenault | 52ef401 | 2016-07-26 16:45:58 +0000 | [diff] [blame] | 945 | ProgInfo.LDSSize = MFI->getLDSSize() + LDSSpillSize; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 946 | ProgInfo.LDSBlocks = |
Aaron Ballman | ef0fe1e | 2016-03-30 21:30:00 +0000 | [diff] [blame] | 947 | alignTo(ProgInfo.LDSSize, 1ULL << LDSAlignShift) >> LDSAlignShift; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 948 | |
| 949 | // Scratch is allocated in 256 dword blocks. |
| 950 | unsigned ScratchAlignShift = 10; |
| 951 | // We need to program the hardware with the amount of scratch memory that |
| 952 | // is used by the entire wave. ProgInfo.ScratchSize is the amount of |
| 953 | // scratch memory used per thread. |
| 954 | ProgInfo.ScratchBlocks = |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 955 | alignTo(ProgInfo.ScratchSize * STM.getWavefrontSize(), |
Aaron Ballman | ef0fe1e | 2016-03-30 21:30:00 +0000 | [diff] [blame] | 956 | 1ULL << ScratchAlignShift) >> |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 957 | ScratchAlignShift; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 958 | |
Stanislav Mekhanoshin | 41bbe10 | 2019-05-03 21:26:39 +0000 | [diff] [blame] | 959 | if (getIsaVersion(getGlobalSTI()->getCPU()).Major >= 10) { |
| 960 | ProgInfo.WgpMode = STM.isCuModeEnabled() ? 0 : 1; |
| 961 | ProgInfo.MemOrdered = 1; |
| 962 | } |
| 963 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 964 | ProgInfo.ComputePGMRSrc1 = |
| 965 | S_00B848_VGPRS(ProgInfo.VGPRBlocks) | |
| 966 | S_00B848_SGPRS(ProgInfo.SGPRBlocks) | |
| 967 | S_00B848_PRIORITY(ProgInfo.Priority) | |
| 968 | S_00B848_FLOAT_MODE(ProgInfo.FloatMode) | |
| 969 | S_00B848_PRIV(ProgInfo.Priv) | |
| 970 | S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 971 | S_00B848_DEBUG_MODE(ProgInfo.DebugMode) | |
Stanislav Mekhanoshin | 41bbe10 | 2019-05-03 21:26:39 +0000 | [diff] [blame] | 972 | S_00B848_IEEE_MODE(ProgInfo.IEEEMode) | |
| 973 | S_00B848_WGP_MODE(ProgInfo.WgpMode) | |
| 974 | S_00B848_MEM_ORDERED(ProgInfo.MemOrdered); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 975 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 976 | // 0 = X, 1 = XY, 2 = XYZ |
| 977 | unsigned TIDIGCompCnt = 0; |
| 978 | if (MFI->hasWorkItemIDZ()) |
| 979 | TIDIGCompCnt = 2; |
| 980 | else if (MFI->hasWorkItemIDY()) |
| 981 | TIDIGCompCnt = 1; |
| 982 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 983 | ProgInfo.ComputePGMRSrc2 = |
| 984 | S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 985 | S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) | |
Konstantin Zhuravlyov | 2ca6b1f | 2018-05-29 19:09:13 +0000 | [diff] [blame] | 986 | // For AMDHSA, TRAP_HANDLER must be zero, as it is populated by the CP. |
| 987 | S_00B84C_TRAP_HANDLER(STM.isAmdHsaOS() ? 0 : STM.isTrapHandlerEnabled()) | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 988 | S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) | |
| 989 | S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) | |
| 990 | S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) | |
| 991 | S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) | |
| 992 | S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) | |
| 993 | S_00B84C_EXCP_EN_MSB(0) | |
Konstantin Zhuravlyov | 6ccb076 | 2017-05-05 20:13:55 +0000 | [diff] [blame] | 994 | // For AMDHSA, LDS_SIZE must be zero, as it is populated by the CP. |
| 995 | S_00B84C_LDS_SIZE(STM.isAmdHsaOS() ? 0 : ProgInfo.LDSBlocks) | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 996 | S_00B84C_EXCP_EN(0); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 999 | static unsigned getRsrcReg(CallingConv::ID CallConv) { |
| 1000 | switch (CallConv) { |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 1001 | default: LLVM_FALLTHROUGH; |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 1002 | case CallingConv::AMDGPU_CS: return R_00B848_COMPUTE_PGM_RSRC1; |
Tim Renouf | ef1ae8f | 2017-09-29 09:51:22 +0000 | [diff] [blame] | 1003 | case CallingConv::AMDGPU_LS: return R_00B528_SPI_SHADER_PGM_RSRC1_LS; |
Marek Olsak | a302a736 | 2017-05-02 15:41:10 +0000 | [diff] [blame] | 1004 | case CallingConv::AMDGPU_HS: return R_00B428_SPI_SHADER_PGM_RSRC1_HS; |
Tim Renouf | ef1ae8f | 2017-09-29 09:51:22 +0000 | [diff] [blame] | 1005 | case CallingConv::AMDGPU_ES: return R_00B328_SPI_SHADER_PGM_RSRC1_ES; |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 1006 | case CallingConv::AMDGPU_GS: return R_00B228_SPI_SHADER_PGM_RSRC1_GS; |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 1007 | case CallingConv::AMDGPU_VS: return R_00B128_SPI_SHADER_PGM_RSRC1_VS; |
Tim Renouf | ef1ae8f | 2017-09-29 09:51:22 +0000 | [diff] [blame] | 1008 | case CallingConv::AMDGPU_PS: return R_00B028_SPI_SHADER_PGM_RSRC1_PS; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF, |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 1013 | const SIProgramInfo &CurrentProgramInfo) { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1014 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1015 | unsigned RsrcReg = getRsrcReg(MF.getFunction().getCallingConv()); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1016 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1017 | if (AMDGPU::isCompute(MF.getFunction().getCallingConv())) { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1018 | OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4); |
| 1019 | |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 1020 | OutStreamer->EmitIntValue(CurrentProgramInfo.ComputePGMRSrc1, 4); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1021 | |
| 1022 | OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4); |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 1023 | OutStreamer->EmitIntValue(CurrentProgramInfo.ComputePGMRSrc2, 4); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1024 | |
| 1025 | OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4); |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 1026 | OutStreamer->EmitIntValue(S_00B860_WAVESIZE(CurrentProgramInfo.ScratchBlocks), 4); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1027 | |
| 1028 | // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 = |
| 1029 | // 0" comment but I don't see a corresponding field in the register spec. |
| 1030 | } else { |
| 1031 | OutStreamer->EmitIntValue(RsrcReg, 4); |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 1032 | OutStreamer->EmitIntValue(S_00B028_VGPRS(CurrentProgramInfo.VGPRBlocks) | |
| 1033 | S_00B028_SGPRS(CurrentProgramInfo.SGPRBlocks), 4); |
Scott Linder | c6c6272 | 2018-10-31 18:54:06 +0000 | [diff] [blame] | 1034 | OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4); |
| 1035 | OutStreamer->EmitIntValue( |
| 1036 | S_0286E8_WAVESIZE(CurrentProgramInfo.ScratchBlocks), 4); |
Tim Renouf | 807ecc3 | 2018-02-06 13:39:38 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | if (MF.getFunction().getCallingConv() == CallingConv::AMDGPU_PS) { |
| 1040 | OutStreamer->EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4); |
| 1041 | OutStreamer->EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(CurrentProgramInfo.LDSBlocks), 4); |
| 1042 | OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4); |
| 1043 | OutStreamer->EmitIntValue(MFI->getPSInputEnable(), 4); |
| 1044 | OutStreamer->EmitIntValue(R_0286D0_SPI_PS_INPUT_ADDR, 4); |
| 1045 | OutStreamer->EmitIntValue(MFI->getPSInputAddr(), 4); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1046 | } |
Marek Olsak | 0532c19 | 2016-07-13 17:35:15 +0000 | [diff] [blame] | 1047 | |
| 1048 | OutStreamer->EmitIntValue(R_SPILLED_SGPRS, 4); |
| 1049 | OutStreamer->EmitIntValue(MFI->getNumSpilledSGPRs(), 4); |
| 1050 | OutStreamer->EmitIntValue(R_SPILLED_VGPRS, 4); |
| 1051 | OutStreamer->EmitIntValue(MFI->getNumSpilledVGPRs(), 4); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Tim Renouf | 72800f0 | 2017-10-03 19:03:52 +0000 | [diff] [blame] | 1054 | // This is the equivalent of EmitProgramInfoSI above, but for when the OS type |
| 1055 | // is AMDPAL. It stores each compute/SPI register setting and other PAL |
Tim Renouf | d737b55 | 2019-03-20 17:42:00 +0000 | [diff] [blame] | 1056 | // metadata items into the PALMD::Metadata, combining with any provided by the |
| 1057 | // frontend as LLVM metadata. Once all functions are written, the PAL metadata |
| 1058 | // is then written as a single block in the .note section. |
Konstantin Zhuravlyov | c3beb6a | 2017-10-11 22:41:09 +0000 | [diff] [blame] | 1059 | void AMDGPUAsmPrinter::EmitPALMetadata(const MachineFunction &MF, |
Tim Renouf | 72800f0 | 2017-10-03 19:03:52 +0000 | [diff] [blame] | 1060 | const SIProgramInfo &CurrentProgramInfo) { |
| 1061 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Tim Renouf | d737b55 | 2019-03-20 17:42:00 +0000 | [diff] [blame] | 1062 | auto CC = MF.getFunction().getCallingConv(); |
| 1063 | auto MD = getTargetStreamer()->getPALMetadata(); |
| 1064 | |
Tim Renouf | e7bd52f | 2019-03-20 18:47:21 +0000 | [diff] [blame] | 1065 | MD->setEntryPoint(CC, MF.getFunction().getName()); |
Tim Renouf | d737b55 | 2019-03-20 17:42:00 +0000 | [diff] [blame] | 1066 | MD->setNumUsedVgprs(CC, CurrentProgramInfo.NumVGPRsForWavesPerEU); |
| 1067 | MD->setNumUsedSgprs(CC, CurrentProgramInfo.NumSGPRsForWavesPerEU); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1068 | if (AMDGPU::isCompute(MF.getFunction().getCallingConv())) { |
Tim Renouf | d737b55 | 2019-03-20 17:42:00 +0000 | [diff] [blame] | 1069 | MD->setRsrc1(CC, CurrentProgramInfo.ComputePGMRSrc1); |
| 1070 | MD->setRsrc2(CC, CurrentProgramInfo.ComputePGMRSrc2); |
Tim Renouf | 72800f0 | 2017-10-03 19:03:52 +0000 | [diff] [blame] | 1071 | } else { |
Tim Renouf | d737b55 | 2019-03-20 17:42:00 +0000 | [diff] [blame] | 1072 | MD->setRsrc1(CC, S_00B028_VGPRS(CurrentProgramInfo.VGPRBlocks) | |
| 1073 | S_00B028_SGPRS(CurrentProgramInfo.SGPRBlocks)); |
Tim Renouf | 72800f0 | 2017-10-03 19:03:52 +0000 | [diff] [blame] | 1074 | if (CurrentProgramInfo.ScratchBlocks > 0) |
Tim Renouf | d737b55 | 2019-03-20 17:42:00 +0000 | [diff] [blame] | 1075 | MD->setRsrc2(CC, S_00B84C_SCRATCH_EN(1)); |
Tim Renouf | 72800f0 | 2017-10-03 19:03:52 +0000 | [diff] [blame] | 1076 | } |
Tim Renouf | d737b55 | 2019-03-20 17:42:00 +0000 | [diff] [blame] | 1077 | // ScratchSize is in bytes, 16 aligned. |
| 1078 | MD->setScratchSize(CC, alignTo(CurrentProgramInfo.ScratchSize, 16)); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1079 | if (MF.getFunction().getCallingConv() == CallingConv::AMDGPU_PS) { |
Tim Renouf | d737b55 | 2019-03-20 17:42:00 +0000 | [diff] [blame] | 1080 | MD->setRsrc2(CC, S_00B02C_EXTRA_LDS_SIZE(CurrentProgramInfo.LDSBlocks)); |
| 1081 | MD->setSpiPsInputEna(MFI->getPSInputEnable()); |
| 1082 | MD->setSpiPsInputAddr(MFI->getPSInputAddr()); |
Tim Renouf | 72800f0 | 2017-10-03 19:03:52 +0000 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | |
Matt Arsenault | 24ee078 | 2016-02-12 02:40:47 +0000 | [diff] [blame] | 1086 | // This is supposed to be log2(Size) |
| 1087 | static amd_element_byte_size_t getElementByteSizeValue(unsigned Size) { |
| 1088 | switch (Size) { |
| 1089 | case 4: |
| 1090 | return AMD_ELEMENT_4_BYTES; |
| 1091 | case 8: |
| 1092 | return AMD_ELEMENT_8_BYTES; |
| 1093 | case 16: |
| 1094 | return AMD_ELEMENT_16_BYTES; |
| 1095 | default: |
| 1096 | llvm_unreachable("invalid private_element_size"); |
| 1097 | } |
| 1098 | } |
| 1099 | |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 1100 | void AMDGPUAsmPrinter::getAmdKernelCode(amd_kernel_code_t &Out, |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 1101 | const SIProgramInfo &CurrentProgramInfo, |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 1102 | const MachineFunction &MF) const { |
Matt Arsenault | 4bec7d4 | 2018-07-20 09:05:08 +0000 | [diff] [blame] | 1103 | const Function &F = MF.getFunction(); |
| 1104 | assert(F.getCallingConv() == CallingConv::AMDGPU_KERNEL || |
| 1105 | F.getCallingConv() == CallingConv::SPIR_KERNEL); |
| 1106 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1107 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 1108 | const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1109 | |
Matt Arsenault | 4cd9509 | 2019-02-12 23:44:13 +0000 | [diff] [blame] | 1110 | AMDGPU::initDefaultAMDKernelCodeT(Out, &STM); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1111 | |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 1112 | Out.compute_pgm_resource_registers = |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 1113 | CurrentProgramInfo.ComputePGMRSrc1 | |
| 1114 | (CurrentProgramInfo.ComputePGMRSrc2 << 32); |
Stanislav Mekhanoshin | 41bbe10 | 2019-05-03 21:26:39 +0000 | [diff] [blame] | 1115 | Out.code_properties |= AMD_CODE_PROPERTY_IS_PTR64; |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 1116 | |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 1117 | if (CurrentProgramInfo.DynamicCallStack) |
| 1118 | Out.code_properties |= AMD_CODE_PROPERTY_IS_DYNAMIC_CALLSTACK; |
| 1119 | |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 1120 | AMD_HSA_BITS_SET(Out.code_properties, |
Matt Arsenault | 24ee078 | 2016-02-12 02:40:47 +0000 | [diff] [blame] | 1121 | AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE, |
| 1122 | getElementByteSizeValue(STM.getMaxPrivateElementSize())); |
| 1123 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 1124 | if (MFI->hasPrivateSegmentBuffer()) { |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 1125 | Out.code_properties |= |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 1126 | AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER; |
| 1127 | } |
| 1128 | |
| 1129 | if (MFI->hasDispatchPtr()) |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 1130 | Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR; |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 1131 | |
| 1132 | if (MFI->hasQueuePtr()) |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 1133 | Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR; |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 1134 | |
| 1135 | if (MFI->hasKernargSegmentPtr()) |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 1136 | Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR; |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 1137 | |
| 1138 | if (MFI->hasDispatchID()) |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 1139 | Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID; |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 1140 | |
| 1141 | if (MFI->hasFlatScratchInit()) |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 1142 | Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT; |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 1143 | |
Tom Stellard | 48f29f2 | 2015-11-26 00:43:29 +0000 | [diff] [blame] | 1144 | if (MFI->hasDispatchPtr()) |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 1145 | Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR; |
Tom Stellard | 48f29f2 | 2015-11-26 00:43:29 +0000 | [diff] [blame] | 1146 | |
Nicolai Haehnle | 5b50497 | 2016-01-04 23:35:53 +0000 | [diff] [blame] | 1147 | if (STM.isXNACKEnabled()) |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 1148 | Out.code_properties |= AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED; |
Nicolai Haehnle | 5b50497 | 2016-01-04 23:35:53 +0000 | [diff] [blame] | 1149 | |
Matt Arsenault | 4bec7d4 | 2018-07-20 09:05:08 +0000 | [diff] [blame] | 1150 | unsigned MaxKernArgAlign; |
| 1151 | Out.kernarg_segment_byte_size = STM.getKernArgSegmentSize(F, MaxKernArgAlign); |
Matt Arsenault | b03dd8d | 2017-05-02 17:14:00 +0000 | [diff] [blame] | 1152 | Out.wavefront_sgpr_count = CurrentProgramInfo.NumSGPR; |
| 1153 | Out.workitem_vgpr_count = CurrentProgramInfo.NumVGPR; |
| 1154 | Out.workitem_private_segment_byte_size = CurrentProgramInfo.ScratchSize; |
| 1155 | Out.workgroup_group_segment_byte_size = CurrentProgramInfo.LDSSize; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1156 | |
Tom Stellard | 175959e | 2016-12-06 21:53:10 +0000 | [diff] [blame] | 1157 | // These alignment values are specified in powers of two, so alignment = |
| 1158 | // 2^n. The minimum alignment is 2^4 = 16. |
Shawn Landden | 3435787 | 2019-05-26 18:15:51 +0000 | [diff] [blame] | 1159 | Out.kernarg_segment_alignment = std::max((size_t)4, |
Matt Arsenault | 4bec7d4 | 2018-07-20 09:05:08 +0000 | [diff] [blame] | 1160 | countTrailingZeros(MaxKernArgAlign)); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1164 | const char *ExtraCode, raw_ostream &O) { |
Matt Arsenault | 36cd185 | 2017-08-09 20:09:35 +0000 | [diff] [blame] | 1165 | // First try the generic code, which knows about modifiers like 'c' and 'n'. |
Nick Desaulniers | 5277b3f | 2019-04-10 16:38:43 +0000 | [diff] [blame] | 1166 | if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O)) |
Matt Arsenault | 36cd185 | 2017-08-09 20:09:35 +0000 | [diff] [blame] | 1167 | return false; |
| 1168 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1169 | if (ExtraCode && ExtraCode[0]) { |
| 1170 | if (ExtraCode[1] != 0) |
| 1171 | return true; // Unknown modifier. |
| 1172 | |
| 1173 | switch (ExtraCode[0]) { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1174 | case 'r': |
| 1175 | break; |
Matt Arsenault | 36cd185 | 2017-08-09 20:09:35 +0000 | [diff] [blame] | 1176 | default: |
| 1177 | return true; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | |
Matt Arsenault | 36cd185 | 2017-08-09 20:09:35 +0000 | [diff] [blame] | 1181 | // TODO: Should be able to support other operand types like globals. |
| 1182 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 1183 | if (MO.isReg()) { |
| 1184 | AMDGPUInstPrinter::printRegOperand(MO.getReg(), O, |
| 1185 | *MF->getSubtarget().getRegisterInfo()); |
| 1186 | return false; |
| 1187 | } |
| 1188 | |
| 1189 | return true; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1190 | } |