| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "AMDGPUArgumentUsageInfo.h" |
| 10 | #include "SIRegisterInfo.h" |
| 11 | #include "Utils/AMDGPUBaseInfo.h" |
| 12 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 13 | #include "llvm/Support/NativeFormatting.h" |
| 14 | #include "llvm/Support/raw_ostream.h" |
| 15 | |
| 16 | using namespace llvm; |
| 17 | |
| 18 | void ArgDescriptor::print(raw_ostream &OS, |
| 19 | const TargetRegisterInfo *TRI) const { |
| 20 | if (!isSet()) { |
| 21 | OS << "<not set>\n" ; |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | if (isRegister()) |
| 26 | OS << "Reg " << printReg(Reg: getRegister(), TRI); |
| 27 | else |
| 28 | OS << "Stack offset " << getStackOffset(); |
| 29 | |
| 30 | if (isMasked()) { |
| 31 | OS << " & " ; |
| 32 | llvm::write_hex(S&: OS, N: Mask, Style: llvm::HexPrintStyle::PrefixLower); |
| 33 | } |
| 34 | |
| 35 | OS << '\n'; |
| 36 | } |
| 37 | |
| 38 | // Hardcoded registers from fixed function ABI |
| 39 | const AMDGPUFunctionArgInfo AMDGPUFunctionArgInfo::FixedABIFunctionInfo = |
| 40 | AMDGPUFunctionArgInfo::fixedABILayout(); |
| 41 | |
| 42 | std::tuple<const ArgDescriptor *, const TargetRegisterClass *, LLT> |
| 43 | AMDGPUFunctionArgInfo::getPreloadedValue( |
| 44 | AMDGPUFunctionArgInfo::PreloadedValue Value) const { |
| 45 | switch (Value) { |
| 46 | case AMDGPUFunctionArgInfo::PRIVATE_SEGMENT_BUFFER: { |
| 47 | return std::tuple(PrivateSegmentBuffer ? &PrivateSegmentBuffer : nullptr, |
| 48 | &AMDGPU::SGPR_128RegClass, LLT::fixed_vector(NumElements: 4, ScalarSizeInBits: 32)); |
| 49 | } |
| 50 | case AMDGPUFunctionArgInfo::IMPLICIT_BUFFER_PTR: |
| 51 | return std::tuple(ImplicitBufferPtr ? &ImplicitBufferPtr : nullptr, |
| 52 | &AMDGPU::SGPR_64RegClass, |
| 53 | LLT::pointer(AddressSpace: AMDGPUAS::CONSTANT_ADDRESS, SizeInBits: 64)); |
| 54 | case AMDGPUFunctionArgInfo::WORKGROUP_ID_X: |
| 55 | return std::tuple(WorkGroupIDX ? &WorkGroupIDX : nullptr, |
| 56 | &AMDGPU::SGPR_32RegClass, LLT::scalar(SizeInBits: 32)); |
| 57 | case AMDGPUFunctionArgInfo::WORKGROUP_ID_Y: |
| 58 | return std::tuple(WorkGroupIDY ? &WorkGroupIDY : nullptr, |
| 59 | &AMDGPU::SGPR_32RegClass, LLT::scalar(SizeInBits: 32)); |
| 60 | case AMDGPUFunctionArgInfo::WORKGROUP_ID_Z: |
| 61 | return std::tuple(WorkGroupIDZ ? &WorkGroupIDZ : nullptr, |
| 62 | &AMDGPU::SGPR_32RegClass, LLT::scalar(SizeInBits: 32)); |
| 63 | case AMDGPUFunctionArgInfo::CLUSTER_WORKGROUP_ID_X: |
| 64 | case AMDGPUFunctionArgInfo::CLUSTER_WORKGROUP_ID_Y: |
| 65 | case AMDGPUFunctionArgInfo::CLUSTER_WORKGROUP_ID_Z: |
| 66 | case AMDGPUFunctionArgInfo::CLUSTER_WORKGROUP_MAX_ID_X: |
| 67 | case AMDGPUFunctionArgInfo::CLUSTER_WORKGROUP_MAX_ID_Y: |
| 68 | case AMDGPUFunctionArgInfo::CLUSTER_WORKGROUP_MAX_ID_Z: |
| 69 | case AMDGPUFunctionArgInfo::CLUSTER_WORKGROUP_MAX_FLAT_ID: |
| 70 | return std::tuple(nullptr, &AMDGPU::SGPR_32RegClass, LLT::scalar(SizeInBits: 32)); |
| 71 | case AMDGPUFunctionArgInfo::LDS_KERNEL_ID: |
| 72 | return std::tuple(LDSKernelId ? &LDSKernelId : nullptr, |
| 73 | &AMDGPU::SGPR_32RegClass, LLT::scalar(SizeInBits: 32)); |
| 74 | case AMDGPUFunctionArgInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET: |
| 75 | return std::tuple( |
| 76 | PrivateSegmentWaveByteOffset ? &PrivateSegmentWaveByteOffset : nullptr, |
| 77 | &AMDGPU::SGPR_32RegClass, LLT::scalar(SizeInBits: 32)); |
| 78 | case AMDGPUFunctionArgInfo::PRIVATE_SEGMENT_SIZE: |
| 79 | return {PrivateSegmentSize ? &PrivateSegmentSize : nullptr, |
| 80 | &AMDGPU::SGPR_32RegClass, LLT::scalar(SizeInBits: 32)}; |
| 81 | case AMDGPUFunctionArgInfo::KERNARG_SEGMENT_PTR: |
| 82 | return std::tuple(KernargSegmentPtr ? &KernargSegmentPtr : nullptr, |
| 83 | &AMDGPU::SGPR_64RegClass, |
| 84 | LLT::pointer(AddressSpace: AMDGPUAS::CONSTANT_ADDRESS, SizeInBits: 64)); |
| 85 | case AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR: |
| 86 | return std::tuple(ImplicitArgPtr ? &ImplicitArgPtr : nullptr, |
| 87 | &AMDGPU::SGPR_64RegClass, |
| 88 | LLT::pointer(AddressSpace: AMDGPUAS::CONSTANT_ADDRESS, SizeInBits: 64)); |
| 89 | case AMDGPUFunctionArgInfo::DISPATCH_ID: |
| 90 | return std::tuple(DispatchID ? &DispatchID : nullptr, |
| 91 | &AMDGPU::SGPR_64RegClass, LLT::scalar(SizeInBits: 64)); |
| 92 | case AMDGPUFunctionArgInfo::FLAT_SCRATCH_INIT: |
| 93 | return std::tuple(FlatScratchInit ? &FlatScratchInit : nullptr, |
| 94 | &AMDGPU::SGPR_64RegClass, LLT::scalar(SizeInBits: 64)); |
| 95 | case AMDGPUFunctionArgInfo::DISPATCH_PTR: |
| 96 | return std::tuple(DispatchPtr ? &DispatchPtr : nullptr, |
| 97 | &AMDGPU::SGPR_64RegClass, |
| 98 | LLT::pointer(AddressSpace: AMDGPUAS::CONSTANT_ADDRESS, SizeInBits: 64)); |
| 99 | case AMDGPUFunctionArgInfo::QUEUE_PTR: |
| 100 | return std::tuple(QueuePtr ? &QueuePtr : nullptr, &AMDGPU::SGPR_64RegClass, |
| 101 | LLT::pointer(AddressSpace: AMDGPUAS::CONSTANT_ADDRESS, SizeInBits: 64)); |
| 102 | case AMDGPUFunctionArgInfo::WORKITEM_ID_X: |
| 103 | return std::tuple(WorkItemIDX ? &WorkItemIDX : nullptr, |
| 104 | &AMDGPU::VGPR_32RegClass, LLT::integer(SizeInBits: 32)); |
| 105 | case AMDGPUFunctionArgInfo::WORKITEM_ID_Y: |
| 106 | return std::tuple(WorkItemIDY ? &WorkItemIDY : nullptr, |
| 107 | &AMDGPU::VGPR_32RegClass, LLT::integer(SizeInBits: 32)); |
| 108 | case AMDGPUFunctionArgInfo::WORKITEM_ID_Z: |
| 109 | return std::tuple(WorkItemIDZ ? &WorkItemIDZ : nullptr, |
| 110 | &AMDGPU::VGPR_32RegClass, LLT::integer(SizeInBits: 32)); |
| 111 | } |
| 112 | llvm_unreachable("unexpected preloaded value type" ); |
| 113 | } |
| 114 | |
| 115 | MCRegister |
| 116 | AMDGPUFunctionArgInfo::getPrivateSegmentBufferReg(CallingConv::ID CC) { |
| 117 | return AMDGPU::isChainCC(CC) ? AMDGPU::SGPR48_SGPR49_SGPR50_SGPR51 |
| 118 | : AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3; |
| 119 | } |
| 120 | |
| 121 | AMDGPUFunctionArgInfo AMDGPUFunctionArgInfo::fixedABILayout() { |
| 122 | AMDGPUFunctionArgInfo AI; |
| 123 | AI.PrivateSegmentBuffer |
| 124 | = ArgDescriptor::createRegister(Reg: AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3); |
| 125 | AI.DispatchPtr = ArgDescriptor::createRegister(Reg: AMDGPU::SGPR4_SGPR5); |
| 126 | AI.QueuePtr = ArgDescriptor::createRegister(Reg: AMDGPU::SGPR6_SGPR7); |
| 127 | |
| 128 | // Do not pass kernarg segment pointer, only pass increment version in its |
| 129 | // place. |
| 130 | AI.ImplicitArgPtr = ArgDescriptor::createRegister(Reg: AMDGPU::SGPR8_SGPR9); |
| 131 | AI.DispatchID = ArgDescriptor::createRegister(Reg: AMDGPU::SGPR10_SGPR11); |
| 132 | |
| 133 | // Skip FlatScratchInit/PrivateSegmentSize |
| 134 | AI.WorkGroupIDX = ArgDescriptor::createRegister(Reg: AMDGPU::SGPR12); |
| 135 | AI.WorkGroupIDY = ArgDescriptor::createRegister(Reg: AMDGPU::SGPR13); |
| 136 | AI.WorkGroupIDZ = ArgDescriptor::createRegister(Reg: AMDGPU::SGPR14); |
| 137 | AI.LDSKernelId = ArgDescriptor::createRegister(Reg: AMDGPU::SGPR15); |
| 138 | |
| 139 | const unsigned Mask = 0x3ff; |
| 140 | AI.WorkItemIDX = ArgDescriptor::createRegister(Reg: AMDGPU::VGPR31, Mask); |
| 141 | AI.WorkItemIDY = ArgDescriptor::createRegister(Reg: AMDGPU::VGPR31, Mask: Mask << 10); |
| 142 | AI.WorkItemIDZ = ArgDescriptor::createRegister(Reg: AMDGPU::VGPR31, Mask: Mask << 20); |
| 143 | return AI; |
| 144 | } |
| 145 | |