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