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