Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 1 | //===- SIMachineFunctionInfo.cpp - SI Machine Function Info ---------------===// |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +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 | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 6 | // |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 9 | #include "SIMachineFunctionInfo.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 10 | #include "AMDGPUArgumentUsageInfo.h" |
Tom Stellard | 9646890 | 2014-09-24 01:33:17 +0000 | [diff] [blame] | 11 | #include "AMDGPUSubtarget.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 12 | #include "SIRegisterInfo.h" |
Tom Stellard | 44b30b4 | 2018-05-22 02:03:23 +0000 | [diff] [blame] | 13 | #include "MCTargetDesc/AMDGPUMCTargetDesc.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 14 | #include "Utils/AMDGPUBaseInfo.h" |
| 15 | #include "llvm/ADT/Optional.h" |
| 16 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Tom Stellard | c5cf2f0 | 2014-08-21 20:40:54 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFunction.h" |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 20 | #include "llvm/IR/CallingConv.h" |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Function.h" |
Eugene Zelenko | 59e1282 | 2017-08-08 00:47:13 +0000 | [diff] [blame] | 22 | #include <cassert> |
| 23 | #include <vector> |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 24 | |
| 25 | #define MAX_LANES 64 |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
| 29 | SIMachineFunctionInfo::SIMachineFunctionInfo(const MachineFunction &MF) |
Vincent Lejeune | ace6f73 | 2013-04-01 21:47:53 +0000 | [diff] [blame] | 30 | : AMDGPUMachineFunction(MF), |
Matt Arsenault | 055e4dc | 2019-03-29 19:14:54 +0000 | [diff] [blame] | 31 | Mode(MF.getFunction()), |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 32 | PrivateSegmentBuffer(false), |
Matt Arsenault | 49affb8 | 2015-11-25 20:55:12 +0000 | [diff] [blame] | 33 | DispatchPtr(false), |
| 34 | QueuePtr(false), |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 35 | KernargSegmentPtr(false), |
Matt Arsenault | 8d718dc | 2016-07-22 17:01:30 +0000 | [diff] [blame] | 36 | DispatchID(false), |
Matt Arsenault | 49affb8 | 2015-11-25 20:55:12 +0000 | [diff] [blame] | 37 | FlatScratchInit(false), |
Tom Stellard | f110f8f | 2016-04-14 16:27:03 +0000 | [diff] [blame] | 38 | WorkGroupIDX(false), |
Matt Arsenault | 49affb8 | 2015-11-25 20:55:12 +0000 | [diff] [blame] | 39 | WorkGroupIDY(false), |
| 40 | WorkGroupIDZ(false), |
| 41 | WorkGroupInfo(false), |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 42 | PrivateSegmentWaveByteOffset(false), |
Tom Stellard | f110f8f | 2016-04-14 16:27:03 +0000 | [diff] [blame] | 43 | WorkItemIDX(false), |
Matt Arsenault | 49affb8 | 2015-11-25 20:55:12 +0000 | [diff] [blame] | 44 | WorkItemIDY(false), |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 45 | WorkItemIDZ(false), |
Matt Arsenault | 817c253 | 2017-08-03 23:12:44 +0000 | [diff] [blame] | 46 | ImplicitBufferPtr(false), |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 47 | ImplicitArgPtr(false), |
Matt Arsenault | 923712b | 2018-02-09 16:57:57 +0000 | [diff] [blame] | 48 | GITPtrHigh(0xffffffff), |
Nicolai Haehnle | 4dc3b2b | 2019-07-01 17:17:45 +0000 | [diff] [blame] | 49 | HighBitsOf32BitAddress(0), |
| 50 | GDSSize(0) { |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 51 | const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 52 | const Function &F = MF.getFunction(); |
| 53 | FlatWorkGroupSizes = ST.getFlatWorkGroupSizes(F); |
| 54 | WavesPerEU = ST.getWavesPerEU(F); |
Matt Arsenault | 49affb8 | 2015-11-25 20:55:12 +0000 | [diff] [blame] | 55 | |
Stanislav Mekhanoshin | d4b500c | 2018-05-31 05:36:04 +0000 | [diff] [blame] | 56 | Occupancy = getMaxWavesPerEU(); |
| 57 | limitOccupancy(MF); |
Matt Arsenault | 4bec7d4 | 2018-07-20 09:05:08 +0000 | [diff] [blame] | 58 | CallingConv::ID CC = F.getCallingConv(); |
| 59 | |
| 60 | if (CC == CallingConv::AMDGPU_KERNEL || CC == CallingConv::SPIR_KERNEL) { |
| 61 | if (!F.arg_empty()) |
| 62 | KernargSegmentPtr = true; |
| 63 | WorkGroupIDX = true; |
| 64 | WorkItemIDX = true; |
| 65 | } else if (CC == CallingConv::AMDGPU_PS) { |
| 66 | PSInputAddr = AMDGPU::getInitialPSInputAddr(F); |
| 67 | } |
Stanislav Mekhanoshin | d4b500c | 2018-05-31 05:36:04 +0000 | [diff] [blame] | 68 | |
Matt Arsenault | 2b1f9aa | 2017-05-17 21:56:25 +0000 | [diff] [blame] | 69 | if (!isEntryFunction()) { |
| 70 | // Non-entry functions have no special inputs for now, other registers |
| 71 | // required for scratch access. |
| 72 | ScratchRSrcReg = AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3; |
Matt Arsenault | d88db6d | 2019-06-20 21:58:24 +0000 | [diff] [blame] | 73 | ScratchWaveOffsetReg = AMDGPU::SGPR33; |
Matt Arsenault | 2b1f9aa | 2017-05-17 21:56:25 +0000 | [diff] [blame] | 74 | FrameOffsetReg = AMDGPU::SGPR5; |
Matt Arsenault | f28683c | 2017-06-26 17:53:59 +0000 | [diff] [blame] | 75 | StackPtrOffsetReg = AMDGPU::SGPR32; |
Matt Arsenault | 1cc47f8 | 2017-07-18 16:44:56 +0000 | [diff] [blame] | 76 | |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 77 | ArgInfo.PrivateSegmentBuffer = |
| 78 | ArgDescriptor::createRegister(ScratchRSrcReg); |
| 79 | ArgInfo.PrivateSegmentWaveByteOffset = |
| 80 | ArgDescriptor::createRegister(ScratchWaveOffsetReg); |
| 81 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 82 | if (F.hasFnAttribute("amdgpu-implicitarg-ptr")) |
Matt Arsenault | 9166ce8 | 2017-07-28 15:52:08 +0000 | [diff] [blame] | 83 | ImplicitArgPtr = true; |
| 84 | } else { |
Matt Arsenault | 1ea0402 | 2018-05-29 19:35:00 +0000 | [diff] [blame] | 85 | if (F.hasFnAttribute("amdgpu-implicitarg-ptr")) { |
Matt Arsenault | 9166ce8 | 2017-07-28 15:52:08 +0000 | [diff] [blame] | 86 | KernargSegmentPtr = true; |
Matt Arsenault | 4bec7d4 | 2018-07-20 09:05:08 +0000 | [diff] [blame] | 87 | MaxKernArgAlign = std::max(ST.getAlignmentForImplicitArgPtr(), |
| 88 | MaxKernArgAlign); |
Matt Arsenault | 1ea0402 | 2018-05-29 19:35:00 +0000 | [diff] [blame] | 89 | } |
Matt Arsenault | 2b1f9aa | 2017-05-17 21:56:25 +0000 | [diff] [blame] | 90 | } |
Marek Olsak | fccabaf | 2016-01-13 11:45:36 +0000 | [diff] [blame] | 91 | |
Matt Arsenault | aa6fb4c | 2019-02-21 23:27:46 +0000 | [diff] [blame] | 92 | if (F.hasFnAttribute("amdgpu-work-group-id-x")) |
Matt Arsenault | e15855d | 2017-07-17 22:35:50 +0000 | [diff] [blame] | 93 | WorkGroupIDX = true; |
Matt Arsenault | aa6fb4c | 2019-02-21 23:27:46 +0000 | [diff] [blame] | 94 | |
| 95 | if (F.hasFnAttribute("amdgpu-work-group-id-y")) |
Matt Arsenault | 49affb8 | 2015-11-25 20:55:12 +0000 | [diff] [blame] | 96 | WorkGroupIDY = true; |
Matt Arsenault | aa6fb4c | 2019-02-21 23:27:46 +0000 | [diff] [blame] | 97 | |
| 98 | if (F.hasFnAttribute("amdgpu-work-group-id-z")) |
Matt Arsenault | 49affb8 | 2015-11-25 20:55:12 +0000 | [diff] [blame] | 99 | WorkGroupIDZ = true; |
Matt Arsenault | aa6fb4c | 2019-02-21 23:27:46 +0000 | [diff] [blame] | 100 | |
| 101 | if (F.hasFnAttribute("amdgpu-work-item-id-x")) |
Matt Arsenault | e15855d | 2017-07-17 22:35:50 +0000 | [diff] [blame] | 102 | WorkItemIDX = true; |
Matt Arsenault | aa6fb4c | 2019-02-21 23:27:46 +0000 | [diff] [blame] | 103 | |
| 104 | if (F.hasFnAttribute("amdgpu-work-item-id-y")) |
Matt Arsenault | 49affb8 | 2015-11-25 20:55:12 +0000 | [diff] [blame] | 105 | WorkItemIDY = true; |
Matt Arsenault | aa6fb4c | 2019-02-21 23:27:46 +0000 | [diff] [blame] | 106 | |
| 107 | if (F.hasFnAttribute("amdgpu-work-item-id-z")) |
Matt Arsenault | 49affb8 | 2015-11-25 20:55:12 +0000 | [diff] [blame] | 108 | WorkItemIDZ = true; |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 109 | |
Matt Arsenault | efa9f4b | 2017-04-11 22:29:28 +0000 | [diff] [blame] | 110 | const MachineFrameInfo &FrameInfo = MF.getFrameInfo(); |
Matt Arsenault | 1cc47f8 | 2017-07-18 16:44:56 +0000 | [diff] [blame] | 111 | bool HasStackObjects = FrameInfo.hasStackObjects(); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 112 | |
Matt Arsenault | e15855d | 2017-07-17 22:35:50 +0000 | [diff] [blame] | 113 | if (isEntryFunction()) { |
| 114 | // X, XY, and XYZ are the only supported combinations, so make sure Y is |
| 115 | // enabled if Z is. |
| 116 | if (WorkItemIDZ) |
| 117 | WorkItemIDY = true; |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 118 | |
Scott Linder | c6c6272 | 2018-10-31 18:54:06 +0000 | [diff] [blame] | 119 | PrivateSegmentWaveByteOffset = true; |
Matt Arsenault | e15855d | 2017-07-17 22:35:50 +0000 | [diff] [blame] | 120 | |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 121 | // HS and GS always have the scratch wave offset in SGPR5 on GFX9. |
| 122 | if (ST.getGeneration() >= AMDGPUSubtarget::GFX9 && |
| 123 | (CC == CallingConv::AMDGPU_HS || CC == CallingConv::AMDGPU_GS)) |
Scott Linder | c6c6272 | 2018-10-31 18:54:06 +0000 | [diff] [blame] | 124 | ArgInfo.PrivateSegmentWaveByteOffset = |
| 125 | ArgDescriptor::createRegister(AMDGPU::SGPR5); |
Marek Olsak | 584d2c0 | 2017-05-04 22:25:20 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Konstantin Zhuravlyov | aa067cb | 2018-10-04 21:02:16 +0000 | [diff] [blame] | 128 | bool isAmdHsaOrMesa = ST.isAmdHsaOrMesa(F); |
| 129 | if (isAmdHsaOrMesa) { |
Scott Linder | c6c6272 | 2018-10-31 18:54:06 +0000 | [diff] [blame] | 130 | PrivateSegmentBuffer = true; |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 131 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 132 | if (F.hasFnAttribute("amdgpu-dispatch-ptr")) |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 133 | DispatchPtr = true; |
Matt Arsenault | 48ab526 | 2016-04-25 19:27:18 +0000 | [diff] [blame] | 134 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 135 | if (F.hasFnAttribute("amdgpu-queue-ptr")) |
Matt Arsenault | 48ab526 | 2016-04-25 19:27:18 +0000 | [diff] [blame] | 136 | QueuePtr = true; |
Matt Arsenault | 8d718dc | 2016-07-22 17:01:30 +0000 | [diff] [blame] | 137 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 138 | if (F.hasFnAttribute("amdgpu-dispatch-id")) |
Matt Arsenault | 8d718dc | 2016-07-22 17:01:30 +0000 | [diff] [blame] | 139 | DispatchID = true; |
Matt Arsenault | ceafc55 | 2018-05-29 17:42:50 +0000 | [diff] [blame] | 140 | } else if (ST.isMesaGfxShader(F)) { |
Scott Linder | c6c6272 | 2018-10-31 18:54:06 +0000 | [diff] [blame] | 141 | ImplicitBufferPtr = true; |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 144 | if (F.hasFnAttribute("amdgpu-kernarg-segment-ptr")) |
Matt Arsenault | 23e4df6 | 2017-07-14 00:11:13 +0000 | [diff] [blame] | 145 | KernargSegmentPtr = true; |
| 146 | |
Konstantin Zhuravlyov | aa067cb | 2018-10-04 21:02:16 +0000 | [diff] [blame] | 147 | if (ST.hasFlatAddressSpace() && isEntryFunction() && isAmdHsaOrMesa) { |
Matt Arsenault | 254ad3d | 2017-07-18 16:44:58 +0000 | [diff] [blame] | 148 | // TODO: This could be refined a lot. The attribute is a poor way of |
| 149 | // detecting calls that may require it before argument lowering. |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 150 | if (HasStackObjects || F.hasFnAttribute("amdgpu-flat-scratch")) |
Matt Arsenault | 254ad3d | 2017-07-18 16:44:58 +0000 | [diff] [blame] | 151 | FlatScratchInit = true; |
| 152 | } |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 153 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 154 | Attribute A = F.getFnAttribute("amdgpu-git-ptr-high"); |
Tim Renouf | 1322915 | 2017-09-29 09:49:35 +0000 | [diff] [blame] | 155 | StringRef S = A.getValueAsString(); |
| 156 | if (!S.empty()) |
| 157 | S.consumeInteger(0, GITPtrHigh); |
Matt Arsenault | 923712b | 2018-02-09 16:57:57 +0000 | [diff] [blame] | 158 | |
| 159 | A = F.getFnAttribute("amdgpu-32bit-address-high-bits"); |
| 160 | S = A.getValueAsString(); |
| 161 | if (!S.empty()) |
| 162 | S.consumeInteger(0, HighBitsOf32BitAddress); |
Nicolai Haehnle | 4dc3b2b | 2019-07-01 17:17:45 +0000 | [diff] [blame] | 163 | |
| 164 | S = F.getFnAttribute("amdgpu-gds-size").getValueAsString(); |
| 165 | if (!S.empty()) |
| 166 | S.consumeInteger(0, GDSSize); |
Matt Arsenault | 49affb8 | 2015-11-25 20:55:12 +0000 | [diff] [blame] | 167 | } |
Tom Stellard | c5cf2f0 | 2014-08-21 20:40:54 +0000 | [diff] [blame] | 168 | |
Stanislav Mekhanoshin | d4b500c | 2018-05-31 05:36:04 +0000 | [diff] [blame] | 169 | void SIMachineFunctionInfo::limitOccupancy(const MachineFunction &MF) { |
| 170 | limitOccupancy(getMaxWavesPerEU()); |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 171 | const GCNSubtarget& ST = MF.getSubtarget<GCNSubtarget>(); |
Stanislav Mekhanoshin | d4b500c | 2018-05-31 05:36:04 +0000 | [diff] [blame] | 172 | limitOccupancy(ST.getOccupancyWithLocalMemSize(getLDSSize(), |
| 173 | MF.getFunction())); |
| 174 | } |
| 175 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 176 | unsigned SIMachineFunctionInfo::addPrivateSegmentBuffer( |
| 177 | const SIRegisterInfo &TRI) { |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 178 | ArgInfo.PrivateSegmentBuffer = |
| 179 | ArgDescriptor::createRegister(TRI.getMatchingSuperReg( |
| 180 | getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_128RegClass)); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 181 | NumUserSGPRs += 4; |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 182 | return ArgInfo.PrivateSegmentBuffer.getRegister(); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | unsigned SIMachineFunctionInfo::addDispatchPtr(const SIRegisterInfo &TRI) { |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 186 | ArgInfo.DispatchPtr = ArgDescriptor::createRegister(TRI.getMatchingSuperReg( |
| 187 | getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass)); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 188 | NumUserSGPRs += 2; |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 189 | return ArgInfo.DispatchPtr.getRegister(); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | unsigned SIMachineFunctionInfo::addQueuePtr(const SIRegisterInfo &TRI) { |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 193 | ArgInfo.QueuePtr = ArgDescriptor::createRegister(TRI.getMatchingSuperReg( |
| 194 | getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass)); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 195 | NumUserSGPRs += 2; |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 196 | return ArgInfo.QueuePtr.getRegister(); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | unsigned SIMachineFunctionInfo::addKernargSegmentPtr(const SIRegisterInfo &TRI) { |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 200 | ArgInfo.KernargSegmentPtr |
| 201 | = ArgDescriptor::createRegister(TRI.getMatchingSuperReg( |
| 202 | getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass)); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 203 | NumUserSGPRs += 2; |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 204 | return ArgInfo.KernargSegmentPtr.getRegister(); |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Matt Arsenault | 8d718dc | 2016-07-22 17:01:30 +0000 | [diff] [blame] | 207 | unsigned SIMachineFunctionInfo::addDispatchID(const SIRegisterInfo &TRI) { |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 208 | ArgInfo.DispatchID = ArgDescriptor::createRegister(TRI.getMatchingSuperReg( |
| 209 | getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass)); |
Matt Arsenault | 8d718dc | 2016-07-22 17:01:30 +0000 | [diff] [blame] | 210 | NumUserSGPRs += 2; |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 211 | return ArgInfo.DispatchID.getRegister(); |
Matt Arsenault | 8d718dc | 2016-07-22 17:01:30 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Matt Arsenault | 296b849 | 2016-02-12 06:31:30 +0000 | [diff] [blame] | 214 | unsigned SIMachineFunctionInfo::addFlatScratchInit(const SIRegisterInfo &TRI) { |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 215 | ArgInfo.FlatScratchInit = ArgDescriptor::createRegister(TRI.getMatchingSuperReg( |
| 216 | getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass)); |
Matt Arsenault | 296b849 | 2016-02-12 06:31:30 +0000 | [diff] [blame] | 217 | NumUserSGPRs += 2; |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 218 | return ArgInfo.FlatScratchInit.getRegister(); |
Matt Arsenault | 296b849 | 2016-02-12 06:31:30 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Matt Arsenault | 10fc062 | 2017-06-26 03:01:31 +0000 | [diff] [blame] | 221 | unsigned SIMachineFunctionInfo::addImplicitBufferPtr(const SIRegisterInfo &TRI) { |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 222 | ArgInfo.ImplicitBufferPtr = ArgDescriptor::createRegister(TRI.getMatchingSuperReg( |
| 223 | getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass)); |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 224 | NumUserSGPRs += 2; |
Matt Arsenault | 8623e8d | 2017-08-03 23:00:29 +0000 | [diff] [blame] | 225 | return ArgInfo.ImplicitBufferPtr.getRegister(); |
Tom Stellard | 2f3f985 | 2017-01-25 01:25:13 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Matt Arsenault | 8e8f8f4 | 2017-08-02 01:52:45 +0000 | [diff] [blame] | 228 | static bool isCalleeSavedReg(const MCPhysReg *CSRegs, MCPhysReg Reg) { |
| 229 | for (unsigned I = 0; CSRegs[I]; ++I) { |
| 230 | if (CSRegs[I] == Reg) |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | return false; |
| 235 | } |
| 236 | |
Matt Arsenault | e0bf7d0 | 2017-02-21 19:12:08 +0000 | [diff] [blame] | 237 | /// Reserve a slice of a VGPR to support spilling for FrameIndex \p FI. |
| 238 | bool SIMachineFunctionInfo::allocateSGPRSpillToVGPR(MachineFunction &MF, |
| 239 | int FI) { |
| 240 | std::vector<SpilledReg> &SpillLanes = SGPRToVGPRSpills[FI]; |
Matt Arsenault | 8d4b0ed | 2016-06-23 20:00:34 +0000 | [diff] [blame] | 241 | |
Matt Arsenault | e0bf7d0 | 2017-02-21 19:12:08 +0000 | [diff] [blame] | 242 | // This has already been allocated. |
| 243 | if (!SpillLanes.empty()) |
| 244 | return true; |
| 245 | |
Tom Stellard | 5bfbae5 | 2018-07-11 20:59:01 +0000 | [diff] [blame] | 246 | const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 247 | const SIRegisterInfo *TRI = ST.getRegisterInfo(); |
Matt Arsenault | e0bf7d0 | 2017-02-21 19:12:08 +0000 | [diff] [blame] | 248 | MachineFrameInfo &FrameInfo = MF.getFrameInfo(); |
| 249 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 250 | unsigned WaveSize = ST.getWavefrontSize(); |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 251 | |
Matt Arsenault | e0bf7d0 | 2017-02-21 19:12:08 +0000 | [diff] [blame] | 252 | unsigned Size = FrameInfo.getObjectSize(FI); |
| 253 | assert(Size >= 4 && Size <= 64 && "invalid sgpr spill size"); |
| 254 | assert(TRI->spillSGPRToVGPR() && "not spilling SGPRs to VGPRs"); |
Tom Stellard | c5cf2f0 | 2014-08-21 20:40:54 +0000 | [diff] [blame] | 255 | |
Matt Arsenault | e0bf7d0 | 2017-02-21 19:12:08 +0000 | [diff] [blame] | 256 | int NumLanes = Size / 4; |
Tom Stellard | c5cf2f0 | 2014-08-21 20:40:54 +0000 | [diff] [blame] | 257 | |
Matt Arsenault | e0b8443 | 2019-06-26 13:39:29 +0000 | [diff] [blame] | 258 | const MCPhysReg *CSRegs = MRI.getCalleeSavedRegs(); |
Matt Arsenault | 8e8f8f4 | 2017-08-02 01:52:45 +0000 | [diff] [blame] | 259 | |
Matt Arsenault | e0bf7d0 | 2017-02-21 19:12:08 +0000 | [diff] [blame] | 260 | // Make sure to handle the case where a wide SGPR spill may span between two |
| 261 | // VGPRs. |
| 262 | for (int I = 0; I < NumLanes; ++I, ++NumVGPRSpillLanes) { |
| 263 | unsigned LaneVGPR; |
| 264 | unsigned VGPRIndex = (NumVGPRSpillLanes % WaveSize); |
Tom Stellard | c5cf2f0 | 2014-08-21 20:40:54 +0000 | [diff] [blame] | 265 | |
Matt Arsenault | e0bf7d0 | 2017-02-21 19:12:08 +0000 | [diff] [blame] | 266 | if (VGPRIndex == 0) { |
| 267 | LaneVGPR = TRI->findUnusedRegister(MRI, &AMDGPU::VGPR_32RegClass, MF); |
| 268 | if (LaneVGPR == AMDGPU::NoRegister) { |
Tim Renouf | 6cb007f | 2017-09-11 08:31:32 +0000 | [diff] [blame] | 269 | // We have no VGPRs left for spilling SGPRs. Reset because we will not |
Matt Arsenault | e0bf7d0 | 2017-02-21 19:12:08 +0000 | [diff] [blame] | 270 | // partially spill the SGPR to VGPRs. |
| 271 | SGPRToVGPRSpills.erase(FI); |
| 272 | NumVGPRSpillLanes -= I; |
| 273 | return false; |
| 274 | } |
Nicolai Haehnle | e705aad | 2016-01-04 15:50:01 +0000 | [diff] [blame] | 275 | |
Matt Arsenault | 8e8f8f4 | 2017-08-02 01:52:45 +0000 | [diff] [blame] | 276 | Optional<int> CSRSpillFI; |
Matt Arsenault | 17f3338 | 2018-03-27 19:42:55 +0000 | [diff] [blame] | 277 | if ((FrameInfo.hasCalls() || !isEntryFunction()) && CSRegs && |
| 278 | isCalleeSavedReg(CSRegs, LaneVGPR)) { |
| 279 | CSRSpillFI = FrameInfo.CreateSpillStackObject(4, 4); |
Matt Arsenault | 8e8f8f4 | 2017-08-02 01:52:45 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | SpillVGPRs.push_back(SGPRSpillVGPRCSR(LaneVGPR, CSRSpillFI)); |
Nicolai Haehnle | e705aad | 2016-01-04 15:50:01 +0000 | [diff] [blame] | 283 | |
Matt Arsenault | e0bf7d0 | 2017-02-21 19:12:08 +0000 | [diff] [blame] | 284 | // Add this register as live-in to all blocks to avoid machine verifer |
| 285 | // complaining about use of an undefined physical register. |
| 286 | for (MachineBasicBlock &BB : MF) |
| 287 | BB.addLiveIn(LaneVGPR); |
| 288 | } else { |
Matt Arsenault | 8e8f8f4 | 2017-08-02 01:52:45 +0000 | [diff] [blame] | 289 | LaneVGPR = SpillVGPRs.back().VGPR; |
Tom Stellard | c5cf2f0 | 2014-08-21 20:40:54 +0000 | [diff] [blame] | 290 | } |
Matt Arsenault | e0bf7d0 | 2017-02-21 19:12:08 +0000 | [diff] [blame] | 291 | |
| 292 | SpillLanes.push_back(SpilledReg(LaneVGPR, VGPRIndex)); |
Tom Stellard | c5cf2f0 | 2014-08-21 20:40:54 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Matt Arsenault | e0bf7d0 | 2017-02-21 19:12:08 +0000 | [diff] [blame] | 295 | return true; |
| 296 | } |
| 297 | |
| 298 | void SIMachineFunctionInfo::removeSGPRToVGPRFrameIndices(MachineFrameInfo &MFI) { |
| 299 | for (auto &R : SGPRToVGPRSpills) |
| 300 | MFI.RemoveStackObject(R.first); |
Sander de Smalen | 7f23e0a | 2019-04-02 09:46:52 +0000 | [diff] [blame] | 301 | // All other SPGRs must be allocated on the default stack, so reset |
| 302 | // the stack ID. |
| 303 | for (unsigned i = MFI.getObjectIndexBegin(), e = MFI.getObjectIndexEnd(); |
| 304 | i != e; ++i) |
| 305 | MFI.setStackID(i, 0); |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 306 | } |
Tom Stellard | 44b30b4 | 2018-05-22 02:03:23 +0000 | [diff] [blame] | 307 | |
Tom Stellard | 44b30b4 | 2018-05-22 02:03:23 +0000 | [diff] [blame] | 308 | MCPhysReg SIMachineFunctionInfo::getNextUserSGPR() const { |
| 309 | assert(NumSystemSGPRs == 0 && "System SGPRs must be added after user SGPRs"); |
| 310 | return AMDGPU::SGPR0 + NumUserSGPRs; |
| 311 | } |
| 312 | |
| 313 | MCPhysReg SIMachineFunctionInfo::getNextSystemSGPR() const { |
| 314 | return AMDGPU::SGPR0 + NumUserSGPRs + NumSystemSGPRs; |
| 315 | } |
Matt Arsenault | bc6d07c | 2019-03-14 22:54:43 +0000 | [diff] [blame] | 316 | |
| 317 | static yaml::StringValue regToString(unsigned Reg, |
| 318 | const TargetRegisterInfo &TRI) { |
| 319 | yaml::StringValue Dest; |
Tim Renouf | 8723a56 | 2019-03-18 19:00:46 +0000 | [diff] [blame] | 320 | { |
| 321 | raw_string_ostream OS(Dest.Value); |
| 322 | OS << printReg(Reg, &TRI); |
| 323 | } |
Matt Arsenault | bc6d07c | 2019-03-14 22:54:43 +0000 | [diff] [blame] | 324 | return Dest; |
| 325 | } |
| 326 | |
Michael Liao | 80177ca | 2019-07-03 02:00:21 +0000 | [diff] [blame] | 327 | static Optional<yaml::SIArgumentInfo> |
| 328 | convertArgumentInfo(const AMDGPUFunctionArgInfo &ArgInfo, |
| 329 | const TargetRegisterInfo &TRI) { |
| 330 | yaml::SIArgumentInfo AI; |
| 331 | |
| 332 | auto convertArg = [&](Optional<yaml::SIArgument> &A, |
| 333 | const ArgDescriptor &Arg) { |
| 334 | if (!Arg) |
| 335 | return false; |
| 336 | |
| 337 | // Create a register or stack argument. |
| 338 | yaml::SIArgument SA = yaml::SIArgument::createArgument(Arg.isRegister()); |
| 339 | if (Arg.isRegister()) { |
| 340 | raw_string_ostream OS(SA.RegisterName.Value); |
| 341 | OS << printReg(Arg.getRegister(), &TRI); |
| 342 | } else |
| 343 | SA.StackOffset = Arg.getStackOffset(); |
| 344 | // Check and update the optional mask. |
| 345 | if (Arg.isMasked()) |
| 346 | SA.Mask = Arg.getMask(); |
| 347 | |
| 348 | A = SA; |
| 349 | return true; |
| 350 | }; |
| 351 | |
| 352 | bool Any = false; |
| 353 | Any |= convertArg(AI.PrivateSegmentBuffer, ArgInfo.PrivateSegmentBuffer); |
| 354 | Any |= convertArg(AI.DispatchPtr, ArgInfo.DispatchPtr); |
| 355 | Any |= convertArg(AI.QueuePtr, ArgInfo.QueuePtr); |
| 356 | Any |= convertArg(AI.KernargSegmentPtr, ArgInfo.KernargSegmentPtr); |
| 357 | Any |= convertArg(AI.DispatchID, ArgInfo.DispatchID); |
| 358 | Any |= convertArg(AI.FlatScratchInit, ArgInfo.FlatScratchInit); |
| 359 | Any |= convertArg(AI.PrivateSegmentSize, ArgInfo.PrivateSegmentSize); |
| 360 | Any |= convertArg(AI.WorkGroupIDX, ArgInfo.WorkGroupIDX); |
| 361 | Any |= convertArg(AI.WorkGroupIDY, ArgInfo.WorkGroupIDY); |
| 362 | Any |= convertArg(AI.WorkGroupIDZ, ArgInfo.WorkGroupIDZ); |
| 363 | Any |= convertArg(AI.WorkGroupInfo, ArgInfo.WorkGroupInfo); |
| 364 | Any |= convertArg(AI.PrivateSegmentWaveByteOffset, |
| 365 | ArgInfo.PrivateSegmentWaveByteOffset); |
| 366 | Any |= convertArg(AI.ImplicitArgPtr, ArgInfo.ImplicitArgPtr); |
| 367 | Any |= convertArg(AI.ImplicitBufferPtr, ArgInfo.ImplicitBufferPtr); |
| 368 | Any |= convertArg(AI.WorkItemIDX, ArgInfo.WorkItemIDX); |
| 369 | Any |= convertArg(AI.WorkItemIDY, ArgInfo.WorkItemIDY); |
| 370 | Any |= convertArg(AI.WorkItemIDZ, ArgInfo.WorkItemIDZ); |
| 371 | |
| 372 | if (Any) |
| 373 | return AI; |
| 374 | |
| 375 | return None; |
| 376 | } |
| 377 | |
Matt Arsenault | bc6d07c | 2019-03-14 22:54:43 +0000 | [diff] [blame] | 378 | yaml::SIMachineFunctionInfo::SIMachineFunctionInfo( |
| 379 | const llvm::SIMachineFunctionInfo& MFI, |
| 380 | const TargetRegisterInfo &TRI) |
| 381 | : ExplicitKernArgSize(MFI.getExplicitKernArgSize()), |
| 382 | MaxKernArgAlign(MFI.getMaxKernArgAlign()), |
| 383 | LDSSize(MFI.getLDSSize()), |
| 384 | IsEntryFunction(MFI.isEntryFunction()), |
| 385 | NoSignedZerosFPMath(MFI.hasNoSignedZerosFPMath()), |
| 386 | MemoryBound(MFI.isMemoryBound()), |
| 387 | WaveLimiter(MFI.needsWaveLimiter()), |
| 388 | ScratchRSrcReg(regToString(MFI.getScratchRSrcReg(), TRI)), |
| 389 | ScratchWaveOffsetReg(regToString(MFI.getScratchWaveOffsetReg(), TRI)), |
| 390 | FrameOffsetReg(regToString(MFI.getFrameOffsetReg(), TRI)), |
Michael Liao | 80177ca | 2019-07-03 02:00:21 +0000 | [diff] [blame] | 391 | StackPtrOffsetReg(regToString(MFI.getStackPtrOffsetReg(), TRI)), |
| 392 | ArgInfo(convertArgumentInfo(MFI.getArgInfo(), TRI)) {} |
Matt Arsenault | bc6d07c | 2019-03-14 22:54:43 +0000 | [diff] [blame] | 393 | |
| 394 | void yaml::SIMachineFunctionInfo::mappingImpl(yaml::IO &YamlIO) { |
| 395 | MappingTraits<SIMachineFunctionInfo>::mapping(YamlIO, *this); |
| 396 | } |
| 397 | |
| 398 | bool SIMachineFunctionInfo::initializeBaseYamlFields( |
| 399 | const yaml::SIMachineFunctionInfo &YamlMFI) { |
| 400 | ExplicitKernArgSize = YamlMFI.ExplicitKernArgSize; |
| 401 | MaxKernArgAlign = YamlMFI.MaxKernArgAlign; |
| 402 | LDSSize = YamlMFI.LDSSize; |
| 403 | IsEntryFunction = YamlMFI.IsEntryFunction; |
| 404 | NoSignedZerosFPMath = YamlMFI.NoSignedZerosFPMath; |
| 405 | MemoryBound = YamlMFI.MemoryBound; |
| 406 | WaveLimiter = YamlMFI.WaveLimiter; |
| 407 | return false; |
| 408 | } |