| 1 | //===-- AMDGPUInstrInfo.h - AMDGPU Instruction Information ------*- C++ -*-===// |
| 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 | /// \file |
| 10 | /// Contains the definition of a TargetInstrInfo class that is common |
| 11 | /// to all AMD GPUs. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUINSTRINFO_H |
| 16 | #define LLVM_LIB_TARGET_AMDGPU_AMDGPUINSTRINFO_H |
| 17 | |
| 18 | #include "Utils/AMDGPUBaseInfo.h" |
| 19 | |
| 20 | namespace llvm { |
| 21 | |
| 22 | class MachineMemOperand; |
| 23 | class MachineInstr; |
| 24 | |
| 25 | namespace AMDGPU { |
| 26 | |
| 27 | bool isUniformMMO(const MachineMemOperand *MMO); |
| 28 | |
| 29 | /// Return the intrinsic ID for opcodes with the G_AMDGPU_INTRIN_ prefix. |
| 30 | /// |
| 31 | /// These opcodes have an Intrinsic::ID operand similar to a GIntrinsic. But |
| 32 | /// they are not actual instances of GIntrinsics, so we cannot use |
| 33 | /// GIntrinsic::getIntrinsicID() on them. |
| 34 | Intrinsic::ID getIntrinsicID(const MachineInstr &I); |
| 35 | |
| 36 | struct RsrcIntrinsic { |
| 37 | unsigned Intr; |
| 38 | uint8_t RsrcArg; |
| 39 | bool IsImage; |
| 40 | }; |
| 41 | const RsrcIntrinsic *lookupRsrcIntrinsic(unsigned Intr); |
| 42 | |
| 43 | #define GET_BufferIntrinsicKind_DECL |
| 44 | #include "AMDGPUGenSearchableTables.inc" |
| 45 | |
| 46 | /// The argument layout of a buffer intrinsic. Argument positions are indices |
| 47 | /// into the IR call's argument list, with NoArg for arguments the intrinsic |
| 48 | /// doesn't have. Use getMIROperandIdx() / getSDOperandIdx() to convert them to |
| 49 | /// operand indices. See AMDGPUBufferIntrinsic in IntrinsicsAMDGPU.td. |
| 50 | struct BufferIntrinsicInfo { |
| 51 | /// The position of absent arguments, (uint8_t)-1. |
| 52 | static constexpr uint8_t NoArg = 255; |
| 53 | |
| 54 | unsigned Intr; |
| 55 | BufferIntrinsicKind Kind; |
| 56 | uint8_t RsrcArg; |
| 57 | uint8_t VDataArg; |
| 58 | uint8_t SizeArg; |
| 59 | uint8_t VIndexArg; |
| 60 | uint8_t VOffsetArg; |
| 61 | uint8_t SOffsetArg; |
| 62 | uint8_t ImmOffsetArg; |
| 63 | uint8_t FormatArg; |
| 64 | uint8_t AuxArg; |
| 65 | /// The data is converted according to the format in the resource (the |
| 66 | /// *_format intrinsics). |
| 67 | bool IsFormatFromRsrc; |
| 68 | /// Loads to LDS that are asynchronous. |
| 69 | bool IsAsync; |
| 70 | |
| 71 | private: |
| 72 | static std::optional<unsigned> getArg(uint8_t Arg) { |
| 73 | if (Arg == NoArg) |
| 74 | return std::nullopt; |
| 75 | return Arg; |
| 76 | } |
| 77 | |
| 78 | public: |
| 79 | bool isStruct() const { return VIndexArg != NoArg; } |
| 80 | bool isTyped() const { return FormatArg != NoArg; } |
| 81 | /// Whether the data is converted according to the format in the resource. |
| 82 | bool isFormatFromRsrc() const { return IsFormatFromRsrc; } |
| 83 | /// Whether the data is converted according to a format, drawn either from |
| 84 | /// the resource descriptor (untyped/MUBUF _format instructions) or from an |
| 85 | /// explicit argument (tbuffer/MFBUF instructions). |
| 86 | bool isFormat() const { return IsFormatFromRsrc || isTyped(); } |
| 87 | |
| 88 | /// The stored value for stores and atomics. |
| 89 | std::optional<unsigned> getVDataArg() const { return getArg(Arg: VDataArg); } |
| 90 | /// The comparison value for compare-and-swap atomics. |
| 91 | std::optional<unsigned> getCmpArg() const { |
| 92 | if (Kind != BufferKindAtomicCmpSwap) |
| 93 | return std::nullopt; |
| 94 | return VDataArg + 1; |
| 95 | } |
| 96 | /// The LDS destination of loads to LDS. |
| 97 | std::optional<unsigned> getLDSPtrArg() const { |
| 98 | if (Kind != BufferKindLoadToLDS) |
| 99 | return std::nullopt; |
| 100 | return RsrcArg + 1; |
| 101 | } |
| 102 | /// The access size of loads to LDS and prefetches. |
| 103 | std::optional<unsigned> getSizeArg() const { return getArg(Arg: SizeArg); } |
| 104 | std::optional<unsigned> getVIndexArg() const { return getArg(Arg: VIndexArg); } |
| 105 | /// The voffset, which scalar loads and prefetches don't have. |
| 106 | std::optional<unsigned> getVOffsetArg() const { return getArg(Arg: VOffsetArg); } |
| 107 | /// The soffset, which is the only offset of scalar loads and prefetches. |
| 108 | std::optional<unsigned> getSOffsetArg() const { return getArg(Arg: SOffsetArg); } |
| 109 | /// The main offset: the voffset if there is one, and the soffset otherwise. |
| 110 | unsigned getOffsetArg() const { |
| 111 | assert((VOffsetArg != NoArg || SOffsetArg != NoArg) && |
| 112 | "buffer intrinsic without an offset" ); |
| 113 | return VOffsetArg != NoArg ? VOffsetArg : SOffsetArg; |
| 114 | } |
| 115 | /// The immediate offset of loads to LDS. |
| 116 | std::optional<unsigned> getImmOffsetArg() const { |
| 117 | return getArg(Arg: ImmOffsetArg); |
| 118 | } |
| 119 | std::optional<unsigned> getFormatArg() const { return getArg(Arg: FormatArg); } |
| 120 | /// The auxiliary (cache policy) argument. |
| 121 | std::optional<unsigned> getAuxArg() const { return getArg(Arg: AuxArg); } |
| 122 | }; |
| 123 | const BufferIntrinsicInfo *lookupBufferIntrinsic(unsigned Intr); |
| 124 | |
| 125 | /// Convert IR argument index \p Arg of the intrinsic called by \p MI, a |
| 126 | /// G_INTRINSIC* instruction, into an operand index of \p MI. |
| 127 | unsigned getMIROperandIdx(const MachineInstr &MI, unsigned Arg); |
| 128 | |
| 129 | /// Convert IR argument index \p Arg of an intrinsic into an operand index of |
| 130 | /// the INTRINSIC_* SelectionDAG node calling it. \p HasChain is false for |
| 131 | /// INTRINSIC_WO_CHAIN. |
| 132 | constexpr unsigned getSDOperandIdx(unsigned Arg, bool HasChain = true) { |
| 133 | // The chain, if any, and the intrinsic ID precede the arguments. |
| 134 | return Arg + (HasChain ? 2 : 1); |
| 135 | } |
| 136 | |
| 137 | struct ImageDimIntrinsicInfo { |
| 138 | unsigned Intr; |
| 139 | unsigned BaseOpcode; |
| 140 | unsigned AtomicNoRetBaseOpcode; |
| 141 | MIMGDim Dim; |
| 142 | |
| 143 | uint8_t NumOffsetArgs; |
| 144 | uint8_t NumBiasArgs; |
| 145 | uint8_t NumZCompareArgs; |
| 146 | uint8_t NumGradients; |
| 147 | uint8_t NumDmask; |
| 148 | uint8_t NumData; |
| 149 | uint8_t NumVAddrs; |
| 150 | uint8_t NumArgs; |
| 151 | |
| 152 | uint8_t DMaskIndex; |
| 153 | uint8_t VAddrStart; |
| 154 | uint8_t OffsetIndex; |
| 155 | uint8_t BiasIndex; |
| 156 | uint8_t ZCompareIndex; |
| 157 | uint8_t GradientStart; |
| 158 | uint8_t CoordStart; |
| 159 | uint8_t LodIndex; |
| 160 | uint8_t MipIndex; |
| 161 | uint8_t VAddrEnd; |
| 162 | uint8_t RsrcIndex; |
| 163 | uint8_t SampIndex; |
| 164 | uint8_t UnormIndex; |
| 165 | uint8_t TexFailCtrlIndex; |
| 166 | uint8_t CachePolicyIndex; |
| 167 | |
| 168 | uint8_t BiasTyArg; |
| 169 | uint8_t GradientTyArg; |
| 170 | uint8_t CoordTyArg; |
| 171 | }; |
| 172 | const ImageDimIntrinsicInfo *getImageDimIntrinsicInfo(unsigned Intr); |
| 173 | |
| 174 | const ImageDimIntrinsicInfo * |
| 175 | getImageDimIntrinsicByBaseOpcode(unsigned BaseOpcode, unsigned Dim); |
| 176 | |
| 177 | } // end AMDGPU namespace |
| 178 | } // End llvm namespace |
| 179 | |
| 180 | #endif |
| 181 | |