|
5 | 5 | #include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
6 | 6 | #include "triton/Conversion/TritonGPUToLLVM/Utility.h" |
7 | 7 |
|
| 8 | +using mlir::triton::AMD::DppCtrl; |
8 | 9 | namespace mlir::triton::AMD { |
9 | 10 |
|
10 | 11 | namespace { |
@@ -103,34 +104,207 @@ Value TargetInfo::loadDShared(RewriterBase &rewriter, Location loc, Value ptr, |
103 | 104 |
|
104 | 105 | Value TargetInfo::shuffleXor(RewriterBase &rewriter, Location loc, Value val, |
105 | 106 | int i) const { |
106 | | - return LLVM::AMD::shuffleXor(loc, rewriter, val, i); |
| 107 | + return LLVM::AMD::shuffleXor(loc, rewriter, val, i, getISAFamily()); |
107 | 108 | } |
108 | 109 |
|
109 | 110 | Value TargetInfo::shuffleUp(RewriterBase &rewriter, Location loc, Value val, |
110 | 111 | int i) const { |
111 | | - return LLVM::AMD::shuffleUp(loc, rewriter, val, i); |
| 112 | + return LLVM::AMD::shuffleUp(loc, rewriter, val, i, getISAFamily()); |
112 | 113 | } |
113 | 114 |
|
114 | 115 | Value TargetInfo::shuffleIdx(RewriterBase &rewriter, Location loc, Value val, |
115 | 116 | int i) const { |
116 | | - return LLVM::AMD::shuffleIdx(loc, rewriter, val, i); |
| 117 | + return LLVM::AMD::shuffleIdx(loc, rewriter, val, i, getISAFamily()); |
117 | 118 | } |
118 | 119 |
|
119 | 120 | Value TargetInfo::shuffleIdx(RewriterBase &rewriter, Location loc, Value val, |
120 | 121 | Value i) const { |
121 | | - return LLVM::AMD::shuffleIdx(loc, rewriter, val, i); |
| 122 | + return LLVM::AMD::shuffleIdx(loc, rewriter, val, i, getISAFamily()); |
122 | 123 | } |
123 | 124 |
|
124 | 125 | Value TargetInfo::programId(RewriterBase &rewriter, Location loc, |
125 | 126 | ModuleOp moduleOp, int axis) const { |
126 | 127 | return LLVM::AMD::llGetPid(loc, rewriter, moduleOp, axis); |
127 | 128 | } |
128 | 129 |
|
| 130 | +// Cast and sext values into specific-length int to meet the requirements of |
| 131 | +// instructions like UpdateDpp or readlane if necessary. |
| 132 | +static inline Type castToAndSExtInt(RewriterBase &rewriter, Location loc, |
| 133 | + Value &val, Type fromType, |
| 134 | + unsigned toBits) { |
| 135 | + unsigned originalBits = fromType.getIntOrFloatBitWidth(); |
| 136 | + Type toType = fromType; |
| 137 | + |
| 138 | + if (!fromType.isIntOrIndex()) { |
| 139 | + val = bitcast(val, int_ty(originalBits)); |
| 140 | + toType = int_ty(originalBits); |
| 141 | + } |
| 142 | + |
| 143 | + if (originalBits < toBits) { |
| 144 | + val = sext(int_ty(toBits), val); |
| 145 | + toType = int_ty(toBits); |
| 146 | + } |
| 147 | + |
| 148 | + return toType; |
| 149 | +} |
| 150 | + |
| 151 | +// Trunc the value to specific length and then cast it to given type if |
| 152 | +// necessary. This function is typically used in conjunction with |
| 153 | +// castToAndSExtInt. |
| 154 | +static inline Value truncAndCastFromInt(RewriterBase &rewriter, Location loc, |
| 155 | + Value val, Type valType, |
| 156 | + unsigned fromBits) { |
| 157 | + unsigned originalBits = valType.getIntOrFloatBitWidth(); |
| 158 | + Value toVal = val; |
| 159 | + |
| 160 | + if (originalBits < fromBits) { |
| 161 | + toVal = trunc(int_ty(originalBits), toVal); |
| 162 | + } |
| 163 | + |
| 164 | + if (!valType.isIntOrIndex()) { |
| 165 | + toVal = bitcast(toVal, valType); |
| 166 | + } |
| 167 | + |
| 168 | + return toVal; |
| 169 | +} |
| 170 | + |
129 | 171 | bool TargetInfo::warpReduce(RewriterBase &rewriter, Location loc, |
130 | 172 | SmallVector<Value> &acc, triton::ReduceOp op, |
131 | 173 | unsigned numLaneToReduce, |
132 | 174 | unsigned interleave) const { |
133 | | - return false; |
| 175 | + if (numLaneToReduce != 64) |
| 176 | + return false; |
| 177 | + |
| 178 | + if (auto family = getISAFamily(); |
| 179 | + family != ISAFamily::CDNA3 && family != ISAFamily::CDNA2) { |
| 180 | + return false; |
| 181 | + } |
| 182 | + |
| 183 | + Operation *reduxOp = op.getSingleCombiner(); |
| 184 | + if (!reduxOp) |
| 185 | + return false; |
| 186 | + |
| 187 | + auto createDppReduxOpWithBoundCtrl = [&](Type valType, Value &src, |
| 188 | + uint32_t dppCtrl, int rowMask, |
| 189 | + int bankMask) -> Value { |
| 190 | + // DPP has limited support for data types, so here we need to |
| 191 | + // cast non-integer types or integer types shorter than 32 bits |
| 192 | + // to int32, except for fp32. |
| 193 | + Type actualType = valType; |
| 194 | + if (!valType.isF32()) { |
| 195 | + actualType = castToAndSExtInt(rewriter, loc, src, valType, 32); |
| 196 | + } |
| 197 | + |
| 198 | + Value dppResult = |
| 199 | + rewriter |
| 200 | + .create<ROCDL::DPPUpdateOp>(loc, actualType, src, src, |
| 201 | + rewriter.getI32IntegerAttr(dppCtrl), |
| 202 | + rewriter.getI32IntegerAttr(rowMask), |
| 203 | + rewriter.getI32IntegerAttr(bankMask), |
| 204 | + rewriter.getBoolAttr(true)) |
| 205 | + .getRes(); |
| 206 | + |
| 207 | + if (!valType.isF32()) { |
| 208 | + src = truncAndCastFromInt(rewriter, loc, src, valType, 32); |
| 209 | + dppResult = truncAndCastFromInt(rewriter, loc, dppResult, valType, 32); |
| 210 | + } |
| 211 | + |
| 212 | + IRMapping mapping; |
| 213 | + mapping.map(reduxOp->getOperand(0), src); |
| 214 | + mapping.map(reduxOp->getOperand(1), dppResult); |
| 215 | + return rewriter.clone(*reduxOp, mapping)->getResult(0); |
| 216 | + }; |
| 217 | + |
| 218 | + for (int i = 0; i < acc.size(); i++) { |
| 219 | + Value buf; |
| 220 | + auto valType = acc[i].getType(); |
| 221 | + |
| 222 | + /* |
| 223 | + Here's the implementation of full-wavefront reduction using dpp. |
| 224 | + https://gpuopen.com/learn/amd-gcn-assembly-cross-lane-operations/ |
| 225 | +
|
| 226 | + Each step has a v_mov_dpp instruction following the redux op. In |
| 227 | + some cases, the lower-level compiler could merge them into single |
| 228 | + instruction. For example, v_mov_dpp + max => v_max_dpp. |
| 229 | +
|
| 230 | + For gfx9, we have 64 threads per warp. These 64 threads are arranged |
| 231 | + into 4 rows, with each row being 16 threads. Each 16 threads are arranged |
| 232 | + further into 4 banks, with each bank being 4 threads. Overall it's in a |
| 233 | + (row, bank, thread) structure. When shuffling, we use row/bank mask to |
| 234 | + indicate which row/bank to participate. Then modifier like row_shr and |
| 235 | + row_bcast means exact data movement schemes. In the following |
| 236 | + instructions, taking row 0 as an example: |
| 237 | +
|
| 238 | + Step 1: Right shift for 8 lanes. |
| 239 | + lane 8-15 = redux(lane 0-7, lane 8-15) |
| 240 | +
|
| 241 | + Step 2: Right shift for 4 lanes. |
| 242 | + lane 12-15 = redux(lane 8-11, lane 12-15) |
| 243 | +
|
| 244 | + Step 3: Right shift for 2 lanes. |
| 245 | + lane 14-15 = redux(lane 12-13, lane 14-15) |
| 246 | +
|
| 247 | + Step 4: Right shift for 1 lane. |
| 248 | + lane 15 = redux(lane 14, lane 15) |
| 249 | +
|
| 250 | + Step 5: Broadcast lane 15 of each row to all the lanes of its next row. |
| 251 | + lane 16-31 = redux(lane 15, lane 16-31) |
| 252 | +
|
| 253 | + Step 6: Broadcast lane 31 to lane 32-63. |
| 254 | + lane 32-63 = redux(lane 31, lane 32-63) |
| 255 | +
|
| 256 | + Now the reduction result is stored in lane 63. |
| 257 | +
|
| 258 | + Step 7: Read the reduction result from lane 63 and broadcast with |
| 259 | + readlane. |
| 260 | + */ |
| 261 | + |
| 262 | + const int allRows = 0xf; |
| 263 | + const int allBanks = 0xf; |
| 264 | + |
| 265 | + const uint32_t dppCtrlRowShr = static_cast<uint32_t>(DppCtrl::ROW_SHR0); |
| 266 | + |
| 267 | + // row_shr:8 |
| 268 | + buf = createDppReduxOpWithBoundCtrl(valType, acc[i], 8 + dppCtrlRowShr, |
| 269 | + allRows, allBanks); |
| 270 | + |
| 271 | + // row_shr:4 |
| 272 | + buf = createDppReduxOpWithBoundCtrl(valType, buf, 4 + dppCtrlRowShr, |
| 273 | + allRows, allBanks); |
| 274 | + |
| 275 | + // row_shr:2 |
| 276 | + buf = createDppReduxOpWithBoundCtrl(valType, buf, 2 + dppCtrlRowShr, |
| 277 | + allRows, allBanks); |
| 278 | + |
| 279 | + // row_shr:1 |
| 280 | + buf = createDppReduxOpWithBoundCtrl(valType, buf, 1 + dppCtrlRowShr, |
| 281 | + allRows, allBanks); |
| 282 | + |
| 283 | + // row_bcast:15 row_mask:0xa |
| 284 | + buf = createDppReduxOpWithBoundCtrl( |
| 285 | + valType, buf, static_cast<uint32_t>(DppCtrl::BCAST15), 0xa, allBanks); |
| 286 | + |
| 287 | + // row_bcast:31 |
| 288 | + buf = createDppReduxOpWithBoundCtrl(valType, buf, |
| 289 | + static_cast<uint32_t>(DppCtrl::BCAST31), |
| 290 | + allRows, allBanks); |
| 291 | + |
| 292 | + // Similarly, we need to cast data types for readlane instruction. |
| 293 | + Type actualType = castToAndSExtInt(rewriter, loc, buf, valType, 16); |
| 294 | + |
| 295 | + // Get reduction result from lane 63 |
| 296 | + std::string intrinsic = "llvm.amdgcn.readlane"; |
| 297 | + Value result = |
| 298 | + LLVM::createLLVMIntrinsicCallOp(rewriter, loc, intrinsic, actualType, |
| 299 | + ValueRange{buf, i32_val(63)}) |
| 300 | + ->getResult(0); |
| 301 | + |
| 302 | + result = truncAndCastFromInt(rewriter, loc, result, valType, 16); |
| 303 | + |
| 304 | + acc[i] = result; |
| 305 | + } |
| 306 | + |
| 307 | + return true; |
134 | 308 | } |
135 | 309 |
|
136 | 310 | void TargetInfo::printfImpl(Value formatStrStart, int formatStrByteCount, |
|
0 commit comments