| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
from rocisa import countInstruction, countGlobalRead, countSMemLoad, findInstCount |
| 27 |
from rocisa.asmpass import getActFuncModuleName, getActFuncBranchModuleName |
| 28 |
from rocisa.code import KernelBody, Label, Macro, Module, RegSet, SrdUpperValue, \ |
| 29 |
StructuredModule, TextBlock, ValueEndif, ValueIf, ValueSet, SignatureBase |
| 30 |
from rocisa.container import DSModifiers, SDWAModifiers, VOP3PModifiers, \ |
| 31 |
MUBUFModifiers, SMEMModifiers, EXEC, VCC, RegisterContainer, \ |
| 32 |
DPPModifiers, vgpr, sgpr, accvgpr, mgpr, ContinuousRegister, \ |
| 33 |
HWRegContainer, GLOBALModifiers |
| 34 |
from rocisa.instruction import SGetPositivePCOffset, SLongBranchPositive, SCLongBranchScc0, SCLongBranchScc1, SCLongBranchVccnz, \ |
| 35 |
SMulInt64to32, VCvtBF16toFP32 |
| 36 |
from rocisa.functions import vectorStaticDivide, vectorStaticRemainder, vectorUInt32CeilDivideAndRemainder, \ |
| 37 |
vectorStaticDivideAndRemainder, scalarStaticDivideAndRemainder, scalarStaticCeilDivide, \ |
| 38 |
scalarStaticRemainder, scalarUInt32DivideAndRemainder, sMagicDiv, vectorStaticMultiply, \ |
| 39 |
vectorStaticMultiplyAdd, scalarStaticMultiply64, BranchIfZero, BranchIfNotZero, DSInit, \ |
| 40 |
ArgumentLoader |
| 41 |
from rocisa.enum import InstType, SelectBit |
| 42 |
from rocisa.macro import MacroVMagicDiv, PseudoRandomGenerator |
| 43 |
from . import CUSTOM_KERNEL_PATH |
| 44 |
from rocisa.instruction import BranchInstruction, BufferLoadB128, BufferLoadB32, \ |
| 45 |
BufferLoadB64, BufferLoadD16B16, BufferLoadD16HIB16, BufferLoadD16HIU8, \ |
| 46 |
BufferLoadD16U8, BufferStoreB128, BufferStoreB16, BufferStoreB32, BufferStoreB64, \ |
| 47 |
BufferStoreB8, BufferStoreD16HIB16, CommonInstruction, DSBPermuteB32, DSLoadB128, \ |
| 48 |
DSLoadB16, DSLoadB32, DSLoadB64, DSLoadU16, DSStoreB128, DSStoreB16, DSStoreB32, \ |
| 49 |
DSStoreB64, DSStoreB8, DSStoreInstruction, FlatLoadB128, FlatLoadB32, FlatLoadB64, \ |
| 50 |
FlatLoadD16B16, FlatLoadD16HIB16, FlatStoreB128, FlatStoreB32, FlatStoreB64, \ |
| 51 |
FlatStoreD16B16, FlatStoreD16HIB16, MFMAInstruction, MUBUFReadInstruction, \ |
| 52 |
MacroInstruction, SAShiftRightI32, SAbsI32, SAddCU32, SAddI32, SAddU32, SAndB32, \ |
| 53 |
SAndB64, SAndN2B32, SAtomicDec, SBarrier, SBfmB32, SBitcmp1B32, SBranch, SCBranchSCC0, \ |
| 54 |
SCBranchSCC1, SCBranchVCCNZ, SCBranchVCCZ, SCMovB32, SCSelectB32, SCmpEQI32, \ |
| 55 |
SCmpEQU32, SCmpEQU64, SCmpGeI32, SCmpGeU32, SCmpGtI32, SCmpGtU32, SCmpKEQU32, \ |
| 56 |
SCmpKGeU32, SCmpKGtU32, SCmpKLGU32, SCmpLeI32, SCmpLeU32, SCmpLgU32, SCmpLtU32, SCmpLtI32, \ |
| 57 |
SEndpgm, SFf1B32, SGetRegB32, SLShiftLeft2AddU32, SLShiftLeftB32, SLShiftLeftB64, SLShiftRightB32, \ |
| 58 |
SLShiftRightB64, SLoadB32, SLoadB64, SMFMAInstruction, SMemLoadInstruction, SMinI32, \ |
| 59 |
SMinU32, SMovB32, SMovB64, SMulHIU32, SMulI32, SNop, SOrB32, SOrSaveExecB32, \ |
| 60 |
SOrSaveExecB64, SSExtI16toI32, SSetPCB64, SSetRegIMM32B32, SSetPrior, SSubBU32, SSubI32, SSubU32, \ |
| 61 |
SWaitCnt, SWaitAlu, SXorB32, VAShiftRightI32, VAccvgprReadB32, VAccvgprWrite, VAccvgprWriteB32, \ |
| 62 |
VAdd3U32, VAddCCOU32, VAddCOU32, VAddF32, VAddF64, VAddLShiftLeftU32, VAddU32, VAndB32, \ |
| 63 |
VBfeU32, VCmpEQI32, VCmpEQU32, VCmpGEI32, VCmpGEU32, VCmpGtU32, VCmpLeI32, VCmpLtI32, \ |
| 64 |
VCmpLtU32, VCmpUF32, VCmpXGeU32, VCmpXLtU32, VCmpXLtU64, VCndMaskB32, VCvtF16toF32, \ |
| 65 |
VCvtF32toF16, VCvtFP8toF32, VCvtInstruction, VCvtPkF32toBF16, VCvtPkF32toBF8, \ |
| 66 |
VCvtPkF32toFP8, VCvtPkFP8toF32, VCvtSRF32toBF8, VCvtSRF32toFP8, VCvtScaleFP8toF16, \ |
| 67 |
VCvtScalePkF16toBF8, VCvtScalePkF16toFP8, VCvtScalePkFP8toF16, VLShiftLeftB32, \ |
| 68 |
VLShiftLeftB64, VLShiftRightB32, VMadU32U24, VMaxF32, VMinI32, VMovB32, VMovB64, VMulF32, \ |
| 69 |
VMulHIU32, VMulLOU32, VMulPKF32S, VMulU32U24, VNotB32, VOrB32, VPackF16toB32, \ |
| 70 |
VPrngB32, VReadfirstlaneB32, VSubF32, VSubI32, VSubU32, VXorB32, GlobalLoadTR8B64, GlobalLoadTR16B128 |
| 71 |
|
| 72 |
from .Component import Component |
| 73 |
from .KernelWriterModules import * |
| 74 |
from .SolutionStructs import isPackedIndex |
| 75 |
from .AsmStoreState import StoreState, VectorDataTypes |
| 76 |
from .Activation import ActivationType |
| 77 |
from .CustomKernels import isCustomKernelConfig |
| 78 |
from .Common import roundUp, log2, ceilDivide |
| 79 |
from Tensile.Common import print2, printExit, printWarning, INDEX_CHARS, DebugConfig, DataDirection |
| 80 |
from Tensile.Common.DataType import DataType |
| 81 |
from Tensile.Common.RegisterPool import RegisterPool, allocTmpGpr, allocTmpGprList |
| 82 |
|
| 83 |
from Tensile.KernelWriter import KernelWriter |
| 84 |
from Tensile.SolutionStructs.Naming import getKernelFileBase |
| 85 |
from Tensile.Toolchain.Component import Assembler |
| 86 |
|
| 87 |
from math import ceil, log |
| 88 |
from copy import deepcopy |
| 89 |
from dataclasses import dataclass, field |
| 90 |
from functools import lru_cache |
| 91 |
from typing import List, NamedTuple, Optional, Tuple, Union |
| 92 |
|
| 93 |
import os |
| 94 |
@dataclass |
| 95 |
class TailOptParams: |
| 96 |
idx: int = 0 |
| 97 |
behavior: str = "" |
| 98 |
jumpLabel: Label = None |
| 99 |
kLabelsList: List[Label] = None |
| 100 |
tmpVgpr: int = None |
| 101 |
kSgpr: int = None |
| 102 |
vgprSlot: List[int] = None |
| 103 |
loadNum: int = 0 |
| 104 |
periodParam: List[int] = None |
| 105 |
preDirectToLdsLoads: int = 0 |
| 106 |
firstLoop: int = 0 |
| 107 |
finalLoop: int = 0 |
| 108 |
|
| 109 |
@dataclass |
| 110 |
class GprInfo: |
| 111 |
addrVgpr: List[str] = field(default_factory=list) |
| 112 |
offset: List[str] = field(default_factory=list) |
| 113 |
|
| 114 |
@dataclass |
| 115 |
class GlobalReadGprRecord: |
| 116 |
a: GprInfo = field(default_factory=GprInfo) |
| 117 |
b: GprInfo = field(default_factory=GprInfo) |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
class KernelWriterAssembly(KernelWriter): |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
def __init__( |
| 128 |
self, |
| 129 |
assembler: Assembler, |
| 130 |
debugConfig: DebugConfig, |
| 131 |
): |
| 132 |
super(KernelWriterAssembly, self).__init__(assembler, debugConfig) |
| 133 |
self.globalread_gpr_record = GlobalReadGprRecord() |
| 134 |
|
| 135 |
def _getCustomKernelSource(self, kernel, CustomKernelDirectory): |
| 136 |
kernelName = getKernelFileBase(self.debugConfig.splitGSU, kernel) |
| 137 |
with open(os.path.join(CustomKernelDirectory, (kernelName + ".s"))) as f: |
| 138 |
rocmVersion = self.assembler.rocm_version |
| 139 |
if not (rocmVersion.major >= 6 and rocmVersion.patch >= 32650): |
| 140 |
code = [] |
| 141 |
for line in f.readlines(): |
| 142 |
if "amdhsa_user_sgpr_kernarg_preload" not in line: |
| 143 |
code.append(line) |
| 144 |
code = "".join(code) |
| 145 |
else: |
| 146 |
code = f.read() |
| 147 |
|
| 148 |
self.tPA = {} |
| 149 |
self.tPB = {} |
| 150 |
self.states.kernel = kernel |
| 151 |
self.states.language = "ASM" |
| 152 |
self.states.version = kernel["ISA"] |
| 153 |
|
| 154 |
return code |
| 155 |
|
| 156 |
|
| 157 |
def getSourceFileString(self, kernel) -> Tuple[int, str]: |
| 158 |
assert kernel["KernelLanguage"] == "Assembly" |
| 159 |
|
| 160 |
if kernel.duplicate: |
| 161 |
self.language = "ASM" |
| 162 |
return (0, "") |
| 163 |
|
| 164 |
try: |
| 165 |
code = self._getCustomKernelSource(kernel, CUSTOM_KERNEL_PATH) if isCustomKernelConfig(kernel) else self._getKernelSource(kernel) |
| 166 |
errcode = 0 |
| 167 |
except RuntimeError as e: |
| 168 |
printWarning(f"Failed to generate assembly source code for {kernel}: {e}") |
| 169 |
code = "" |
| 170 |
errcode = -2 |
| 171 |
return (errcode, code) |
| 172 |
|
| 173 |
def getSgprOccupancy(self, sgprs): |
| 174 |
return self.states.regCaps["PhysicalMaxSgpr"]//sgprs |
| 175 |
|
| 176 |
def getVgprOccupancy(self, numThreads, vgprs, doubleVgpr=False): |
| 177 |
multiplier = int(ceil(max(numThreads, 256) / 256.0)) |
| 178 |
maxOccupancy = self.consts.maxOccupancy//multiplier |
| 179 |
|
| 180 |
vgprAllocateAligned = 4 if not doubleVgpr else 8 |
| 181 |
totalVgprs = self.states.regCaps["MaxVgpr"] if not doubleVgpr else self.states.regCaps["MaxVgpr"]*2 |
| 182 |
vgprsAligned = int(ceil(vgprs/vgprAllocateAligned))*vgprAllocateAligned |
| 183 |
vgprsAligned *= multiplier |
| 184 |
|
| 185 |
if vgprsAligned > totalVgprs: return 0 |
| 186 |
elif vgprsAligned < 1: return maxOccupancy |
| 187 |
occupancy = min(totalVgprs//vgprsAligned, maxOccupancy) |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
return occupancy |
| 192 |
|
| 193 |
|
| 194 |
def getOccupancy(self, numThreads, vgprs, sgprs, ldsSize, accvgprs=0, doubleVgpr=False): |
| 195 |
|
| 196 |
deviceLdsSize = self.states.archCaps["DeviceLDS"] |
| 197 |
ldsLimitedOccupancy = self.getLdsLimitedOccupancy(deviceLdsSize, ldsSize) |
| 198 |
|
| 199 |
if not doubleVgpr: |
| 200 |
vgprLimitedOccupancy = self.getVgprOccupancy(numThreads, vgprs, doubleVgpr) |
| 201 |
accvgprLimitedOccupancy = self.getVgprOccupancy(numThreads, accvgprs, doubleVgpr) |
| 202 |
else: |
| 203 |
vgprLimitedOccupancy = self.getVgprOccupancy(numThreads, ceil(vgprs/8)*8+accvgprs, doubleVgpr) |
| 204 |
accvgprLimitedOccupancy = vgprLimitedOccupancy |
| 205 |
sgprLimitedOccupancy = self.getSgprOccupancy(sgprs) |
| 206 |
|
| 207 |
return min(ldsLimitedOccupancy, vgprLimitedOccupancy, accvgprLimitedOccupancy, sgprLimitedOccupancy) |
| 208 |
|
| 209 |
|
| 210 |
def getMaxRegsForOccupancy(self, numThreads, vgprs, sgprs, ldsSize, accvgprs=0, doubleVgpr=False): |
| 211 |
lastVgprs = vgprs |
| 212 |
considerAccVgprs = 0 if not doubleVgpr else accvgprs |
| 213 |
totalVgprs = self.states.regCaps["MaxVgpr"] if not doubleVgpr else self.states.regCaps["MaxVgpr"]*2 |
| 214 |
|
| 215 |
initOccupancy = self.getOccupancy(numThreads, vgprs, sgprs, ldsSize, accvgprs, doubleVgpr) |
| 216 |
if initOccupancy == 0: return lastVgprs, 1 |
| 217 |
|
| 218 |
def getVgpr(vgpr, doubleVgpr): |
| 219 |
return vgpr if not doubleVgpr else ceil(vgpr/8)*8 |
| 220 |
while (getVgpr(vgprs, doubleVgpr) + considerAccVgprs) < totalVgprs and vgprs < self.states.regCaps["MaxVgpr"]: |
| 221 |
vgprs += 1 |
| 222 |
if self.getVgprOccupancy(numThreads, getVgpr(vgprs, doubleVgpr) + considerAccVgprs, doubleVgpr) >= initOccupancy: |
| 223 |
lastVgprs = vgprs |
| 224 |
next |
| 225 |
else: |
| 226 |
break |
| 227 |
|
| 228 |
return lastVgprs, initOccupancy |
| 229 |
|
| 230 |
@staticmethod |
| 231 |
def getLdsLimitedOccupancy(deviceLdsSize, ldsSize): |
| 232 |
|
| 233 |
|
| 234 |
ldsSize = min(ldsSize + 255, deviceLdsSize) & 0xffffff00 |
| 235 |
|
| 236 |
ldsLimitedOccupancy = deviceLdsSize//ldsSize |
| 237 |
return ldsLimitedOccupancy |
| 238 |
|
| 239 |
@staticmethod |
| 240 |
def getLdsSize(kernel): |
| 241 |
ldsSize = kernel["LdsNumBytes"] |
| 242 |
return ldsSize |
| 243 |
|
| 244 |
|
| 245 |
def sizeRef(self, idx): |
| 246 |
""" |
| 247 |
Return sgpr() or const with the specified size |
| 248 |
See above definitions for how these are mapped to Free or Sum sizes |
| 249 |
based on the problem definition. |
| 250 |
""" |
| 251 |
idxChar= INDEX_CHARS[idx] |
| 252 |
return sgpr("Size%s"%idxChar) |
| 253 |
|
| 254 |
def loopChar(self, kernel, loopIdx): |
| 255 |
loopDim = kernel["ProblemType"]["IndicesSummation"][loopIdx] |
| 256 |
return INDEX_CHARS[loopDim] |
| 257 |
|
| 258 |
def loopSizeRef(self, kernel, loopIdx): |
| 259 |
loopDim = kernel["ProblemType"]["IndicesSummation"][loopIdx] |
| 260 |
return self.sizeRef(loopDim) |
| 261 |
|
| 262 |
def loopCounterName(self, kernel, loopIdx): |
| 263 |
return "LoopCounter%s"%(self.loopChar(kernel, loopIdx)) |
| 264 |
|
| 265 |
def loopCounter(self, kernel, loopIdx): |
| 266 |
""" |
| 267 |
Return loopCounter for loopIdx wrapped in "SGPR" syntax |
| 268 |
loop idx is 0...unrollIdx |
| 269 |
""" |
| 270 |
return sgpr(self.loopCounterName(kernel,loopIdx)) |
| 271 |
|
| 272 |
def checkLastIter(self, kernel, comment="at last iteration?"): |
| 273 |
""" Return last iteration of unroll loop. """ |
| 274 |
return SCmpEQU32(src0=self.loopCounter(kernel, self.states.unrollIdx), src1=0, comment=comment) |
| 275 |
|
| 276 |
def isConstUnitStride(self, stride): |
| 277 |
if isinstance(stride, RegisterContainer): |
| 278 |
return False |
| 279 |
return stride.startswith("const") |
| 280 |
|
| 281 |
|
| 282 |
def strideRef(self, tc, dim): |
| 283 |
""" |
| 284 |
Return sgpr with specified stride or define starting with const if constant. |
| 285 |
dim is index 0...max indices and is in global index space. |
| 286 |
""" |
| 287 |
problemType = self.states.kernel["ProblemType"] |
| 288 |
if tc in ['A','B', "Metadata"]: |
| 289 |
if not problemType["UseInitialStridesAB"] and \ |
| 290 |
dim == problemType["IndexAssignments%s"%tc][0]: |
| 291 |
return ("constStride%s%s"%(tc,self.states.indexChars[dim])) |
| 292 |
else: |
| 293 |
return sgpr("Stride%s%s"%(tc,self.states.indexChars[dim])) |
| 294 |
elif tc in ['E','D','C','TD']: |
| 295 |
if not problemType["UseInitialStridesCD"] and dim == 0: |
| 296 |
return ("constStride%s%s"%(tc,self.states.indexChars[dim])) |
| 297 |
else: |
| 298 |
return sgpr("Stride%s%s"%(tc,self.states.indexChars[dim])) |
| 299 |
else: |
| 300 |
raise ValueError("unexpected tensorChar='%s' in stride function"%tc) |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
def findMemoryInstructionForWidthStride(self, width, strides, combine, \ |
| 306 |
instructions): |
| 307 |
for i in range(0, len(instructions)): |
| 308 |
instruction = instructions[i] |
| 309 |
numAddresses = instruction.numAddresses |
| 310 |
numOffsets = instruction.numOffsets |
| 311 |
offsetMultiplier = instruction.offsetMultiplier |
| 312 |
blockWidth = instruction.blockWidth |
| 313 |
valid = True |
| 314 |
if width < blockWidth: |
| 315 |
valid = False |
| 316 |
if combine: |
| 317 |
if numOffsets > 0: |
| 318 |
for stride in strides: |
| 319 |
if stride % offsetMultiplier != 0: |
| 320 |
valid = False |
| 321 |
else: |
| 322 |
if numOffsets > 1 or numAddresses > 1: |
| 323 |
valid = False |
| 324 |
if valid: |
| 325 |
return i |
| 326 |
else: |
| 327 |
continue |
| 328 |
|
| 329 |
printWarning("Could not find valid memory instruction for width=%f" % width) |
| 330 |
return len(instructions) |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
def selectMemoryInstruction(self, |
| 337 |
operation, |
| 338 |
width, |
| 339 |
write2, |
| 340 |
para2, |
| 341 |
perp2, |
| 342 |
strides ): |
| 343 |
|
| 344 |
|
| 345 |
instructions = self.memoryInstructions[operation] |
| 346 |
|
| 347 |
if (write2 == "Coalesced" and para2) \ |
| 348 |
or (write2 == "Perpendicular" and perp2): |
| 349 |
instructionIdx = self.findMemoryInstructionForWidthStride( \ |
| 350 |
width, strides, True, instructions) |
| 351 |
|
| 352 |
else: |
| 353 |
instructionIdx = self.findMemoryInstructionForWidthStride( \ |
| 354 |
width, strides, False, instructions) |
| 355 |
|
| 356 |
if instructionIdx < len(instructions): |
| 357 |
return instructionIdx |
| 358 |
else: |
| 359 |
raise RuntimeError("Could not find valid memory instruction for operation=%s, width=%f, kernel=%s" %(operation, width, self.states.kernelName)) |
| 360 |
|
| 361 |
def initGlobalReadMemoryInstruction(self, instructions, tP, bpr): |
| 362 |
|
| 363 |
globalReadWidth = float(tP["nrcv"]*tP["bpeGR"])/bpr |
| 364 |
globalRead2Coalesced = tP["nrc"] > 1 |
| 365 |
globalRead2Perpendicular = tP["nrp"] > 1 |
| 366 |
globalReadInstructionIdx = self.selectMemoryInstruction("GlobalRead", globalReadWidth, \ |
| 367 |
False, \ |
| 368 |
globalRead2Coalesced, globalRead2Perpendicular, [] ) |
| 369 |
|
| 370 |
tP["globalReadInstruction"] = instructions["GlobalRead"][globalReadInstructionIdx] |
| 371 |
|
| 372 |
def initLocalWriteMemoryInstruction(self, instructions, kernel, tP, bpr): |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
localWriteWidth = tP["nwcv"]*tP["bpeDS"]//bpr |
| 378 |
if localWriteWidth < 1: |
| 379 |
localWriteWidth = (1.0*tP["nwcv"]*tP["bpeDS"])/bpr |
| 380 |
localWrite2Coalesced = tP["nrc"]>1 or tP["wtc"] |
| 381 |
localWrite2Perpendicular = tP["nrp"]>1 |
| 382 |
|
| 383 |
if tP["tlu"]: |
| 384 |
if tP["wtc"]: |
| 385 |
localWriteStrideTile = 1 |
| 386 |
else: |
| 387 |
localWriteStrideTile = kernel[tP["lsc"]] |
| 388 |
else: |
| 389 |
localWriteStrideTile = kernel[tP["lsp"]] |
| 390 |
localWriteStrideTile = localWriteStrideTile*tP["bpeDS"]//bpr |
| 391 |
|
| 392 |
if tP["tlu"]: |
| 393 |
localWriteStrideUnroll = kernel[tP["lsc"]]*kernel[tP["mt"]] |
| 394 |
else: |
| 395 |
if tP["wtc"]: |
| 396 |
localWriteStrideUnroll = 1*kernel[tP["mt"]] |
| 397 |
else: |
| 398 |
localWriteStrideUnroll = kernel[tP["lsc"]]*kernel[tP["mt"]] |
| 399 |
localWriteStrideUnroll = \ |
| 400 |
(localWriteStrideUnroll*tP["bpeDS"])//bpr |
| 401 |
localWriteInstructionIdx = self.selectMemoryInstruction("LocalWrite", localWriteWidth, \ |
| 402 |
False, \ |
| 403 |
localWrite2Coalesced, localWrite2Perpendicular, |
| 404 |
[localWriteStrideTile, localWriteStrideUnroll] ) |
| 405 |
|
| 406 |
tP["localWrite2Coalesced"] = localWrite2Coalesced |
| 407 |
tP["localWrite2Perpendicular"] = localWrite2Perpendicular |
| 408 |
tP["localWriteStrideTile"] = localWriteStrideTile |
| 409 |
tP["localWriteStrideUnroll"] = localWriteStrideUnroll |
| 410 |
tP["localWriteInstruction"] = instructions["LocalWrite"][localWriteInstructionIdx] |
| 411 |
|
| 412 |
def initLocalReadMemoryInstruction(self, instructions, kernel, tP, bpr): |
| 413 |
|
| 414 |
tChar = "A" if tP["isA"] else "B" if tP["isB"] else "Metadata" |
| 415 |
if kernel["UnrollMajorLDS%s"%tChar]: |
| 416 |
if tChar == "A": |
| 417 |
localReadWidth = (self.states.lrvwUnrollA * tP["bpeDS"]) / bpr |
| 418 |
if tChar == "B": |
| 419 |
localReadWidth = (self.states.lrvwUnrollB * tP["bpeDS"]) / bpr |
| 420 |
if tChar == "Metadata": |
| 421 |
localReadWidth = (self.states.lrvwUnrollMetadata * tP["bpeDS"]) / bpr |
| 422 |
else: |
| 423 |
if tChar == "A": |
| 424 |
localReadWidth = (self.states.lrvwTileA * tP["bpeDS"]) / bpr |
| 425 |
if tChar == "B": |
| 426 |
localReadWidth = (self.states.lrvwTileB * tP["bpeDS"]) / bpr |
| 427 |
if tChar == "Metadata": |
| 428 |
localReadWidth = (self.states.lrvwTileMetadata * tP["bpeDS"]) / bpr |
| 429 |
|
| 430 |
|
| 431 |
localRead2Perpendicular = False |
| 432 |
localReadStrideCoalesced = kernel[tP["tt"]] * tP["bpeDS"] // bpr |
| 433 |
localRead2Coalesced = False |
| 434 |
tP["enableLDSTr"] = False |
| 435 |
if tChar != "Metadata": |
| 436 |
tP["enableLDSTr"] = kernel["enableLDSTr%s"%tChar] |
| 437 |
length = len(instructions["LocalRead"]) |
| 438 |
|
| 439 |
if tP["enableLDSTr"]: |
| 440 |
localReadInstructionIdx = length - 1 |
| 441 |
else: |
| 442 |
localReadInstructionIdx = self.selectMemoryInstruction("LocalRead", localReadWidth, \ |
| 443 |
False, \ |
| 444 |
localRead2Coalesced, localRead2Perpendicular, |
| 445 |
[localReadStrideCoalesced] ) |
| 446 |
tP["localRead2Coalesced"] = localRead2Coalesced |
| 447 |
tP["localRead2Perpendicular"] = localRead2Perpendicular |
| 448 |
tP["localReadStrideCoalesced"] = localReadStrideCoalesced |
| 449 |
tP["localReadInstruction"] = instructions["LocalRead"][localReadInstructionIdx] |
| 450 |
|
| 451 |
def allocTmpSgpr(self, num: int, alignment=None, tag=None): |
| 452 |
def overflowListener(e): |
| 453 |
self.states.overflowedResources = 2 |
| 454 |
if self.db["AssertOnSgprOverflow"]: |
| 455 |
raise e |
| 456 |
|
| 457 |
tmpSgpr = allocTmpGpr(self.sgprPool, num, self.states.regCaps["MaxSgpr"], alignment, tag, overflowListener) |
| 458 |
return tmpSgpr |
| 459 |
|
| 460 |
def allocTmpSgprList(self, nums: List[int], alignments: Optional[List[int]]=None, tag=None): |
| 461 |
def overflowListener(e): |
| 462 |
self.states.overflowedResources = 2 |
| 463 |
if self.db["AssertOnSgprOverflow"]: |
| 464 |
raise e |
| 465 |
|
| 466 |
tmpSgpr = allocTmpGprList(self.sgprPool, nums, self.states.regCaps["MaxSgpr"], alignments, tag, overflowListener) |
| 467 |
return tmpSgpr |
| 468 |
|
| 469 |
def defineMultiVgprIndex(self, names: List[str], numVgprs: List[int], align=1): |
| 470 |
assert(len(names) == len(numVgprs)) |
| 471 |
vgprIdxVec = self.vgprPool.checkOutMulti(numVgprs, align, tags=names) |
| 472 |
return vgprIdxVec |
| 473 |
|
| 474 |
def defineSgprIdx(self, name, numSgprs, align=1): |
| 475 |
if numSgprs == 0: return |
| 476 |
|
| 477 |
sgprIdx = self.sgprPool.checkOutAligned(numSgprs, align, tag=name, preventOverflow=False) |
| 478 |
|
| 479 |
|
| 480 |
self.sgprs[name] = sgprIdx |
| 481 |
|
| 482 |
return sgprIdx |
| 483 |
|
| 484 |
def defineSgpr(self, name, numSgprs, align=1): |
| 485 |
return RegSet("s", "sgpr"+name, self.defineSgprIdx(name, numSgprs, align)) |
| 486 |
|
| 487 |
def defineMultiSgprIndex(self, names: List[str], numSgprs: List[int], align=1): |
| 488 |
assert(len(names) == len(numSgprs)) |
| 489 |
|
| 490 |
sgprIdxVec = self.sgprPool.checkOutMulti(numSgprs, align, tags=names) |
| 491 |
|
| 492 |
|
| 493 |
for idx, name in enumerate(names): |
| 494 |
self.sgprs[name] = sgprIdxVec[idx] |
| 495 |
|
| 496 |
return sgprIdxVec |
| 497 |
|
| 498 |
def setSgprToInUseState(self, name): |
| 499 |
self.sgprPool.removeFromCheckOut(self.sgprs[name]) |
| 500 |
return RegSet("s", "sgpr"+name, self.sgprs[name]) |
| 501 |
|
| 502 |
def undefineSgpr(self, name): |
| 503 |
self.sgprPool.checkIn(self.sgprs[name]) |
| 504 |
|
| 505 |
|
| 506 |
return ValueSet(name="sgpr"+name, value="UNDEF", format = -1) |
| 507 |
|
| 508 |
def setSgprToFreeState(self, name): |
| 509 |
self.sgprPool.addFromCheckOut(self.sgprs[name]) |
| 510 |
|
| 511 |
|
| 512 |
return ValueSet(name="sgpr"+name, value="UNDEF", format = -1) |
| 513 |
|
| 514 |
def defineVariableSgprs(self, kernel): |
| 515 |
module = Module("DefineVariableSgpr") |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
if kernel["BufferLoad"]: |
| 524 |
|
| 525 |
module.add(self.defineSgpr("SrdA", 4, 4)) |
| 526 |
module.add(self.defineSgpr("SrdB", 4, 4)) |
| 527 |
if kernel["ProblemType"]["Sparse"]: |
| 528 |
module.add(self.defineSgpr("SrdMetadata", 4, 4)) |
| 529 |
|
| 530 |
if self.states.use64bShadowLimit: |
| 531 |
module.add(self.defineSgpr("ShadowLimitA", 2, 2)) |
| 532 |
module.add(self.defineSgpr("ShadowLimitB", 2, 2)) |
| 533 |
if kernel["ProblemType"]["Sparse"]: |
| 534 |
module.add(self.defineSgpr("ShadowLimitMetadata", 2, 2)) |
| 535 |
|
| 536 |
module.add(self.defineSgpr("StaggerUIter", 1)) |
| 537 |
isDTVAorB = (kernel["DirectToVgprA"] != kernel["DirectToVgprB"]) |
| 538 |
if kernel["PrefetchGlobalRead"] == 2 and isDTVAorB: |
| 539 |
|
| 540 |
module.add(self.defineSgpr("StaggerUIterDTV", 1)) |
| 541 |
module.add(self.defineSgpr("WrapUA", 2)) |
| 542 |
module.add(self.defineSgpr("WrapUB", 2)) |
| 543 |
if kernel["ProblemType"]["Sparse"]: |
| 544 |
module.add(self.defineSgpr("WrapUMetadata", 2)) |
| 545 |
|
| 546 |
module.add(self.defineSgpr("GlobalReadIncsA", self.states.a.numSgprGlobalReadIncs)) |
| 547 |
module.add(self.defineSgpr("GlobalReadIncsB", self.states.b.numSgprGlobalReadIncs)) |
| 548 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 549 |
module.add(self.defineSgpr("GlobalReadIncsMetadata", self.states.m.numSgprGlobalReadIncs)) |
| 550 |
|
| 551 |
needPackK16 = False |
| 552 |
needPackK8Lw = False |
| 553 |
needPackK8Hi = False |
| 554 |
|
| 555 |
if kernel["ProblemType"]["DataType"].isHalf() or kernel["ProblemType"]["DataType"].isBFloat16(): |
| 556 |
if self.states.lrvwTileA > 1 or self.states.lrvwTileB > 1: |
| 557 |
needPackK16 = True |
| 558 |
if self.states.lrvwTileMetadata > 1: |
| 559 |
needPackK8Lw = True |
| 560 |
if self.states.lrvwTileMetadata > 2: |
| 561 |
needPackK8Hi = True |
| 562 |
elif kernel["ProblemType"]["DataType"].isInt8() or kernel["ProblemType"]["DataType"].is8bitFloat(): |
| 563 |
if self.states.lrvwTileA > 1 or self.states.lrvwTileB > 1 or self.states.lrvwTileMetadata > 1: |
| 564 |
needPackK8Lw = True |
| 565 |
if self.states.lrvwTileA > 2 or self.states.lrvwTileB > 2 or self.states.lrvwTileMetadata > 2: |
| 566 |
needPackK8Hi = True |
| 567 |
|
| 568 |
|
| 569 |
tPackM = "M" if needPackK16 and needPackK8Lw else "" |
| 570 |
|
| 571 |
if needPackK16: |
| 572 |
module.add(self.defineSgpr("PackKForV0", 1)) |
| 573 |
module.add(self.defineSgpr("PackKForV1", 1)) |
| 574 |
if kernel["StreamK"] != 0: |
| 575 |
self.states.nonPostLoopSgpr.append("PackKForV0") |
| 576 |
self.states.nonPostLoopSgpr.append("PackKForV1") |
| 577 |
if needPackK8Lw: |
| 578 |
module.add(self.defineSgpr("PackKFor%sV0"%tPackM, 1)) |
| 579 |
module.add(self.defineSgpr("PackKFor%sV1"%tPackM, 1)) |
| 580 |
if kernel["StreamK"] != 0 and not needPackK16 : |
| 581 |
self.states.nonPostLoopSgpr.append("PackKForV0") |
| 582 |
self.states.nonPostLoopSgpr.append("PackKForV1") |
| 583 |
if needPackK8Hi: |
| 584 |
module.add(self.defineSgpr("PackKFor%sV2"%tPackM, 1)) |
| 585 |
module.add(self.defineSgpr("PackKFor%sV3"%tPackM, 1)) |
| 586 |
if kernel["StreamK"] != 0 and not needPackK16: |
| 587 |
self.states.nonPostLoopSgpr.append("PackKForV2") |
| 588 |
self.states.nonPostLoopSgpr.append("PackKForV3") |
| 589 |
|
| 590 |
if kernel["ProblemType"]["StochasticRounding"]: |
| 591 |
module.add(self.defineSgpr("RNDSeed", 1)) |
| 592 |
|
| 593 |
if kernel["_UseSgprForGRO"]: |
| 594 |
needFirstSgprOffset = kernel["DirectToLdsA"] and kernel["UseInstOffsetForGRO"] |
| 595 |
numberOfSgpr = self.states.a.numVgprGlobalReadOffsets if needFirstSgprOffset else (self.states.a.numVgprGlobalReadOffsets-1) |
| 596 |
if numberOfSgpr > 0: |
| 597 |
module.add(self.defineSgpr("ScalarGlobalReadOffsetA", numberOfSgpr)) |
| 598 |
|
| 599 |
needFirstSgprOffset = kernel["DirectToLdsB"] and kernel["UseInstOffsetForGRO"] |
| 600 |
numberOfSgpr = self.states.b.numVgprGlobalReadOffsets if needFirstSgprOffset else (self.states.b.numVgprGlobalReadOffsets-1) |
| 601 |
if numberOfSgpr > 0: |
| 602 |
module.add(self.defineSgpr("ScalarGlobalReadOffsetB", numberOfSgpr)) |
| 603 |
|
| 604 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 605 |
needFirstSgprOffset = kernel["DirectToLdsMetadata"] and kernel["UseInstOffsetForGRO"] |
| 606 |
numberOfSgpr = self.states.m.numVgprGlobalReadOffsets if needFirstSgprOffset else (self.states.m.numVgprGlobalReadOffsets-1) |
| 607 |
if numberOfSgpr > 0: |
| 608 |
module.add(self.defineSgpr("ScalarGlobalReadOffsetMetadata", numberOfSgpr)) |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
numDummySgpr= 0 |
| 614 |
for i in range(numDummySgpr): |
| 615 |
module.add(self.defineSgpr("DummySgpr%d"%i, 1)) |
| 616 |
|
| 617 |
if self.sgprPool.size() > self.states.regCaps["MaxSgpr"]: |
| 618 |
print ("warning: Number of defined SGPRS (%d) overflowed max SGPRS (%d)." \ |
| 619 |
% (self.sgprPool.size(), self.states.regCaps["MaxSgpr"])) |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
self.states.numStoreSgprNames2 = [] |
| 628 |
self.states.numStoreSgprNameSizes2 = [] |
| 629 |
storeSgprLoad2 = 0 |
| 630 |
|
| 631 |
if kernel["_GlobalAccumulation"] == 'MultipleBufferSingleKernel': |
| 632 |
storeSgprLoad2 += self.states.rpga |
| 633 |
self.states.numStoreSgprNames2.append("AddressTD") |
| 634 |
self.states.numStoreSgprNameSizes2.append(self.states.rpga) |
| 635 |
|
| 636 |
if kernel["_GlobalAccumulation"] == 'MultipleBufferSingleKernel': |
| 637 |
storeSgprLoad2 += self.states.rpga |
| 638 |
self.states.numStoreSgprNames2.append("Synchronizer") |
| 639 |
self.states.numStoreSgprNameSizes2.append(self.states.rpga) |
| 640 |
|
| 641 |
self.states.numStoreSgprToLoad2 = storeSgprLoad2 |
| 642 |
|
| 643 |
return module |
| 644 |
|
| 645 |
|
| 646 |
def functionSignature(self) -> SignatureBase: |
| 647 |
""" |
| 648 |
Function Signature |
| 649 |
called after rest of code |
| 650 |
""" |
| 651 |
signatureClass = Component.Signature.find(self) |
| 652 |
signature = signatureClass(self) |
| 653 |
return signature |
| 654 |
|
| 655 |
def macroAndSet(self, kernel, tPA, tPB) -> Module: |
| 656 |
module = Module("MacroNSet") |
| 657 |
module.add(MacroVMagicDiv(kernel["MagicDivAlg"])) |
| 658 |
|
| 659 |
tPM = tPA["tpsMetadata"] if tPA["is_sparse"] else tPB["tpsMetadata"] |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
module.addComment2("VGPR Assignments") |
| 664 |
module.addComment0("ValuC range: [%u-%u), %s"%(self.states.c.startVgprValu, self.states.c.startVgprValu+self.states.c.numVgprValu, \ |
| 665 |
"serializedStore enabled" if self.states.serializedStore else "")) |
| 666 |
module.add(RegSet("v", "vgprValuC", self.states.c.startVgprValu)) |
| 667 |
|
| 668 |
module.addComment0("ValuA/B Xn=PLR buffer idx, In=InnerUnroll idx") |
| 669 |
|
| 670 |
|
| 671 |
PLR = self.states.numVgprBuffer |
| 672 |
numBi = PLR |
| 673 |
ri = 0 |
| 674 |
moduleVgprMacro = Module("VALU/G2L Vgpr Macro") |
| 675 |
moduleVgprMacroValuA = Module("VALUA Vgpr Macro") |
| 676 |
moduleVgprMacroValuB = Module("VALUB Vgpr Macro") |
| 677 |
moduleVgprMacroValuAPack = Module("VALUA Pack Vgpr Macro") |
| 678 |
moduleVgprMacroValuBPack = Module("VALUB Pack Vgpr Macro") |
| 679 |
moduleVgprMacroValuM = Module("VALUMetadata Vgpr Macro") |
| 680 |
moduleVgprMacroValuMPack = Module("VALUMetadata Pack Vgpr Macro") |
| 681 |
moduleVgprMacroG2LA = Module("G2LA Vgpr Macro") |
| 682 |
moduleVgprMacroG2LB = Module("G2LB Vgpr Macro") |
| 683 |
module.add(RegSet("v", "vgprBase", self.states.startVgpr)) |
| 684 |
if self.states.a.numVgprValu > 0: |
| 685 |
numBiFactor = numBi |
| 686 |
if kernel["DirectToVgprA"] and (self.states.packDTVA or self.states.convDTVA): |
| 687 |
|
| 688 |
numBiFactor = kernel["LoopIters"] * 2 |
| 689 |
if self.states.lrvwTileA > 1: |
| 690 |
moduleVgprMacro.add(RegSet("v", "vgprValuA_X0_I0_BASE", "vgprBase", self.states.a.startVgprValu - self.states.startVgpr)) |
| 691 |
for bi in range(0,numBiFactor): |
| 692 |
for iui in range(0, kernel["InnerUnroll"]): |
| 693 |
moduleVgprMacroValuA.add(RegSet("v", "vgprValuA_X%u_I%u"%(bi,iui), "vgprValuA_X0_I0_BASE", ri)) |
| 694 |
ri += self.states.a.numVgprValuPerBlock |
| 695 |
if tPA["bpe"] < 4 and not kernel["UnrollMajorLDSA"] and not (kernel["UsePLRPack"] and self.states.numItersPLR): |
| 696 |
ri = 0 |
| 697 |
ri = 0 |
| 698 |
if tPA["bpe"] < 4 and not kernel["UnrollMajorLDSA"]: |
| 699 |
moduleVgprMacro.add(RegSet("v", "vgprValuA_X0_I0_D0_PACK", "vgprBase", self.states.a.startVgprValuPack - self.states.startVgpr)) |
| 700 |
for bi in range(0,numBiFactor): |
| 701 |
for iui in range(0, kernel["InnerUnroll"]): |
| 702 |
for data in range(0,kernel["MIInputPerThreadA"]): |
| 703 |
moduleVgprMacroValuAPack.add(RegSet("v", "vgprValuA_X%u_I%u_D%u"%(bi,iui,data),"vgprValuA_X0_I0_D0_PACK", ri)) |
| 704 |
ri += ceil(kernel["VectorWidthA"] * tPA["bpe"] / self.states.bpr) * kernel["MIWaveTileA"] // kernel["VectorWidthA"] |
| 705 |
if (kernel["UsePLRPack"] and self.states.numItersPLR): |
| 706 |
ri = 0 |
| 707 |
else: |
| 708 |
moduleVgprMacro.add(RegSet("v", "vgprValuA_X0_I0_BASE", "vgprBase", self.states.a.startVgprValu - self.states.startVgpr)) |
| 709 |
for bi in range(0,numBiFactor): |
| 710 |
for iui in range(0, kernel["InnerUnroll"]): |
| 711 |
moduleVgprMacroValuA.add(RegSet("v", "vgprValuA_X%u_I%u"%(bi,iui), "vgprValuA_X0_I0_BASE", ri)) |
| 712 |
ri += self.states.a.numVgprValuPerBlock |
| 713 |
ri = 0 |
| 714 |
if tPA["bpe"] < 4 and not kernel["UnrollMajorLDSA"]: |
| 715 |
moduleVgprMacro.add(RegSet("v", "vgprValuA_X0_I0_D0_PACK", "vgprBase", self.states.a.startVgprValuPack - self.states.startVgpr)) |
| 716 |
for data in range(1,int(self.states.bpr/tPA["bpeDS"])): |
| 717 |
for bi in range(0,numBiFactor): |
| 718 |
if bi % self.states.numVgprBufferPackA == 0: |
| 719 |
ri = (data-1) * kernel["InnerUnroll"] * self.states.numVgprBufferPackA * self.states.a.numVgprValuPerBlock |
| 720 |
for iui in range(0, kernel["InnerUnroll"]): |
| 721 |
moduleVgprMacroValuAPack.add(RegSet("v", "vgprValuA_X%u_I%u_D%u"%(bi,iui,data),"vgprValuA_X0_I0_D0_PACK", ri)) |
| 722 |
ri += self.states.a.numVgprValuPerBlock |
| 723 |
|
| 724 |
ri = 0 |
| 725 |
if self.states.b.numVgprValu > 0: |
| 726 |
numBiFactor = numBi |
| 727 |
if kernel["DirectToVgprB"] and (self.states.packDTVB or self.states.convDTVB): |
| 728 |
|
| 729 |
numBiFactor = kernel["LoopIters"] * 2 |
| 730 |
if self.states.lrvwTileB > 1: |
| 731 |
moduleVgprMacro.add(RegSet("v", "vgprValuB_X0_I0_BASE", "vgprBase", self.states.b.startVgprValu - self.states.startVgpr)) |
| 732 |
for bi in range(0,numBiFactor): |
| 733 |
for iui in range(0, kernel["InnerUnroll"]): |
| 734 |
moduleVgprMacroValuB.add(RegSet("v", "vgprValuB_X%u_I%u"%(bi,iui), "vgprValuB_X0_I0_BASE", ri)) |
| 735 |
ri += self.states.b.numVgprValuPerBlock |
| 736 |
if (tPB["bpe"] < 4 and not kernel["UnrollMajorLDSB"]) and not (kernel["UsePLRPack"] and self.states.numItersPLR): |
| 737 |
ri = 0 |
| 738 |
ri = 0 |
| 739 |
if tPB["bpe"] < 4 and not kernel["UnrollMajorLDSB"]: |
| 740 |
moduleVgprMacro.add(RegSet("v", "vgprValuB_X0_I0_D0_PACK", "vgprBase", self.states.b.startVgprValuPack - self.states.startVgpr)) |
| 741 |
for bi in range(0,numBiFactor): |
| 742 |
for iui in range(0, kernel["InnerUnroll"]): |
| 743 |
for data in range(0,kernel["MIInputPerThreadB"]): |
| 744 |
moduleVgprMacroValuBPack.add(RegSet("v", "vgprValuB_X%u_I%u_D%u"%(bi,iui,data), "vgprValuB_X0_I0_D0_PACK", ri)) |
| 745 |
ri += ceil(kernel["VectorWidthB"] * tPB["bpe"] / self.states.bpr) * kernel["MIWaveTileB"] // kernel["VectorWidthB"] |
| 746 |
if (kernel["UsePLRPack"] and self.states.numItersPLR): |
| 747 |
ri = 0 |
| 748 |
else: |
| 749 |
moduleVgprMacro.add(RegSet("v", "vgprValuB_X0_I0_BASE", "vgprBase", self.states.b.startVgprValu - self.states.startVgpr)) |
| 750 |
for bi in range(0,numBiFactor): |
| 751 |
for iui in range(0, kernel["InnerUnroll"]): |
| 752 |
moduleVgprMacroValuB.add(RegSet("v", "vgprValuB_X%u_I%u"%(bi,iui), "vgprValuB_X0_I0_BASE", ri)) |
| 753 |
ri += self.states.b.numVgprValuPerBlock |
| 754 |
ri = 0 |
| 755 |
if tPB["bpe"] < 4 and not kernel["UnrollMajorLDSB"]: |
| 756 |
moduleVgprMacro.add(RegSet("v", "vgprValuB_X0_I0_D0_PACK", "vgprBase", self.states.b.startVgprValuPack - self.states.startVgpr)) |
| 757 |
for data in range(1,int(self.states.bpr/tPB["bpeDS"])): |
| 758 |
for bi in range(0,numBiFactor): |
| 759 |
if bi % self.states.numVgprBufferPackB == 0: |
| 760 |
ri = (data-1) * kernel["InnerUnroll"] * self.states.numVgprBufferPackB * self.states.b.numVgprValuPerBlock |
| 761 |
for iui in range(0, kernel["InnerUnroll"]): |
| 762 |
moduleVgprMacroValuBPack.add(RegSet("v", "vgprValuB_X%u_I%u_D%u"%(bi,iui,data), "vgprValuB_X0_I0_D0_PACK", ri)) |
| 763 |
ri += self.states.b.numVgprValuPerBlock |
| 764 |
|
| 765 |
if kernel["ConvertAfterDS"]: |
| 766 |
cvtTemp = max(self.states.a.startVgprValuCvtTemp, self.states.b.startVgprValuCvtTemp) |
| 767 |
if (cvtTemp != -1): |
| 768 |
moduleVgprMacro.add(RegSet("v", "vgprCvtTemp", "vgprBase", cvtTemp - self.states.startVgpr)) |
| 769 |
|
| 770 |
if kernel["ProblemType"]["Gradient"] and kernel["ProblemType"]["UseBias"] and (kernel["ProblemType"]["BiasSrc"] == "A" or kernel["ProblemType"]["BiasSrc"] == "B"): |
| 771 |
moduleVgprMacro.add(RegSet("v", "vgprValuSum", "vgprBase", self.states.bias.startVgprValu - self.states.startVgpr)) |
| 772 |
|
| 773 |
if kernel["ProblemType"]["Sparse"]: |
| 774 |
if kernel["DirectToVgprSparseMetadata"]: |
| 775 |
moduleVgprMacro.add(RegSet("v", "vgprValuMetadata_X0_I0_BASE", "vgprBase", self.states.m.startVgprValu - self.states.startVgpr)) |
| 776 |
moduleVgprMacroValuM.add(RegSet("v", "vgprValuMetadata", "vgprValuMetadata_X0_I0_BASE", 0)) |
| 777 |
else: |
| 778 |
ri = 0 |
| 779 |
if self.states.m.numVgprValu > 0: |
| 780 |
if self.states.lrvwTileMetadata > 1: |
| 781 |
moduleVgprMacro.add(RegSet("v", "vgprValuMetadata_X0_I0_BASE", "vgprBase", self.states.m.startVgprValu - self.states.startVgpr)) |
| 782 |
for bi in range(0,PLR): |
| 783 |
for iui in range(0, kernel["InnerUnroll"]): |
| 784 |
moduleVgprMacroValuM.add(RegSet("v", "vgprValuMetadata_X%u_I%u"%(bi,iui), "vgprValuMetadata_X0_I0_BASE", ri)) |
| 785 |
ri += self.states.m.numVgprValuPerBlock |
| 786 |
if not kernel["UnrollMajorLDSMetadata"]: |
| 787 |
ri = 0 |
| 788 |
ri = 0 |
| 789 |
if not kernel["UnrollMajorLDSMetadata"]: |
| 790 |
miWaveTile = kernel["MIWaveTileB"] if kernel["ProblemType"]["Sparse"] == 2 else kernel["MIWaveTileA"] |
| 791 |
moduleVgprMacro.add(RegSet("v", "vgprValuMetadata_X0_I0_D_PACK", "vgprBase", self.states.m.startVgprValuPack - self.states.startVgpr)) |
| 792 |
for data in range(0,kernel["MIInputPerThreadMetadata"]): |
| 793 |
for bi in range(0,PLR): |
| 794 |
for iui in range(0, kernel["InnerUnroll"]): |
| 795 |
moduleVgprMacroValuMPack.add(RegSet("v", "vgprValuMetadata_X%u_I%u_D%u"%(bi,iui,data), "vgprValuMetadata_X0_I0_D_PACK", ri)) |
| 796 |
ri += ceil(kernel["VectorWidthMetadata"] * tPM["bpe"] / self.states.bpr) * miWaveTile // kernel["VectorWidthMetadata"] |
| 797 |
else: |
| 798 |
moduleVgprMacro.add(RegSet("v", "vgprValuMetadata_X0_I0_BASE", "vgprBase", self.states.m.startVgprValu - self.states.startVgpr)) |
| 799 |
for bi in range(0,PLR): |
| 800 |
for iui in range(0, kernel["InnerUnroll"]): |
| 801 |
moduleVgprMacroValuM.add(RegSet("v", "vgprValuMetadata_X%u_I%u"%(bi,iui), "vgprValuMetadata_X0_I0_BASE", ri)) |
| 802 |
ri += self.states.m.numVgprValuPerBlock |
| 803 |
|
| 804 |
if not kernel["LocalWriteUseSgprA"] and self.states.a.numVgprLocalWriteAddr > 0: |
| 805 |
module.add(RegSet("v", "vgprLocalWriteAddrA", \ |
| 806 |
self.states.a.startVgprLocalWriteAddr)) |
| 807 |
if self.states.a.numVgprLocalWriteAddr > 1: |
| 808 |
module.add(RegSet("v", "vgprLocalWriteAddrOverhangA", \ |
| 809 |
self.states.a.startVgprLocalWriteAddr+1)) |
| 810 |
if not kernel["LocalWriteUseSgprB"] and self.states.b.numVgprLocalWriteAddr > 0: |
| 811 |
module.add(RegSet("v", "vgprLocalWriteAddrB", \ |
| 812 |
self.states.b.startVgprLocalWriteAddr)) |
| 813 |
if self.states.b.numVgprLocalWriteAddr > 1: |
| 814 |
module.add(RegSet("v", "vgprLocalWriteAddrOverhangB", \ |
| 815 |
self.states.b.startVgprLocalWriteAddr+1)) |
| 816 |
if self.states.m.numVgprLocalWriteAddr > 0: |
| 817 |
module.add(RegSet("v", "vgprLocalWriteAddrMetadata", \ |
| 818 |
self.states.m.startVgprLocalWriteAddr)) |
| 819 |
if self.states.m.numVgprLocalWriteAddr > 1: |
| 820 |
module.add(RegSet("v", "vgprLocalWriteAddrOverhangMetadata", \ |
| 821 |
self.states.m.startVgprLocalWriteAddr+1)) |
| 822 |
if kernel["BufferLoad"]: |
| 823 |
module.add(RegSet("v", "vgprGlobalReadOffsetA", \ |
| 824 |
self.startVgprGlobalReadOffsetA)) |
| 825 |
module.add(RegSet("v", "vgprGlobalReadOffsetB", \ |
| 826 |
self.startVgprGlobalReadOffsetB)) |
| 827 |
if kernel["ProblemType"]["Sparse"]: |
| 828 |
module.add(RegSet("v", "vgprGlobalReadOffsetMetadata", \ |
| 829 |
self.startVgprGlobalReadOffsetMetadata)) |
| 830 |
else: |
| 831 |
module.add(RegSet("v", "vgprGlobalReadAddrA", \ |
| 832 |
self.startVgprGlobalReadAddressesA)) |
| 833 |
module.add(RegSet("v", "vgprGlobalReadAddrB", \ |
| 834 |
self.startVgprGlobalReadAddressesB)) |
| 835 |
|
| 836 |
if self.states.a.startVgprG2L is not None: |
| 837 |
moduleVgprMacro.add(RegSet("v", "vgprG2LA_BASE", "vgprBase", self.states.a.startVgprG2L - self.states.startVgpr)) |
| 838 |
if self.states.b.startVgprG2L is not None: |
| 839 |
moduleVgprMacro.add(RegSet("v", "vgprG2LB_BASE", "vgprBase", self.states.b.startVgprG2L - self.states.startVgpr)) |
| 840 |
if not kernel["DirectToLdsA"] or self.do["KeepDirectToLdsAlloc"]: |
| 841 |
moduleVgprMacroG2LA.add(RegSet("v", "vgprG2LA", "vgprG2LA_BASE", 0)) |
| 842 |
if kernel["DirectToVgprA"]: |
| 843 |
|
| 844 |
moduleVgprMacroG2LA.add(RegSet("v", "vgprG2LA2", "vgprG2LA_BASE", self.states.a.numVgprG2LAllocated//2)) |
| 845 |
if not kernel["DirectToLdsB"] or self.do["KeepDirectToLdsAlloc"]: |
| 846 |
moduleVgprMacroG2LB.add(RegSet("v", "vgprG2LB", "vgprG2LB_BASE", 0)) |
| 847 |
if kernel["DirectToVgprB"]: |
| 848 |
|
| 849 |
moduleVgprMacroG2LB.add(RegSet("v", "vgprG2LB2", "vgprG2LB_BASE", self.states.b.numVgprG2LAllocated//2)) |
| 850 |
if kernel["UnrollLoopSwapGlobalReadOrder"] and not kernel["DirectToLdsA"] and not kernel["DirectToLdsB"]: |
| 851 |
if kernel["ULSGRODoubleG2L"] == 0: |
| 852 |
moduleVgprMacroG2LA.add(RegSet("v", "vgprG2LB2", "vgprG2LA_BASE", 0)) |
| 853 |
moduleVgprMacroG2LA.add(RegSet("v", "vgprG2LA2", "vgprG2LA_BASE", self.states.b.numVgprG2LAllocated)) |
| 854 |
else: |
| 855 |
moduleVgprMacroG2LA.add(RegSet("v", "vgprG2LA2", "vgprG2LA_BASE", self.states.a.numVgprG2LAllocated)) |
| 856 |
moduleVgprMacroG2LB.add(RegSet("v", "vgprG2LB2", "vgprG2LB_BASE", self.states.b.numVgprG2LAllocated)) |
| 857 |
|
| 858 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 859 |
moduleVgprMacro.add(RegSet("v", "vgprG2LMetadata", "vgprBase", self.states.m.startVgprG2L - self.states.startVgpr)) |
| 860 |
|
| 861 |
if ((tPA["bpe"] < 4 and not kernel["UnrollMajorLDSA"]) or \ |
| 862 |
(tPB["bpe"] < 4 and not kernel["UnrollMajorLDSB"]) or \ |
| 863 |
(not kernel["UnrollMajorLDSMetadata"] and kernel["MIInputPerThreadMetadata"] == 4)) \ |
| 864 |
and (kernel["ProblemType"]["DataType"].isInt8() or kernel["ProblemType"]["DataType"].is8bitFloat()): |
| 865 |
moduleVgprMacro.add(RegSet("v", "vgprPackTemp", "vgprBase", self.states.a.startVgprValuPackTemp - self.states.startVgpr)) |
| 866 |
|
| 867 |
if self.states.globalReadIncsUseVgpr: |
| 868 |
module.add(RegSet("v", "vgprGlobalReadIncsA", \ |
| 869 |
self.startVgprGlobalReadIncsA)) |
| 870 |
module.add(RegSet("v", "vgprGlobalReadIncsB", \ |
| 871 |
self.startVgprGlobalReadIncsB)) |
| 872 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 873 |
module.add(RegSet("v", "vgprGlobalReadIncsMetadata", \ |
| 874 |
self.startVgprGlobalReadIncsMetadata)) |
| 875 |
if self.states.a.numVgprLocalReadAddr > 0: |
| 876 |
module.add(RegSet("v", "vgprLocalReadAddrA", \ |
| 877 |
self.states.a.startVgprLocalReadAddr)) |
| 878 |
if self.states.b.numVgprLocalReadAddr > 0: |
| 879 |
module.add(RegSet("v", "vgprLocalReadAddrB", \ |
| 880 |
self.states.b.startVgprLocalReadAddr)) |
| 881 |
if self.states.m.numVgprLocalReadAddr > 0: |
| 882 |
module.add(RegSet("v", "vgprLocalReadAddrMetadata", \ |
| 883 |
self.states.m.startVgprLocalReadAddr)) |
| 884 |
if self.states.a.numVgprLocalReadSwapAddr > 0: |
| 885 |
module.add(RegSet("v", "vgprLocalReadSwapAddrA", \ |
| 886 |
self.states.a.startVgprLocalReadSwapAddr)) |
| 887 |
if self.states.b.numVgprLocalReadSwapAddr > 0: |
| 888 |
module.add(RegSet("v", "vgprLocalReadSwapAddrB", \ |
| 889 |
self.states.b.startVgprLocalReadSwapAddr)) |
| 890 |
if self.states.m.numVgprLocalReadSwapAddr > 0: |
| 891 |
module.add(RegSet("v", "vgprLocalReadSwapAddrMetadata", \ |
| 892 |
self.states.m.startVgprLocalReadSwapAddr)) |
| 893 |
if self.states.a.numVgprLocalWriteSwapAddr > 0: |
| 894 |
module.add(RegSet("v", "vgprLocalWriteSwapAddrA", \ |
| 895 |
self.states.a.startVgprLocalWriteSwapAddr)) |
| 896 |
if self.states.b.numVgprLocalWriteSwapAddr > 0: |
| 897 |
module.add(RegSet("v", "vgprLocalWriteSwapAddrB", \ |
| 898 |
self.states.b.startVgprLocalWriteSwapAddr)) |
| 899 |
if self.states.m.numVgprLocalWriteSwapAddr > 0: |
| 900 |
module.add(RegSet("v", "vgprLocalWriteSwapAddrMetadata", \ |
| 901 |
self.states.m.startVgprLocalWriteSwapAddr)) |
| 902 |
|
| 903 |
if kernel["ProblemType"]["OutputAmaxD"]: |
| 904 |
module.add(RegSet("v", "vgprAmaxOut", self.startVgprAmaxOut)) |
| 905 |
module.add(RegSet("v", "vgprAmaxOutB", self.startVgprAmaxOutB)) |
| 906 |
|
| 907 |
if kernel["ProblemType"]["DataType"].isDoubleComplex() and kernel["MIArchVgpr"]: |
| 908 |
module.add(RegSet("v", "vgprAlphaTmp", \ |
| 909 |
self.states.startVgprAlphaTmp)) |
| 910 |
|
| 911 |
module.add(RegSet("v", "vgprSerial", self.states.startVgprSerial)) |
| 912 |
|
| 913 |
if self.debugConfig.debugKernel: |
| 914 |
module.add(RegSet("v", "vgprAddressDbg", \ |
| 915 |
self.states.startVgprAddressDbg)) |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
self.moduleVgprMacro = moduleVgprMacro |
| 920 |
self.moduleVgprMacroValuA = moduleVgprMacroValuA |
| 921 |
self.moduleVgprMacroValuB = moduleVgprMacroValuB |
| 922 |
self.moduleVgprMacroValuAPack = moduleVgprMacroValuAPack |
| 923 |
self.moduleVgprMacroValuBPack = moduleVgprMacroValuBPack |
| 924 |
self.moduleVgprMacroValuM = moduleVgprMacroValuM |
| 925 |
self.moduleVgprMacroValuMPack = moduleVgprMacroValuMPack |
| 926 |
self.moduleVgprMacroValuA = moduleVgprMacroValuA |
| 927 |
self.moduleVgprMacroG2LA = moduleVgprMacroG2LA |
| 928 |
self.moduleVgprMacroG2LB = moduleVgprMacroG2LB |
| 929 |
module.addComment2("VGPR Macro Assignments") |
| 930 |
module.add(self.moduleVgprMacro) |
| 931 |
module.add(self.moduleVgprMacroValuA) |
| 932 |
module.add(self.moduleVgprMacroValuB) |
| 933 |
module.add(self.moduleVgprMacroValuAPack) |
| 934 |
module.add(self.moduleVgprMacroValuBPack) |
| 935 |
module.add(self.moduleVgprMacroValuM) |
| 936 |
module.add(self.moduleVgprMacroValuMPack) |
| 937 |
module.add(self.moduleVgprMacroG2LA) |
| 938 |
module.add(self.moduleVgprMacroG2LB) |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
module.addComment2("SGPR Assignments") |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
for skey in self.sgprs: |
| 948 |
module.add(RegSet("s", "sgpr"+skey, self.sgprs[skey])) |
| 949 |
|
| 950 |
|
| 951 |
if kernel["StreamK"] == 2 or kernel["StreamK"] == 3: |
| 952 |
module.addSpaceLine() |
| 953 |
module.addComment0("StreamK Parallel Reduction Assignments") |
| 954 |
module.add(RegSet("s", "sgprSkSplit", "sgprskTiles", 0)) |
| 955 |
module.add(RegSet("s", "sgprSkPartialIdx", "sgprBeta", 0)) |
| 956 |
|
| 957 |
module.addSpaceLine() |
| 958 |
module.addComment0("Size Assignments") |
| 959 |
problemType = kernel["ProblemType"] |
| 960 |
for idx in range(max(problemType["IndexAssignmentsA"] + problemType["IndexAssignmentsB"])+1): |
| 961 |
idxChar= INDEX_CHARS[idx] |
| 962 |
if idx in problemType["IndicesFree"] or idx in problemType["IndicesBatch"]: |
| 963 |
idxType="Free" |
| 964 |
elif idx in problemType["IndicesSummation"]: |
| 965 |
idxType="Sum" |
| 966 |
idx = idx - problemType["NumIndicesC"] |
| 967 |
else: |
| 968 |
raise ValueError("unexpected index type in size assignments") |
| 969 |
|
| 970 |
module.add(RegSet("s", "sgprSize%s"%(idxChar), \ |
| 971 |
"sgprSizes%s"%idxType, idx)) |
| 972 |
|
| 973 |
module.addSpaceLine() |
| 974 |
module.addComment0("Stride Assignments") |
| 975 |
for tc in ('D','C'): |
| 976 |
for idx in range(0, problemType["NumIndicesC"]): |
| 977 |
i = idx |
| 978 |
idxChar= self.states.indexChars[idx] |
| 979 |
if i == 0 and not kernel["ProblemType"]["UseInitialStridesCD"]: |
| 980 |
module.add(ValueSet("constStride%s%s"%(tc,idxChar), 1)) |
| 981 |
else: |
| 982 |
if not kernel["ProblemType"]["UseInitialStridesCD"]: |
| 983 |
i = i-1 |
| 984 |
module.add(RegSet("s", "sgprStride%s%s"%(tc,idxChar), \ |
| 985 |
"sgprStrides%s"%tc, i)) |
| 986 |
|
| 987 |
tcList = ["A", "B"] |
| 988 |
if kernel["ProblemType"]["Sparse"]: |
| 989 |
tcList.append("Metadata") |
| 990 |
for tc in tcList: |
| 991 |
for i, idx in enumerate(problemType["IndexAssignments%s"%tc]): |
| 992 |
idxChar= self.states.indexChars[idx] |
| 993 |
if i == 0 and not kernel["ProblemType"]["UseInitialStridesAB"]: |
| 994 |
module.add(ValueSet("constStride%s%s"%(tc,idxChar), 1)) |
| 995 |
else: |
| 996 |
if not kernel["ProblemType"]["UseInitialStridesAB"]: |
| 997 |
i = i-1 |
| 998 |
module.add(RegSet("s", "sgprStride%s%s"%(tc,idxChar), \ |
| 999 |
"sgprStrides%s"%tc, i)) |
| 1000 |
|
| 1001 |
module.addSpaceLine() |
| 1002 |
module.add(ValueSet("MT0", kernel["MacroTile0"])) |
| 1003 |
module.add(ValueSet("MT1", kernel["MacroTile1"])) |
| 1004 |
module.add(ValueSet("DepthU", kernel["DepthU"])) |
| 1005 |
module.add(ValueSet("BpeA", tPA["bpe"])) |
| 1006 |
module.add(ValueSet("BpeALog2", log2(tPA["bpe"]))) |
| 1007 |
module.add(ValueSet("BpeB", tPB["bpe"])) |
| 1008 |
module.add(ValueSet("BpeBLog2", log2(tPB["bpe"]))) |
| 1009 |
module.add(ValueSet("BpeAGR", tPA["bpeGR"])) |
| 1010 |
module.add(ValueSet("BpeAGRLog2", log2(tPA["bpeGR"]))) |
| 1011 |
module.add(ValueSet("BpeBGR", tPB["bpeGR"])) |
| 1012 |
module.add(ValueSet("BpeBGRLog2", log2(tPB["bpeGR"]))) |
| 1013 |
|
| 1014 |
if kernel["ProblemType"]["SwizzleTensorA"]: |
| 1015 |
module.add(ValueSet("MI_M", 16)) |
| 1016 |
if kernel["ProblemType"]["SwizzleTensorB"]: |
| 1017 |
module.add(ValueSet("MI_N", 16)) |
| 1018 |
|
| 1019 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 1020 |
module.add(ValueSet("BpeMetadata", tPM["bpe"])) |
| 1021 |
module.add(ValueSet("BpeMetadataLog2", log2(tPM["bpe"]))) |
| 1022 |
module.addComment0("Number of elements to shift-left SRD") |
| 1023 |
module.add(ValueSet("SrdShiftLeftA", self.states.srdShiftLeft['A'])) |
| 1024 |
module.add(ValueSet("SrdShiftLeftB", self.states.srdShiftLeft['B'])) |
| 1025 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 1026 |
module.add(ValueSet("SrdShiftLeftMetadata", self.states.srdShiftLeft["Metadata"])) |
| 1027 |
if kernel["BufferLoad"] or kernel["BufferStore"]: |
| 1028 |
module.addComment0("2GB limit - set offsets to -1 to exceed this and clamp") |
| 1029 |
module.add(ValueSet("BufferLimit", 0xffffffff, format=1)) |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
module.add(ValueSet("BufferOOB", 0x80000000, format=1)) |
| 1034 |
|
| 1035 |
srdUpperValue = SrdUpperValue(self.states.version) |
| 1036 |
module.addComment2("Bits 127:96 of SRD.\n" + srdUpperValue.desc()) |
| 1037 |
module.add(ValueSet("Srd127_96", srdUpperValue.getValue(), format=1)) |
| 1038 |
|
| 1039 |
if ((kernel["GlobalSplitUAlgorithm"] == 'MultipleBufferSingleKernel')): |
| 1040 |
module.addComment0("MT offset for 64b address (=MT0*MT1*bpeC)") |
| 1041 |
reductionOffset = int(kernel["MacroTile0"]*kernel["MacroTile1"]*self.states.bpeCinternal) |
| 1042 |
reductionOffsetLow32 = reductionOffset & 0xFFFFFFFF |
| 1043 |
reductionOffsetHigh32 = (reductionOffset >> 32) & 0xFFFFFFFF |
| 1044 |
module.add(ValueSet("MTOffset", reductionOffsetLow32, format=1)) |
| 1045 |
module.add(ValueSet("MTOffsetH32", reductionOffsetHigh32, format=1)) |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
GOList = [ \ |
| 1055 |
("C", list(range(0, kernel["ProblemType"]["NumIndicesC"])), kernel["BufferStore"], None, False), \ |
| 1056 |
("A", kernel["ProblemType"]["IndexAssignmentsA"], kernel["BufferLoad"], tPA, False), \ |
| 1057 |
("B", kernel["ProblemType"]["IndexAssignmentsB"], kernel["BufferLoad"], tPB, False) ] |
| 1058 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 1059 |
GOList.append(("Metadata", kernel["ProblemType"]["IndexAssignmentsMetadata"], kernel["BufferLoad"], tPM, False)) |
| 1060 |
if kernel["ProblemType"]["SwizzleTensorA"]: |
| 1061 |
GOList.append(("A", kernel["ProblemType"]["IndexAssignmentsA"], kernel["BufferLoad"], tPA, True)) |
| 1062 |
if kernel["ProblemType"]["SwizzleTensorB"]: |
| 1063 |
GOList.append(("B", kernel["ProblemType"]["IndexAssignmentsB"], kernel["BufferLoad"], tPB, True)) |
| 1064 |
|
| 1065 |
for (tc, indices, justOffset32, tP, isSwizzled) in GOList: |
| 1066 |
|
| 1067 |
|
| 1068 |
if tc == "C" and kernel["BufferStore"]: |
| 1069 |
continue |
| 1070 |
|
| 1071 |
|
| 1072 |
suffix_tc = tc + "_SWIZZLED" if isSwizzled else tc |
| 1073 |
module.addComment1("Global Offset %s"%suffix_tc) |
| 1074 |
numDim = len(indices) |
| 1075 |
idxChars = [] |
| 1076 |
for i in indices: |
| 1077 |
idxChars.append(self.states.indexChars[i]) |
| 1078 |
|
| 1079 |
|
| 1080 |
calcDims = [] |
| 1081 |
mirrorSumDims = [] |
| 1082 |
macroArgs = [] |
| 1083 |
for i in range(0, numDim): |
| 1084 |
if tc == 'C': |
| 1085 |
useInitialStrides = kernel["ProblemType"]["UseInitialStridesCD"] |
| 1086 |
idxChar = self.states.indexChars[i] |
| 1087 |
else: |
| 1088 |
useInitialStrides = kernel["ProblemType"]["UseInitialStridesAB"] |
| 1089 |
idxChar = self.states.indexChars[tP['ia'][i]] |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
if tc in ('A','C') and indices[i] == kernel["ProblemType"]["Index0"] \ |
| 1094 |
or tc in ('B','C', "Metadata") and indices[i] == kernel["ProblemType"]["Index1"] \ |
| 1095 |
or indices[i] == kernel["ProblemType"]["IndexUnroll"]: |
| 1096 |
macroArgs.append("vgprOffset%s:req" % idxChars[i]) |
| 1097 |
calcDims.append(i) |
| 1098 |
elif indices[i] in kernel["ProblemType"]["IndicesSummation"]: |
| 1099 |
|
| 1100 |
if tc in ('A', 'B', "Metadata") and indices[i] in kernel["ProblemType"]["MirrorDims%s" % tc]: |
| 1101 |
mirrorSumDims.append(i) |
| 1102 |
continue |
| 1103 |
else: |
| 1104 |
|
| 1105 |
if isPackedIndex(kernel, indices[i]): |
| 1106 |
calcDims.append(i) |
| 1107 |
macroArgs.append("vgprOffset%s:req" % idxChars[i]) |
| 1108 |
elif not justOffset32: |
| 1109 |
calcDims.append(i) |
| 1110 |
macroArgs.append("sgprOffset%s:req" % idxChars[i]) |
| 1111 |
|
| 1112 |
macro = Macro("GLOBAL_OFFSET_%s" % suffix_tc, ["vgprAddr:req", *macroArgs, "vgprTmp:req"]) |
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
pendingOffset = None |
| 1122 |
offsetIsVgpr = False |
| 1123 |
destLo = None |
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
writeDirectToAddr = 1 |
| 1128 |
|
| 1129 |
|
| 1130 |
for i in mirrorSumDims: |
| 1131 |
if writeDirectToAddr: |
| 1132 |
dest = vgpr("Addr+0", isMacro=True) |
| 1133 |
needAdd = 0 |
| 1134 |
writeDirectToAddr = 0 |
| 1135 |
else: |
| 1136 |
dest = vgpr("Tmp+0", isMacro=True) |
| 1137 |
needAdd = 1 |
| 1138 |
macro.add(VSubU32(dst=dest, \ |
| 1139 |
src0=sgpr("Size%s"%INDEX_CHARS[indices[i]]), \ |
| 1140 |
src1=1, \ |
| 1141 |
comment="mirror %s%s 1"%(tc, INDEX_CHARS[indices[i]]))) |
| 1142 |
macro.add(VMulLOU32(dst=dest, \ |
| 1143 |
src0=dest, \ |
| 1144 |
src1=self.strideRef(tc, indices[i]), \ |
| 1145 |
comment="mirror %s%s 2"%(tc, INDEX_CHARS[indices[i]]))) |
| 1146 |
|
| 1147 |
if needAdd: |
| 1148 |
writeDirectToAddr = 0 |
| 1149 |
destLo = vgpr("Addr+0", isMacro=True) |
| 1150 |
destHi = vgpr("Addr+1", isMacro=True) |
| 1151 |
|
| 1152 |
srcLo = pendingOffset if pendingOffset else destLo |
| 1153 |
srcHi = 0 if pendingOffset else destHi |
| 1154 |
macro.add(VAddCOU32(dst=destLo, \ |
| 1155 |
dst1=VCC(), \ |
| 1156 |
src0=srcLo, \ |
| 1157 |
src1="v[\\vgprTmp+0]", \ |
| 1158 |
comment="accumulate %s lower"%idxChar)) |
| 1159 |
|
| 1160 |
for i in calcDims: |
| 1161 |
|
| 1162 |
idx = indices[i] |
| 1163 |
isMirrorIdx = tc in ('A', 'B', "Metadata") and idx in kernel["ProblemType"]["MirrorDims%s" % tc] |
| 1164 |
assert not (idx in kernel["ProblemType"]["IndicesSummation"] and idx != kernel["ProblemType"]["IndexUnroll"]) |
| 1165 |
|
| 1166 |
if indices[i] == kernel["ProblemType"]["Index0"] \ |
| 1167 |
or indices[i] == kernel["ProblemType"]["Index1"] \ |
| 1168 |
or indices[i] == kernel["ProblemType"]["IndexUnroll"]: |
| 1169 |
offsetIsVgpr = True |
| 1170 |
|
| 1171 |
elif indices[i] < kernel["ProblemType"]["NumIndicesC"]: |
| 1172 |
if isPackedIndex(kernel, indices[i]): |
| 1173 |
offsetIsVgpr = True |
| 1174 |
else: |
| 1175 |
offsetIsVgpr = False |
| 1176 |
else: |
| 1177 |
assert(0) |
| 1178 |
|
| 1179 |
|
| 1180 |
if offsetIsVgpr: |
| 1181 |
offset = "v[\\vgprOffset%s]" % idxChars[i] |
| 1182 |
else: |
| 1183 |
offset = "s[\\sgprOffset%s]" % idxChars[i] |
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
needAdd = 0 |
| 1189 |
|
| 1190 |
if i==0 and not useInitialStrides: |
| 1191 |
|
| 1192 |
pendingOffset = offset |
| 1193 |
writeDirectToAddr = 0 |
| 1194 |
else: |
| 1195 |
|
| 1196 |
if offsetIsVgpr: |
| 1197 |
if writeDirectToAddr: |
| 1198 |
destLo = vgpr("Addr+0", isMacro=True) |
| 1199 |
destHi = vgpr("Addr+1", isMacro=True) |
| 1200 |
needAdd = 0 |
| 1201 |
writeDirectToAddr = 0 |
| 1202 |
else: |
| 1203 |
destLo = vgpr("Tmp+0", isMacro=True) |
| 1204 |
destHi = vgpr("Tmp+1", isMacro=True) |
| 1205 |
needAdd = 1 |
| 1206 |
if isMirrorIdx: |
| 1207 |
macro.add(VSubI32( |
| 1208 |
dst=vgpr("Tmp+0", isMacro=True), |
| 1209 |
src0=sgpr("Size%s"%INDEX_CHARS[idx]), \ |
| 1210 |
src1=offset, \ |
| 1211 |
comment="mirror %s%s 1"%(tc, INDEX_CHARS[indices[i]]))) |
| 1212 |
macro.add(VSubI32(\ |
| 1213 |
dst=vgpr("Tmp+0", isMacro=True), |
| 1214 |
src0="v[\\vgprTmp+0]", \ |
| 1215 |
src1=1, \ |
| 1216 |
comment="mirror %s%s 2"%(tc, INDEX_CHARS[indices[i]]))) |
| 1217 |
offset = "v[\\vgprTmp+0]" |
| 1218 |
|
| 1219 |
|
| 1220 |
if isSwizzled: |
| 1221 |
|
| 1222 |
macro.add(VMovB32(dst=destLo, src=offset)) |
| 1223 |
else: |
| 1224 |
macro.add(VMulLOU32(dst=destLo, |
| 1225 |
src0=self.strideRef(tc, indices[i]), \ |
| 1226 |
src1=offset, \ |
| 1227 |
comment="mul d%u lower"%i)) |
| 1228 |
if not justOffset32: |
| 1229 |
macro.add(VMulHIU32(dst=destHi, |
| 1230 |
src0=self.strideRef(tc, indices[i]), \ |
| 1231 |
src1=offset, \ |
| 1232 |
comment="mul d%u upper"%i)) |
| 1233 |
else: |
| 1234 |
assert not isMirrorIdx |
| 1235 |
if not justOffset32: |
| 1236 |
|
| 1237 |
macro.add(VMovB32(dst=vgpr("Tmp+2", isMacro=True), src="s[\\sgprOffset%s]"%idxChars[i], \ |
| 1238 |
comment="sgprOffset -> vgprTmp+2")) |
| 1239 |
|
| 1240 |
macro.add(VMulLOU32(dst=vgpr("Tmp+0", isMacro=True), \ |
| 1241 |
src0=self.strideRef(tc, indices[i]), src1="v[\\vgprTmp+2]", \ |
| 1242 |
comment="other stride mul d%u lower"%i)) |
| 1243 |
macro.add(VMulHIU32(dst=vgpr("Tmp+1", isMacro=True), \ |
| 1244 |
src0=self.strideRef(tc, indices[i]), src1="v[\\vgprTmp+2]", \ |
| 1245 |
comment="mul d%u upper"%i)) |
| 1246 |
needAdd = 1 |
| 1247 |
|
| 1248 |
if needAdd: |
| 1249 |
writeDirectToAddr = 0 |
| 1250 |
destLo = vgpr("Addr+0", isMacro=True) |
| 1251 |
destHi = vgpr("Addr+1", isMacro=True) |
| 1252 |
|
| 1253 |
|
| 1254 |
srcLo = pendingOffset if pendingOffset else destLo |
| 1255 |
srcHi = 0 if pendingOffset else destHi |
| 1256 |
macro.add(VAddCOU32(dst=destLo, dst1=VCC(), \ |
| 1257 |
src0=srcLo, src1="v[\\vgprTmp+0]", \ |
| 1258 |
comment="accumulate %s lower"%idxChar)) |
| 1259 |
|
| 1260 |
|
| 1261 |
if not justOffset32: |
| 1262 |
macro.add(VAddCCOU32(dst=vgpr("Addr+1", isMacro=True), dst1=VCC(), \ |
| 1263 |
src0="v[\\vgprTmp+1]", src1=srcHi, src2=VCC(), \ |
| 1264 |
comment="accumulate %s upper"%idxChar)) |
| 1265 |
pendingOffset = None |
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
if pendingOffset != None: |
| 1271 |
destLo = vgpr("Addr+0", isMacro=True) |
| 1272 |
if writeDirectToAddr: |
| 1273 |
macro.add(VMovB32(dst=destLo, src=offset, comment="setup d0 lower")) |
| 1274 |
if not justOffset32: |
| 1275 |
macro.add(VMovB32(dst=vgpr("Addr+1", isMacro=True), src=0, comment="d0 upper")) |
| 1276 |
else: |
| 1277 |
macro.add(VAddCOU32(dst=destLo, dst1=VCC(), \ |
| 1278 |
src0=destLo, src1=pendingOffset, \ |
| 1279 |
comment="accumulate final pendingOffset")) |
| 1280 |
|
| 1281 |
|
| 1282 |
if tP != None and kernel["BufferLoad"] and self.states.srdShiftLeft[tc]: |
| 1283 |
macro.add(VAddU32(dst=vgpr("Addr+0", isMacro=True), \ |
| 1284 |
src0=hex(self.states.srdShiftLeft[tc]), \ |
| 1285 |
src1="v[\\vgprAddr+0]", \ |
| 1286 |
comment="add prepad for pointer shift")) |
| 1287 |
|
| 1288 |
bpeGR = tP["bpeGR"] if not tP["isM"] else tP["bpe"] |
| 1289 |
|
| 1290 |
if justOffset32: |
| 1291 |
macro.add(vectorStaticMultiply(vgpr("Addr+0", isMacro=True), vgpr("Addr+0", isMacro=True), bpeGR, None, "offset *= bytes/element")) |
| 1292 |
else: |
| 1293 |
macro.add(VLShiftLeftB64(dst=vgpr("Addr+0", 2, isMacro=True), \ |
| 1294 |
shiftHex=hex(log2(bpeGR)), \ |
| 1295 |
src="v[\\vgprAddr+0:\\vgprAddr+1]", \ |
| 1296 |
comment="offset *= bytes/element")) |
| 1297 |
module.add(macro) |
| 1298 |
|
| 1299 |
if kernel["ProblemType"]["StochasticRounding"] and not self.states.asmCaps["v_prng_b32"] : |
| 1300 |
module.add(PseudoRandomGenerator()) |
| 1301 |
|
| 1302 |
if not kernel["EnableMatrixInstruction"]: |
| 1303 |
|
| 1304 |
PLR = kernel["PrefetchLocalRead"] \ |
| 1305 |
if kernel["PrefetchLocalRead"] < kernel["LoopIters"] \ |
| 1306 |
else kernel["LoopIters"] - 1 |
| 1307 |
for m in range(0, 1+PLR): |
| 1308 |
macro = Macro("MAC_%ux%u_X%u" % (kernel["ThreadTile0"], kernel["ThreadTile1"], m), []) |
| 1309 |
component = Component.MAC.find(self) |
| 1310 |
if not component: |
| 1311 |
printExit("Assembly doesn't support datatype %s" % kernel["ProblemType"]["DataType"]) |
| 1312 |
innerModule = component(self, tPA, tPB, m, kernel["InnerUnroll"]) |
| 1313 |
for item in innerModule.items(): |
| 1314 |
macro.add(item) |
| 1315 |
module.add(macro) |
| 1316 |
|
| 1317 |
|
| 1318 |
if kernel["UseDotInstruction"]: |
| 1319 |
macro = Macro("MAC_%ux%u_X%u_OneIUI" % (kernel["ThreadTile0"],kernel["ThreadTile1"], 0), [""]) |
| 1320 |
component = Component.MAC.find(self) |
| 1321 |
if not component: |
| 1322 |
printExit("Assembly doesn't support datatype %s" % kernel["ProblemType"]["DataType"]) |
| 1323 |
innerModule = component(self, tPA, tPB, 0, 1) |
| 1324 |
for item in innerModule.items(): |
| 1325 |
macro.add(item) |
| 1326 |
module.add(macro) |
| 1327 |
|
| 1328 |
module.setNoOpt(True) |
| 1329 |
return module |
| 1330 |
|
| 1331 |
def checkResources(self, kernel, mkb: KernelBody): |
| 1332 |
|
| 1333 |
totalVgprs = self.vgprPool.size() |
| 1334 |
totalAgprs = self.agprPool.size() |
| 1335 |
totalSgprs = self.sgprPool.size() |
| 1336 |
|
| 1337 |
mkb.setGprs(totalVgprs=totalVgprs, totalAgprs=totalAgprs, totalSgprs=totalSgprs) |
| 1338 |
|
| 1339 |
if self.vgprPool.size() > self.states.regCaps["MaxVgpr"]: |
| 1340 |
self.states.overflowedResources = 1 |
| 1341 |
elif self.sgprPool.size() > self.states.regCaps["MaxSgpr"]: |
| 1342 |
self.states.overflowedResources = 2 |
| 1343 |
|
| 1344 |
|
| 1345 |
kernel["CUOccupancy"] = self.getOccupancy(kernel["NumThreads"], self.vgprPool.size(), self.sgprPool.size(), \ |
| 1346 |
self.getLdsSize(kernel), self.agprPool.size(), self.states.doubleVgpr) |
| 1347 |
if kernel["ScheduleIterAlg"] == 2 and kernel["CUOccupancy"] < 2: |
| 1348 |
self.states.overflowedResources = 6 |
| 1349 |
|
| 1350 |
vgprPerThreadPerOccupancy = self.states.regCaps["PhysicalMaxVgprCU"] // kernel["NumThreads"] |
| 1351 |
numWorkGroupsPerCU = vgprPerThreadPerOccupancy // mkb.getNextFreeVgpr() |
| 1352 |
if numWorkGroupsPerCU < 1: |
| 1353 |
self.states.overflowedResources = 4 |
| 1354 |
|
| 1355 |
if self.states.invalidLSUCode: |
| 1356 |
self.states.overflowedResources = 7 |
| 1357 |
|
| 1358 |
self.vgprPool.checkFinalState() |
| 1359 |
|
| 1360 |
if self.states.overflowedResources: |
| 1361 |
mkb.body.add(ValueEndif("overflowed resources")) |
| 1362 |
if self.states.overflowedResources == 1: |
| 1363 |
msg = "too many vgprs" |
| 1364 |
elif self.states.overflowedResources == 2: |
| 1365 |
msg = "too many sgprs" |
| 1366 |
elif self.states.overflowedResources == 3: |
| 1367 |
msg = "half store requires at least two elements per batch" |
| 1368 |
elif self.states.overflowedResources == 4: |
| 1369 |
msg = "Occupancy limit" |
| 1370 |
elif self.states.overflowedResources == 5: |
| 1371 |
if kernel["DirectToLdsA"] or kernel["DirectToLdsB"]: |
| 1372 |
msg = "cannot schedule local read with DirectToLds" |
| 1373 |
else: |
| 1374 |
msg = "reading and writing LDS at same time require 2 LDS buffer" |
| 1375 |
elif self.states.overflowedResources == 6: |
| 1376 |
msg = "SIA2 better with occupancy 2" |
| 1377 |
elif self.states.overflowedResources == 7: |
| 1378 |
msg = "invalid LSU code due to assertion fail" |
| 1379 |
else: |
| 1380 |
msg = "unknown" |
| 1381 |
|
| 1382 |
if self.debugConfig.printSolutionRejectionReason: |
| 1383 |
printWarning("%s overflowed resources. errorCode=%d, msg=\"%s\", vgprs=%u, sgprs=%u" \ |
| 1384 |
% (self.states.kernelName, self.states.overflowedResources, msg, \ |
| 1385 |
self.vgprPool.size(), self.sgprPool.size())) |
| 1386 |
mkb.body.add(SEndpgm(comment="overflowed resources"), 0) |
| 1387 |
mkb.body.add(ValueIf(value=0), 1) |
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
def loadBatchedAddress(self, kernel, Batch, tmpSgprResource: ContinuousRegister): |
| 1393 |
tmpSgpr = tmpSgprResource.idx |
| 1394 |
laneSC = tmpSgprResource.size |
| 1395 |
module = Module("loadBatchedAddress %s" % Batch) |
| 1396 |
module.addSpaceLine() |
| 1397 |
|
| 1398 |
|
| 1399 |
if not kernel["_GlobalAccumulation"]: |
| 1400 |
for idx in kernel["ProblemType"]["IndicesBatch"]: |
| 1401 |
if not isPackedIndex(kernel,idx): |
| 1402 |
module.add(SMulI32(dst=sgpr(tmpSgpr), src0=sgpr(Batch), src1=0x8, comment="offset of global buffer address")) |
| 1403 |
module.add(SLoadB64(dst=sgpr("AddressD", 2), base=sgpr("AddressD",2), soffset=sgpr(tmpSgpr), comment="load global buffer D address")) |
| 1404 |
|
| 1405 |
endCheckLabel = Label(self.labels.getName(f"label_skip_c_buffer_deref_{Batch}"), "") |
| 1406 |
module.add(BranchIfZero("Beta", kernel["ProblemType"]["ComputeDataType"].toEnum(), tmpSgpr, laneSC, endCheckLabel, \ |
| 1407 |
kernel['WavefrontSize'])) |
| 1408 |
|
| 1409 |
for idx in kernel["ProblemType"]["IndicesBatch"]: |
| 1410 |
if not isPackedIndex(kernel,idx): |
| 1411 |
module.add(SMulI32(dst=sgpr(tmpSgpr), src0=sgpr(Batch), src1=0x8, comment="offset of global buffer address")) |
| 1412 |
module.add(SLoadB64(dst=sgpr("AddressC", 2), base=sgpr("AddressC",2), soffset=sgpr(tmpSgpr), comment="load global buffer C address")) |
| 1413 |
|
| 1414 |
module.add(endCheckLabel) |
| 1415 |
|
| 1416 |
|
| 1417 |
endCheckLabel = Label(self.labels.getName(f"label_skip_ab_buffer_deref_{Batch}"), "") |
| 1418 |
module.add(SMovB32(dst=sgpr(tmpSgpr), src=hex(1), comment="check summation size")) |
| 1419 |
for i in range(0, self.states.numSgprSizesSum): |
| 1420 |
module.add(SMulI32(dst=sgpr(tmpSgpr), src0=sgpr("SizesSum+%u"%(i)), src1=sgpr(tmpSgpr), comment="check summation size")) |
| 1421 |
module.add(SCmpEQU32(src0=sgpr(tmpSgpr), src1=0, comment="skip buffer deref is size of summation is 0")) |
| 1422 |
module.add(SCBranchSCC1(labelName=endCheckLabel.getLabelName(), comment="skip buffer deref is size of summation is 0")) |
| 1423 |
module.add(BranchIfZero("Alpha", kernel["ProblemType"]["ComputeDataType"].toEnum(), tmpSgpr, laneSC, endCheckLabel, \ |
| 1424 |
kernel['WavefrontSize'])) |
| 1425 |
|
| 1426 |
module.add(SMulI32(dst=sgpr(tmpSgpr), src0=sgpr(Batch), src1=0x8, comment="offset of global buffer address")) |
| 1427 |
for idx in kernel["ProblemType"]["IndicesBatch"]: |
| 1428 |
if not isPackedIndex(kernel,idx): |
| 1429 |
module.add(SLoadB64(dst=sgpr("AddressA", 2), base=sgpr("AddressA",2), soffset=sgpr(tmpSgpr), comment="load global buffer A address")) |
| 1430 |
module.add(SLoadB64(dst=sgpr("AddressB", 2), base=sgpr("AddressB",2), soffset=sgpr(tmpSgpr), comment="load global buffer B address")) |
| 1431 |
|
| 1432 |
module.add(endCheckLabel) |
| 1433 |
|
| 1434 |
return module |
| 1435 |
|
| 1436 |
|
| 1437 |
def getKernelArgLoadModule(self, kernel, sgprStartIdx, numsOfLoad, preloadNum): |
| 1438 |
kernelArgs = Module("load arguments") |
| 1439 |
kernelArgs.addComment1("Load Kernel Args") |
| 1440 |
if self.debugConfig.debugKernel: |
| 1441 |
kernelArgs.add(self.argLoader.loadKernArg("AddressDbg", "KernArgAddress", dword=2)) |
| 1442 |
self.argLoader.resetOffset() |
| 1443 |
kernelArgs.addModuleAsFlatItems(self.argLoader.loadAllKernArg(sgprStartIdx, "KernArgAddress", numsOfLoad, preloadNum)) |
| 1444 |
if kernel["ProblemType"]["UseScaleAB"] == "Scalar": |
| 1445 |
sgprOffset = self.argLoader.getOffset() |
| 1446 |
for preloadScale, name in zip([self.states.preloadScaleA, self.states.preloadScaleB], ['A','B']): |
| 1447 |
if preloadScale: |
| 1448 |
kernelArgs.add(self.argLoader.loadKernArg("AddressScale%s"%name, "KernArgAddress", sgprOffset=hex(sgprOffset), dword=2)) |
| 1449 |
sgprOffset += (self.states.rpga * self.states.bpr) |
| 1450 |
return kernelArgs |
| 1451 |
|
| 1452 |
def localReadAddresses(self, kernel, tPA, tPB, tPM): |
| 1453 |
module = Module("Local Read Addresses") |
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
module.addComment2("Local Read Addresses") |
| 1459 |
|
| 1460 |
|
| 1461 |
module.addComment1("local read addresses: tile assignments a/b") |
| 1462 |
module.add(self.lraTileAssignment(kernel, tPA, tPB)) |
| 1463 |
|
| 1464 |
|
| 1465 |
module.addComment1("local read addresses: final offsets a") |
| 1466 |
module.add(self.lraFinalOffset(kernel, tPA)) |
| 1467 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 1468 |
module.addComment1("local read addresses: final offsets metadata") |
| 1469 |
module.add(self.lraFinalOffset(kernel, tPM)) |
| 1470 |
module.addComment1("local read addresses: final offsets b") |
| 1471 |
module.add(self.lraFinalOffset(kernel, tPB)) |
| 1472 |
|
| 1473 |
|
| 1474 |
module.addComment1("local read addresses: declare addresses a") |
| 1475 |
module.add(self.lraDeclareAddresses(kernel, tPA)) |
| 1476 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 1477 |
module.addComment1("local read addresses: declare addresses metadata") |
| 1478 |
module.add(self.lraDeclareAddresses(kernel, tPM)) |
| 1479 |
module.addComment1("local read addresses: declare addresses b") |
| 1480 |
module.add(self.lraDeclareAddresses(kernel, tPB)) |
| 1481 |
|
| 1482 |
if self.states.a.numVgprLocalReadAddr > 0: |
| 1483 |
module.add(self.lraSwapAddressesForDTLPad(kernel, tPA)) |
| 1484 |
if self.states.b.numVgprLocalReadAddr > 0: |
| 1485 |
module.add(self.lraSwapAddressesForDTLPad(kernel, tPB)) |
| 1486 |
if self.states.m.numVgprLocalReadAddr > 0: |
| 1487 |
module.add(self.lraSwapAddressesForDTLPad(kernel, tPM)) |
| 1488 |
|
| 1489 |
return module |
| 1490 |
|
| 1491 |
def localWriteAddresses(self, kernel, tPA, tPB, tPM): |
| 1492 |
module = Module("Local Write Addresses") |
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
module.addComment2("Local Write Addresses") |
| 1498 |
|
| 1499 |
|
| 1500 |
module.add(self.lwaTileAssignment(kernel, tPA)) |
| 1501 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 1502 |
module.add(self.lwaTileAssignment(kernel, tPM)) |
| 1503 |
module.add(self.lwaTileAssignment(kernel, tPB)) |
| 1504 |
|
| 1505 |
|
| 1506 |
module.add(self.lwaUnrollAssignment(kernel, tPA)) |
| 1507 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 1508 |
module.add(self.lwaUnrollAssignment(kernel, tPM)) |
| 1509 |
module.add(self.lwaUnrollAssignment(kernel, tPB)) |
| 1510 |
|
| 1511 |
|
| 1512 |
module.addComment1("local write addresses: first offset a") |
| 1513 |
module.add(self.lwaFirstOffset(kernel, tPA)) |
| 1514 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 1515 |
module.addComment1("local write addresses: first offset metadata") |
| 1516 |
module.add(self.lwaFirstOffset(kernel, tPM)) |
| 1517 |
module.addComment1("local write addresses: first offset b") |
| 1518 |
module.add(self.lwaFirstOffset(kernel, tPB)) |
| 1519 |
|
| 1520 |
return module |
| 1521 |
|
| 1522 |
def wgmXCC(self, kernel, tmpSgprNumWorkGroups): |
| 1523 |
module = Module("WGMXCC") |
| 1524 |
module.addComment1("remap workgroup to XCCs") |
| 1525 |
with self.allocTmpSgpr(6, 2) as tmpSgprRes: |
| 1526 |
tmpSgpr = tmpSgprRes.idx |
| 1527 |
tmpSgpr0 = tmpSgpr+1 |
| 1528 |
tmpSgpr1 = tmpSgpr0+1 |
| 1529 |
tmpSgpr2 = tmpSgpr1+1 |
| 1530 |
WGMXCCSgpr = tmpSgpr2+1 |
| 1531 |
CU_CountSgpr = WGMXCCSgpr+1 |
| 1532 |
|
| 1533 |
module.add(SLShiftRightB32(dst=sgpr(WGMXCCSgpr), shiftHex=hex(16), src=sgpr("WGM"), comment="Get WGMXCC")) |
| 1534 |
module.add(SFf1B32(dst=sgpr(WGMXCCSgpr), src=sgpr(WGMXCCSgpr), comment="Get log(WGMXCC)")) |
| 1535 |
module.add(SLShiftRightB32(dst=sgpr(CU_CountSgpr), shiftHex=hex(22), src=sgpr("WGM"), comment="Get CU_Count")) |
| 1536 |
|
| 1537 |
label_skipWGMXCC = Label(label="skip_WGMXCC", comment="skip WGMXCC if no enough WGs to remap") |
| 1538 |
|
| 1539 |
module.addComment0("remap WGs if WGMXCC > 1 ( log(WGMXCC) > 0 )") |
| 1540 |
module.add(SCmpGtI32(src0=sgpr(WGMXCCSgpr), src1=0)) |
| 1541 |
module.add(SCBranchSCC0(label_skipWGMXCC.getLabelName())) |
| 1542 |
|
| 1543 |
module.addComment0("only remap WGs in the range") |
| 1544 |
tmpVgpr = self.vgprPool.checkOut(2) |
| 1545 |
tmpVgprRes = ContinuousRegister(tmpVgpr, 2) |
| 1546 |
module.add(SLShiftRightB32(dst=sgpr(tmpSgpr0), shiftHex=sgpr(WGMXCCSgpr), src=sgpr(tmpSgprNumWorkGroups))) |
| 1547 |
module.add(SLShiftLeftB32(dst=sgpr(tmpSgpr0), shiftHex=sgpr(WGMXCCSgpr), src=sgpr(tmpSgpr0))) |
| 1548 |
module.add(SCmpGeU32(src0=sgpr("WorkGroup0"), src1=sgpr(tmpSgpr0))) |
| 1549 |
module.add(SCBranchSCC1(label_skipWGMXCC.getLabelName())) |
| 1550 |
|
| 1551 |
label_XCCG_nonzero = Label(label="XCCG_nonzero", comment="") |
| 1552 |
module.add(SCmpEQU32(src0=sgpr(CU_CountSgpr), src1=0, comment="CU_Count == 0 ?")) |
| 1553 |
module.add(SCBranchSCC0(label_XCCG_nonzero.getLabelName())) |
| 1554 |
|
| 1555 |
|
| 1556 |
module.add(SLShiftRightB32(dst=sgpr(tmpSgpr0), shiftHex=sgpr(WGMXCCSgpr), src=sgpr("WorkGroup0"))) |
| 1557 |
module.add(SBfmB32(dst=sgpr(tmpSgpr1), src0=sgpr(WGMXCCSgpr), src1=0)) |
| 1558 |
module.add(SAndB32(dst=sgpr(tmpSgpr1), src0=sgpr("WorkGroup0"), src1=sgpr(tmpSgpr1))) |
| 1559 |
module.add(SLShiftRightB32(dst=sgpr(tmpSgpr2), shiftHex=sgpr(WGMXCCSgpr), src=sgpr(tmpSgprNumWorkGroups))) |
| 1560 |
module.add(SMulI32(dst=sgpr(tmpSgpr1), src0=sgpr(tmpSgpr1), src1=sgpr(tmpSgpr2))) |
| 1561 |
module.add(SAddU32(dst=sgpr("WorkGroup0"), src0=sgpr(tmpSgpr0), src1=sgpr(tmpSgpr1))) |
| 1562 |
module.add(SBranch(label_skipWGMXCC.getLabelName())) |
| 1563 |
|
| 1564 |
|
| 1565 |
module.add(label_XCCG_nonzero) |
| 1566 |
module.addComment0("temp0 = (wg//CU_Count)*CU_Count") |
| 1567 |
module.add(scalarUInt32DivideAndRemainder(qReg=tmpSgpr0, dReg="WorkGroup0", divReg=CU_CountSgpr, rReg=tmpSgpr1, tmpVgprRes=tmpVgprRes, wavewidth=kernel["WavefrontSize"], doRemainder=True, comment="wg//CU_Count")) |
| 1568 |
module.add(SMulI32(dst=sgpr(tmpSgpr0), src0=sgpr(tmpSgpr0), src1=sgpr(CU_CountSgpr))) |
| 1569 |
module.addComment0("temp1 = (wg%CU_Count)//WGMXCC") |
| 1570 |
module.add(SLShiftRightB32(dst=sgpr(tmpSgpr1), shiftHex=sgpr(WGMXCCSgpr), src=sgpr(tmpSgpr1))) |
| 1571 |
module.addComment0("temp0 = temp0 + temp1") |
| 1572 |
module.add(SAddU32(dst=sgpr(tmpSgpr0), src0=sgpr(tmpSgpr0), src1=sgpr(tmpSgpr1))) |
| 1573 |
module.addComment0("temp1 = (wg%WGMXCC) * ((WGs - (WGs//CU_Count) * CU_Count) if (wg > (WGs//CU_Count) * CU_Count) else CU_Count)//WGMXCC") |
| 1574 |
module.add(scalarUInt32DivideAndRemainder(qReg=tmpSgpr1, dReg=tmpSgprNumWorkGroups, divReg=CU_CountSgpr, rReg=-1, tmpVgprRes=tmpVgprRes, wavewidth=kernel["WavefrontSize"], doRemainder=False, comment="WGs//CU_Count")) |
| 1575 |
module.add(SMulI32(dst=sgpr(tmpSgpr1), src0=sgpr(tmpSgpr1), src1=sgpr(CU_CountSgpr))) |
| 1576 |
module.add(SSubU32(dst=sgpr(tmpSgpr2), src0=sgpr(tmpSgprNumWorkGroups), src1=sgpr(tmpSgpr1))) |
| 1577 |
module.add(SCmpGtU32(src0=sgpr("WorkGroup0"), src1=sgpr(tmpSgpr1))) |
| 1578 |
module.add(SCSelectB32(dst=sgpr(tmpSgpr1), src0=sgpr(tmpSgpr2), src1=sgpr(CU_CountSgpr))) |
| 1579 |
module.add(SLShiftRightB32(dst=sgpr(tmpSgpr1), shiftHex=sgpr(WGMXCCSgpr), src=sgpr(tmpSgpr1))) |
| 1580 |
module.add(SBfmB32(dst=sgpr(tmpSgpr2), src0=sgpr(WGMXCCSgpr), src1=0)) |
| 1581 |
module.add(SAndB32(dst=sgpr(tmpSgpr2), src0=sgpr("WorkGroup0"), src1=sgpr(tmpSgpr2))) |
| 1582 |
self.vgprPool.checkIn(tmpVgpr) |
| 1583 |
module.add(SMulI32(dst=sgpr(tmpSgpr1), src0=sgpr(tmpSgpr1), src1=sgpr(tmpSgpr2))) |
| 1584 |
module.addComment0("WorkGroup0 = temp0 + temp1") |
| 1585 |
module.add(SAddU32(dst=sgpr("WorkGroup0"), src0=sgpr(tmpSgpr0), src1=sgpr(tmpSgpr1))) |
| 1586 |
|
| 1587 |
module.add(label_skipWGMXCC) |
| 1588 |
return module |
| 1589 |
|
| 1590 |
def remapWgSerial(self, kernel, earlyStop=True): |
| 1591 |
module = Module("RemapWgSerial") |
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
if "WorkGroup2" in self.sgprs: |
| 1597 |
with self.allocTmpSgpr(2) as tmpSgpr: |
| 1598 |
module.addComment1("remap wg from 1D(idxWG012) to 3D(wg2,wg1,wg0)") |
| 1599 |
module.addComment0("wg2 = idxWG012 * smallMagicNumber(1/(numWG0*numWG1))") |
| 1600 |
tmpVgpr = self.vgprPool.checkOut(2) |
| 1601 |
tmpVgprRes = ContinuousRegister(tmpVgpr, 2) |
| 1602 |
module.add(SMulI32(dst=sgpr(tmpSgpr.idx), src0=sgpr("NumWorkGroups0"), src1=sgpr("NumWorkGroups1"))) |
| 1603 |
if kernel["GlobalSplitU"] != 0: |
| 1604 |
module.add(SAndB32(dst=sgpr(tmpSgpr.idx+1), src0=sgpr("GSU"), src1=hex(0x3FFF), comment="Restore GSU")) |
| 1605 |
module.add(SMulI32(dst=sgpr(tmpSgpr.idx), src0=sgpr(tmpSgpr.idx), src1=sgpr(tmpSgpr.idx+1))) |
| 1606 |
module.add(scalarUInt32DivideAndRemainder(qReg=tmpSgpr.idx, dReg="WorkGroup0", divReg=tmpSgpr.idx, rReg=tmpSgpr.idx+1,\ |
| 1607 |
tmpVgprRes=tmpVgprRes, wavewidth=kernel["WavefrontSize"], doRemainder=False)) |
| 1608 |
module.add(SMovB32(dst=sgpr("WorkGroup2"), src=sgpr(tmpSgpr.idx))) |
| 1609 |
module.addComment0("idxWG01 = idxWG012 - wg2 * numWG0 * numWG1") |
| 1610 |
module.add(SMulI32(dst=sgpr(tmpSgpr.idx), src0=sgpr("NumWorkGroups1"), src1=sgpr("NumWorkGroups0"))) |
| 1611 |
module.add(SMulI32(dst=sgpr(tmpSgpr.idx), src0=sgpr(tmpSgpr.idx), src1=sgpr("WorkGroup2"))) |
| 1612 |
if kernel["GlobalSplitU"] != 0: |
| 1613 |
module.add(SMulI32(dst=sgpr(tmpSgpr.idx), src0=sgpr(tmpSgpr.idx), src1=sgpr(tmpSgpr.idx+1))) |
| 1614 |
module.add(SSubU32(dst=sgpr("WorkGroup0"), src0=sgpr("WorkGroup0"), src1=sgpr(tmpSgpr.idx))) |
| 1615 |
module.addComment0("wg1 = idxWG01 * smallMagicNumber(1/numWG0)") |
| 1616 |
module.add(scalarUInt32DivideAndRemainder(qReg=tmpSgpr.idx, dReg="WorkGroup0", divReg="NumWorkGroups0", rReg=tmpSgpr.idx+1,\ |
| 1617 |
tmpVgprRes=tmpVgprRes, wavewidth=kernel["WavefrontSize"], doRemainder=False)) |
| 1618 |
self.vgprPool.checkIn(tmpVgpr) |
| 1619 |
module.add(SMovB32(dst=sgpr("WorkGroup1"), src=sgpr(tmpSgpr.idx))) |
| 1620 |
module.addComment0("wg0 = idxWG01 - wg1 * numWG0") |
| 1621 |
module.add(SMulI32(dst=sgpr(tmpSgpr.idx), src0=sgpr("WorkGroup1"), src1=sgpr("NumWorkGroups0"))) |
| 1622 |
module.add(SSubU32(dst=sgpr("WorkGroup0"), src0=sgpr("WorkGroup0"), src1=sgpr(tmpSgpr.idx))) |
| 1623 |
|
| 1624 |
|
| 1625 |
if earlyStop: |
| 1626 |
module.addComment1("Early stop if wg exceed") |
| 1627 |
module.add(SCmpGeU32(src0=sgpr("WorkGroup2"), src1=sgpr("SizesFree+2"))) |
| 1628 |
label_EarlyStop = Label(self.labels.getNameInc("EarlyStop_if_wg_exceed"), "") |
| 1629 |
label_nonEarlyStop = Label(self.labels.getNameInc("NoEarlyStop_wgExceed"), "") |
| 1630 |
module.add(SCBranchSCC0(labelName=label_nonEarlyStop.getLabelName())) |
| 1631 |
module.add(label_EarlyStop) |
| 1632 |
module.add(SEndpgm()) |
| 1633 |
module.add(label_nonEarlyStop) |
| 1634 |
return module |
| 1635 |
|
| 1636 |
def defineAndResources(self, kernel, tPA, tPB, tPM): |
| 1637 |
module = Module("allocateResources") |
| 1638 |
module.add(self.macroAndSet(kernel, tPA, tPB)) |
| 1639 |
|
| 1640 |
module.addComment2("Allocate Resources") |
| 1641 |
moduleArgs = Module("load arguments") |
| 1642 |
moduleRegInit = Module("Init regs") |
| 1643 |
|
| 1644 |
tPM = tPA["tpsMetadata"] if tPA["is_sparse"] else tPB["tpsMetadata"] |
| 1645 |
|
| 1646 |
sgprNumsOfGemm = None |
| 1647 |
|
| 1648 |
if self.do["PreLoop"]: |
| 1649 |
|
| 1650 |
|
| 1651 |
sgprNumsOfGemm = self.sgprPool.checkOut(1, preventOverflow=False) |
| 1652 |
|
| 1653 |
self.kernArgOffset = 0 |
| 1654 |
self.argLoader = ArgumentLoader() |
| 1655 |
self.externalArgLoader = ArgumentLoader() |
| 1656 |
|
| 1657 |
|
| 1658 |
sgprArgType = self.sgprPool.checkOut(1, preventOverflow=False) |
| 1659 |
commonArgs = Module("load arguments") |
| 1660 |
commonArgs.addComment1("Load num of Gemms") |
| 1661 |
commonArgs.add(self.argLoader.loadKernArg(sgprNumsOfGemm, "KernArgAddress", 0, dword=1)) |
| 1662 |
|
| 1663 |
sgprPackedArgs = self.sgprPool.checkOut(1, preventOverflow=False) |
| 1664 |
|
| 1665 |
commonArgs.addComment1("Load packed kernel args (StaggerU/GSU)") |
| 1666 |
commonArgs.add(self.argLoader.loadKernArg(sgprPackedArgs, "KernArgAddress", 4, dword=1)) |
| 1667 |
commonArgs.addComment1("Load WGM data") |
| 1668 |
commonArgs.add(self.argLoader.loadKernArg("WGM", "KernArgAddress", 8, dword=1)) |
| 1669 |
tmpSgprNumWorkGroups = self.sgprPool.checkOut(1, preventOverflow=False) |
| 1670 |
commonArgs.addComment1("Load num of WGs") |
| 1671 |
commonArgs.add(self.argLoader.loadKernArg(tmpSgprNumWorkGroups, "KernArgAddress", 12, dword=1)) |
| 1672 |
|
| 1673 |
|
| 1674 |
load = self.states.numSgprToLoad |
| 1675 |
sgprStart = self.sgprs["SizesFree"] |
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
hbmArgs = Module("load HBM arguments") |
| 1680 |
hbmArgs.addComment1("Load address of kernel arguments") |
| 1681 |
hbmArgs.add(self.argLoader.loadKernArg("KernArgAddress", "KernArgAddress", self.states.userArgsInfo.commonArgsSize, dword=2)) |
| 1682 |
|
| 1683 |
moduleArgs.addModuleAsFlatItems(deepcopy(commonArgs)) |
| 1684 |
moduleArgs.add(SWaitCnt(kmcnt=0, comment="load args")) |
| 1685 |
moduleArgs.add(SLShiftRightB32(dst=sgpr(sgprArgType), shiftHex=hex(30), src=sgpr(sgprNumsOfGemm), comment="Get arg type")) |
| 1686 |
moduleArgs.add(SAndB32(dst=sgpr(sgprNumsOfGemm), src0=hex(0x3FFFFFFF), src1=sgpr(sgprNumsOfGemm), comment="Get nums of gemm")) |
| 1687 |
|
| 1688 |
if ((kernel["GlobalSplitUAlgorithm"] == 'MultipleBufferSingleKernel')): |
| 1689 |
extReadEpilogueLabeltmp = Label(label=self.labels.getNameInc("LoadExternalEpilogueStruct"), comment="") |
| 1690 |
moduleArgs.addComment0("Check if custom structure pointer is null") |
| 1691 |
if kernel["ProblemType"]["SupportUserArgs"]: |
| 1692 |
moduleArgs.add(SCmpEQU32(src0=sgpr(sgprArgType), src1=2, comment="ArgType == 2 ?")) |
| 1693 |
moduleArgs.add(SCBranchSCC0(labelName=extReadEpilogueLabeltmp.getLabelName())) |
| 1694 |
moduleArgs.addComment1("Grouped Gemm: Load address of external kernel arguments") |
| 1695 |
moduleArgs.add(self.argLoader.loadKernArg("AddressTD", "KernArgAddress", hex(self.states.userArgsInfo.commonArgsSize+16), dword=2)) |
| 1696 |
moduleArgs.add(self.argLoader.loadKernArg("Synchronizer", "KernArgAddress", hex(self.states.userArgsInfo.commonArgsSize+8), dword=2)) |
| 1697 |
moduleArgs.add(extReadEpilogueLabeltmp) |
| 1698 |
|
| 1699 |
moduleArgs.add(SCmpEQU32(src0=sgpr(sgprArgType), src1=(0), comment="Is kernel args")) |
| 1700 |
labelHBM = Label("HBMArgs", comment="") |
| 1701 |
labelLoadEnd = Label("LoadArgsEnd", comment="") |
| 1702 |
moduleArgs.add(SCBranchSCC0(labelName=labelHBM.getLabelName())) |
| 1703 |
moduleArgs.add(SAddU32(dst=sgpr("KernArgAddress"), src0=sgpr("KernArgAddress"), src1=hex(self.states.userArgsInfo.commonArgsSize), comment="Shift common args")) |
| 1704 |
moduleArgs.add(SAddCU32(dst=sgpr("KernArgAddress+1"), src0=sgpr("KernArgAddress+1"), src1=0)) |
| 1705 |
moduleArgs.addModuleAsFlatItems(self.getKernelArgLoadModule(kernel, sgprStart, load, 0)) |
| 1706 |
if self.states.numSgprPreload > 0: |
| 1707 |
moduleArgs.add(SWaitCnt(kmcnt=0, comment="preload")) |
| 1708 |
moduleArgs.add(SBranch(labelName=labelLoadEnd.getLabelName())) |
| 1709 |
moduleArgs.add(labelHBM) |
| 1710 |
moduleArgs.addModuleAsFlatItems(deepcopy(hbmArgs)) |
| 1711 |
moduleArgs.add(SWaitCnt(kmcnt=0, comment="wait for args to load")) |
| 1712 |
moduleArgs.add(labelLoadEnd) |
| 1713 |
|
| 1714 |
if self.states.numSgprPreload > 0: |
| 1715 |
common_kern_entry = Label(label="common_kernel_entry", comment="for both preload/non-preload common code") |
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
moduleArgs.add(SBranch(common_kern_entry.getLabelName())) |
| 1720 |
total_inst_dwords = 0 |
| 1721 |
for inst in moduleArgs.items(): |
| 1722 |
if isinstance(inst, (BranchInstruction, SWaitCnt, CommonInstruction)): |
| 1723 |
total_inst_dwords = total_inst_dwords + 1 |
| 1724 |
elif isinstance(inst, (SMemLoadInstruction)): |
| 1725 |
total_inst_dwords = total_inst_dwords + 2 |
| 1726 |
assert total_inst_dwords <= 64 |
| 1727 |
moduleArgs.addComment1("pad %u snops to satisfy 0x100 code size for Preload Backward Compatibility Prologue" % (64 - total_inst_dwords)) |
| 1728 |
for i in range(64 - total_inst_dwords): |
| 1729 |
moduleArgs.add(SNop(waitState=0, comment="")) |
| 1730 |
moduleArgs.add(Label("Preload_Offset_Start", "")) |
| 1731 |
|
| 1732 |
preloadSgprStartIdx = self.states.rpga |
| 1733 |
moduleArgs.add(SAndB32(dst=sgpr(sgprNumsOfGemm), src0=hex(0x3FFFFFFF), src1=sgpr(preloadSgprStartIdx), comment="Get nums of gemm")) |
| 1734 |
moduleArgs.add(SLShiftRightB32(dst=sgpr(sgprArgType), shiftHex=hex(30), src=sgpr(preloadSgprStartIdx), comment="Get arg type")) |
| 1735 |
|
| 1736 |
if ((kernel["GlobalSplitUAlgorithm"] == 'MultipleBufferSingleKernel')): |
| 1737 |
extReadEpilogueLabeltmp = Label(label=self.labels.getNameInc("LoadExternalEpilogueStruct"), comment="") |
| 1738 |
moduleArgs.addComment0("Check if custom structure pointer is null") |
| 1739 |
if kernel["ProblemType"]["SupportUserArgs"]: |
| 1740 |
moduleArgs.add(SCmpEQU32(src0=sgpr(sgprArgType), src1=2, comment="ArgType == 2 ?")) |
| 1741 |
moduleArgs.add(SCBranchSCC0(labelName=extReadEpilogueLabeltmp.getLabelName())) |
| 1742 |
moduleArgs.add(SMovB32(dst=sgpr("Synchronizer+1"), src=sgpr(preloadSgprStartIdx+7), comment="Load Synchronizer data")) |
| 1743 |
moduleArgs.add(SMovB32(dst=sgpr("Synchronizer"), src=sgpr(preloadSgprStartIdx+6), comment="Load Synchronizer data")) |
| 1744 |
moduleArgs.add(SMovB32(dst=sgpr("AddressTD+1"), src=sgpr(preloadSgprStartIdx+9), comment="Load AddressTD data")) |
| 1745 |
moduleArgs.add(SMovB32(dst=sgpr("AddressTD"), src=sgpr(preloadSgprStartIdx+8), comment="Load AddressTD data")) |
| 1746 |
moduleArgs.add(extReadEpilogueLabeltmp) |
| 1747 |
|
| 1748 |
moduleArgs.add(SMovB32(dst=sgpr(sgprPackedArgs), src=sgpr(preloadSgprStartIdx+1), comment="Preload internal args")) |
| 1749 |
moduleArgs.add(SCmpEQU32(src0=sgpr(sgprArgType), src1=(0), comment="Is kernel args")) |
| 1750 |
preloadLabelHBM = Label("Preload_HBMArgs", comment="") |
| 1751 |
perloadLabelLoadEnd = Label("Preload_LoadArgsEnd", comment="") |
| 1752 |
moduleArgs.add(SCBranchSCC0(labelName=preloadLabelHBM.getLabelName())) |
| 1753 |
moduleArgs.add(SAddU32(dst=sgpr("KernArgAddress"), src0=sgpr("KernArgAddress"), src1=hex(self.states.userArgsInfo.commonArgsSize), comment="Shift common args")) |
| 1754 |
moduleArgs.add(SAddCU32(dst=sgpr("KernArgAddress+1"), src0=sgpr("KernArgAddress+1"), src1=0)) |
| 1755 |
self.argLoader.resetOffset() |
| 1756 |
moduleArgs.addModuleAsFlatItems(self.getKernelArgLoadModule(kernel, sgprStart, load, self.states.numSgprPreload - self.states.userArgsInfo.commonArgsNum)) |
| 1757 |
i = self.states.userArgsInfo.commonArgsNum |
| 1758 |
while i < self.states.numSgprPreload: |
| 1759 |
dstIndex = sgprStart+i-self.states.userArgsInfo.commonArgsNum |
| 1760 |
srcIndex = preloadSgprStartIdx+i |
| 1761 |
if (i + 1 < self.states.numSgprPreload) and (dstIndex % 2 == 0) and (srcIndex % 2 == 0): |
| 1762 |
moduleArgs.add(SMovB64(dst=sgpr(sgprStart+i-self.states.userArgsInfo.commonArgsNum, 2), src=sgpr(preloadSgprStartIdx+i, 2), comment="move preload data to correct sgpr")) |
| 1763 |
i += 2 |
| 1764 |
else: |
| 1765 |
moduleArgs.add(SMovB32(dst=sgpr(sgprStart+i-self.states.userArgsInfo.commonArgsNum), src=sgpr(preloadSgprStartIdx+i), comment="move preload data to correct sgpr")) |
| 1766 |
i += 1 |
| 1767 |
moduleArgs.add(SBranch(labelName=perloadLabelLoadEnd.getLabelName())) |
| 1768 |
moduleArgs.add(preloadLabelHBM) |
| 1769 |
moduleArgs.add(SMovB64(dst=sgpr("KernArgAddress", 2), src=sgpr(preloadSgprStartIdx+4, 2), comment="Load address of kernel arguments")) |
| 1770 |
moduleArgs.add(perloadLabelLoadEnd) |
| 1771 |
moduleArgs.add(SMovB32(dst=sgpr("WGM"), src=sgpr(preloadSgprStartIdx+2), comment="Preload internal args2")) |
| 1772 |
moduleArgs.add(SMovB32(dst=sgpr(tmpSgprNumWorkGroups), src=sgpr(preloadSgprStartIdx+3), comment="Load num of WGs")) |
| 1773 |
|
| 1774 |
moduleRegInit.add(common_kern_entry) |
| 1775 |
for i in range(kernel["ProblemType"]["NumIndicesC"]): |
| 1776 |
moduleRegInit.add(SMovB32(dst=sgpr("WorkGroup0+%u"%i), src=sgpr(preloadSgprStartIdx+self.states.numSgprPreload+i), \ |
| 1777 |
comment="restore workgroup id")) |
| 1778 |
|
| 1779 |
moduleRegInit.add(SAndB32(dst=sgpr("StaggerU"), src0=sgpr(sgprPackedArgs), src1=hex(0xFFFF0000), comment="Restore StaggerU related vars")) |
| 1780 |
moduleRegInit.add(SLShiftRightB32(dst=sgpr("StaggerU"), shiftHex=hex(16), src=sgpr("StaggerU"))) |
| 1781 |
if kernel["GlobalSplitU"] != 0: |
| 1782 |
moduleRegInit.add(SAndB32(dst=sgpr("GSU"), src0=sgpr(sgprPackedArgs), src1=hex(0xFFFF), comment="Restore GSUConfig and GSU")) |
| 1783 |
|
| 1784 |
if kernel["ProblemType"]["SupportUserArgs"]: |
| 1785 |
moduleRegInit.add(SMovB32(dst=sgpr("ArgType"),src=sgpr(sgprArgType))) |
| 1786 |
|
| 1787 |
self.sgprPool.checkIn(sgprPackedArgs) |
| 1788 |
|
| 1789 |
if kernel["StorePriorityOpt"]: |
| 1790 |
moduleRegInit.add(SSetPrior(prior=3, comment="optimization store")) |
| 1791 |
|
| 1792 |
if self.do["PreLoop"]: |
| 1793 |
if self.db["InitSgpr"] & 0x1: |
| 1794 |
moduleRegInit.addComment1("Init SGPRs") |
| 1795 |
for i in range(self.states.firstInitSgpr, self.sgprPool.size()): |
| 1796 |
moduleRegInit.add(SMovB32(dst=sgpr(i), src=hex(self.consts.initSgprValue), comment="InitSgpr&0x1")) |
| 1797 |
moduleRegInit.addSpaceLine() |
| 1798 |
|
| 1799 |
if self.db["InitVgpr"] & 0x1: |
| 1800 |
moduleRegInit.addComment1("Init VGPRs") |
| 1801 |
for i in range(1, self.states.totalVgprs): |
| 1802 |
moduleRegInit.add(VMovB32(dst=vgpr(i), src=hex(self.consts.initVgprValue), comment="InitVgpr&0x1")) |
| 1803 |
moduleRegInit.addSpaceLine() |
| 1804 |
|
| 1805 |
|
| 1806 |
moduleRegInit.add(SMovB32(dst=mgpr(0), src=hex(kernel["LdsNumBytes"]), |
| 1807 |
comment="LDS clamp at %u bytes"%(kernel["LdsNumBytes"]))) |
| 1808 |
|
| 1809 |
|
| 1810 |
moduleRegInit.add(VMovB32(dst=vgpr("Serial"), src=vgpr(0), comment="thread serial id")) |
| 1811 |
|
| 1812 |
if self.states.kernel["WavefrontSize"] == 32: |
| 1813 |
moduleRegInit.add(SMovB32(dst=VCC(setHi=True), src=0, comment="Ensure hi bits are zero")) |
| 1814 |
|
| 1815 |
waitForScaleAB = False |
| 1816 |
moduleScaleAB = Module("Load ScaleAB") |
| 1817 |
if kernel["ProblemType"]["UseScaleAB"] == "Scalar": |
| 1818 |
for preloadScale, name in zip([self.states.preloadScaleA, self.states.preloadScaleB], ['A','B']): |
| 1819 |
if preloadScale: |
| 1820 |
waitForScaleAB = True |
| 1821 |
moduleScaleAB.add(SMovB32(dst=sgpr("Scale%s"%name), src=1.0 , comment="init as 1" )) |
| 1822 |
label = Label(self.labels.getNameInc("Scale%sValid"%name), "") |
| 1823 |
moduleScaleAB.add(BranchIfZero("AddressScale%s"%name, DataType('int64').toEnum(), -1, int(kernel["WavefrontSize"]/32), label, kernel["WavefrontSize"])) |
| 1824 |
|
| 1825 |
moduleScaleAB.add(SLoadB32(dst=sgpr("Scale%s"%name), base=sgpr("AddressScale%s"%name,2), soffset=0, comment="load scale%s"%name)) |
| 1826 |
moduleScaleAB.add(label) |
| 1827 |
|
| 1828 |
moduleWg = Module("Calculate Workgroup") |
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
self.vgprPool.add(self.states.a.startVgprValu , \ |
| 1833 |
self.states.lastValuAB - self.states.a.startVgprValu , "ValuAB") |
| 1834 |
moduleWg.addComment0("init: add vgpr [%u...%u) to pool" % \ |
| 1835 |
(self.states.a.startVgprValu, self.states.lastValuAB+self.states.a.startVgprValu)) |
| 1836 |
|
| 1837 |
self.vgprPool.add(self.states.c.startVgprValu, \ |
| 1838 |
self.states.c.numVgprValu, "ValuC-Block") |
| 1839 |
moduleWg.addComment0("init: add vgpr [%u...%u) to pool" % \ |
| 1840 |
(self.states.c.startVgprValu, self.states.c.startVgprValu+self.states.c.numVgprValu)) |
| 1841 |
|
| 1842 |
numAccvgprs = self.states.totalAgprs |
| 1843 |
self.agprPool.add(0, numAccvgprs, "ValuC-Block") |
| 1844 |
moduleWg.addComment0("init: add agpr [%u...%u) to pool" % \ |
| 1845 |
(0, numAccvgprs)) |
| 1846 |
|
| 1847 |
if kernel["StreamK"] == 0: |
| 1848 |
moduleWg.add(self.localReadAddresses(kernel, tPA, tPB, tPM)) |
| 1849 |
moduleWg.add(self.localWriteAddresses(kernel, tPA, tPB, tPM)) |
| 1850 |
|
| 1851 |
def waitForArgsToLoad(): |
| 1852 |
if kernel["ProblemType"]["SupportUserArgs"]: |
| 1853 |
moduleWg.add(SWaitCnt(kmcnt=0, comment="wait for %u/%u bytes of kern args" % \ |
| 1854 |
(self.argLoader.getOffset() - (self.states.numSgprPreload*4), self.externalArgLoader.getOffset()))) |
| 1855 |
else: |
| 1856 |
moduleWg.add(SWaitCnt(kmcnt=0, comment="wait for %u bytes of kern args" % \ |
| 1857 |
(self.argLoader.getOffset() - (self.states.numSgprPreload*4)))) |
| 1858 |
moduleWg.addModuleAsFlatItems(moduleScaleAB) |
| 1859 |
|
| 1860 |
def calculateWG(): |
| 1861 |
|
| 1862 |
qReg = self.vgprPool.checkOut(4) |
| 1863 |
dReg = qReg + 1 |
| 1864 |
divReg = qReg + 2 |
| 1865 |
rReg = qReg + 3 |
| 1866 |
moduleWg.add(VMovB32(dst=vgpr(divReg), src="MT0", comment="set MT0 into sgpr")) |
| 1867 |
moduleWg.add(VMovB32(dst=vgpr(dReg), src=sgpr("SizesFree+0"), comment="set Free0 size")) |
| 1868 |
moduleWg.add(vectorUInt32CeilDivideAndRemainder(qReg=qReg, dReg=dReg, divReg=divReg, rReg=rReg, doRemainder=False)) |
| 1869 |
moduleWg.add(VMovB32(dst=vgpr(divReg), src="MT1", comment="set MT1 into sgpr")) |
| 1870 |
moduleWg.add(VMovB32(dst=vgpr(dReg), src=sgpr("SizesFree+1"), comment="set Free1 size")) |
| 1871 |
moduleWg.add(VReadfirstlaneB32(dst=sgpr("NumWorkGroups0"), src=vgpr(qReg), comment="set back to numWorkGroup0")) |
| 1872 |
moduleWg.add(vectorUInt32CeilDivideAndRemainder(qReg=qReg, dReg=dReg, divReg=divReg, rReg=rReg, doRemainder=False)) |
| 1873 |
if self.states.archCaps["TransOpWait"]: |
| 1874 |
moduleWg.add(SNop(waitState=0, comment="1 wait states")) |
| 1875 |
moduleWg.add(VReadfirstlaneB32(dst=sgpr("NumWorkGroups1"), src=vgpr(qReg), comment="set back to numWorkGroup1")) |
| 1876 |
self.vgprPool.checkIn(qReg) |
| 1877 |
|
| 1878 |
if self.states.numSgprPreload > 0: |
| 1879 |
calculateWG() |
| 1880 |
waitForArgsToLoad() |
| 1881 |
else: |
| 1882 |
waitForArgsToLoad() |
| 1883 |
calculateWG() |
| 1884 |
|
| 1885 |
if not kernel["ProblemType"]["StridedBatched"]: |
| 1886 |
with self.allocTmpSgpr(self.states.laneSGPRCount) as tmpSgpr: |
| 1887 |
moduleWg.add(self.loadBatchedAddress(kernel, "WorkGroup2", tmpSgpr)) |
| 1888 |
moduleWg.add(SWaitCnt(kmcnt=0, comment="wait global buffer address ready")) |
| 1889 |
elif waitForScaleAB: |
| 1890 |
moduleWg.add(SWaitCnt(kmcnt=0, comment="wait for scaleA/B to load")) |
| 1891 |
|
| 1892 |
labelMultiGemm = Label(label="MultiGemm", comment="") |
| 1893 |
labelMultiGemmEnd = Label(label="MultiGemmEnd", comment="") |
| 1894 |
module.add(moduleArgs) |
| 1895 |
module.add(moduleRegInit) |
| 1896 |
module.add(self.wgmXCC(kernel, tmpSgprNumWorkGroups)) |
| 1897 |
self.sgprPool.checkIn(tmpSgprNumWorkGroups) |
| 1898 |
tmpSgprNumWorkGroups = None |
| 1899 |
module.add(SCmpEQU32(src0=sgpr(sgprArgType), src1=0)) |
| 1900 |
self.sgprPool.checkIn(sgprArgType) |
| 1901 |
sgprArgType = None |
| 1902 |
module.add(SCBranchSCC0(labelName=labelMultiGemm.getLabelName())) |
| 1903 |
module.add(deepcopy(moduleWg)) |
| 1904 |
if kernel["StreamK"] == 0: |
| 1905 |
module.add(self.remapWgSerial(kernel, earlyStop=False)) |
| 1906 |
module.add(SBranch(labelName=labelMultiGemmEnd.getLabelName())) |
| 1907 |
module.add(labelMultiGemm) |
| 1908 |
|
| 1909 |
|
| 1910 |
for i in self.states.preloadGuard: |
| 1911 |
self.sgprPool.checkIn(i) |
| 1912 |
self.states.preloadGuard = [] |
| 1913 |
|
| 1914 |
numStoreSgprToLoad = self.states.numStoreSgprToLoad |
| 1915 |
if kernel["ProblemType"]["UseScaleAB"] == "Scalar": |
| 1916 |
if self.states.preloadScaleA: |
| 1917 |
numStoreSgprToLoad += 2 |
| 1918 |
if self.states.preloadScaleB: |
| 1919 |
numStoreSgprToLoad += 2 |
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
with self.allocTmpSgpr(8, 2) as tmpSgpr: |
| 1925 |
tmpSgprM = self.sgprs["SizesFree"] |
| 1926 |
tmpSgprN = tmpSgprM+1 |
| 1927 |
tmpSgprB = tmpSgprN+1 |
| 1928 |
tmpSgprArgAddress0 = tmpSgpr.idx |
| 1929 |
tmpSgpr0 = tmpSgpr.idx + 2 |
| 1930 |
tmpSgprNumWG0 = tmpSgpr.idx + 4 |
| 1931 |
tmpSgprNumWG1 = tmpSgpr.idx + 5 |
| 1932 |
tmpSgprAddrM = tmpSgpr.idx + 6 |
| 1933 |
tmpSgprAccumTiles = tmpSgpr.idx + 7 |
| 1934 |
tmpSgprLoopCounter = self.sgprs["NumWorkGroups0"] |
| 1935 |
tmpSgprArgOffsett = self.sgprs["NumWorkGroups1"] |
| 1936 |
|
| 1937 |
|
| 1938 |
extValidLabel = Label(label="IsExternalValid", comment="") |
| 1939 |
extValidLabelEnd = Label(label="IsExternalValidEnd", comment="") |
| 1940 |
if kernel["ProblemType"]["SupportUserArgs"]: |
| 1941 |
module.addComment1("Check if custom structure pointer is null") |
| 1942 |
module.add(SCmpEQU32(src0=sgpr("ArgType"), src1=2, comment="ArgType == 2 ?")) |
| 1943 |
module.add(SCBranchSCC1(labelName=extValidLabel.getLabelName(), comment="branch if ArgType == 2")) |
| 1944 |
if ((kernel["GlobalSplitUAlgorithm"] == 'MultipleBufferSingleKernel')): |
| 1945 |
module.add(SMovB32(dst=sgpr(tmpSgprArgOffsett), src=(self.argLoader.getOffset() + (numStoreSgprToLoad * 4) + (self.states.numSgprAddressGSUSync)*4))) |
| 1946 |
else: |
| 1947 |
module.add(SMovB32(dst=sgpr(tmpSgprArgOffsett), src=(self.argLoader.getOffset() + (numStoreSgprToLoad * 4)))) |
| 1948 |
module.add(SMulI32(dst=sgpr(tmpSgprAddrM), src0=sgpr(sgprNumsOfGemm), src1=4)) |
| 1949 |
module.add(SMovB64(dst=sgpr(tmpSgprArgAddress0,2), src=sgpr("KernArgAddress",2))) |
| 1950 |
module.add(SBranch(extValidLabelEnd.getLabelName())) |
| 1951 |
module.add(extValidLabel) |
| 1952 |
module.add(SMovB32(dst=sgpr(tmpSgprArgOffsett), src=self.states.userArgsInfo.totalSize)) |
| 1953 |
module.add(SMovB32(dst=sgpr(tmpSgprAddrM), src=0)) |
| 1954 |
module.add(SMovB64(dst=sgpr(tmpSgprArgAddress0,2), src=sgpr("KernArgAddress",2))) |
| 1955 |
module.add(extValidLabelEnd) |
| 1956 |
else: |
| 1957 |
module.add(SMovB32(dst=sgpr(tmpSgprArgOffsett), src=(self.argLoader.getOffset() + (numStoreSgprToLoad * 4)))) |
| 1958 |
module.add(SMulI32(dst=sgpr(tmpSgprAddrM), src0=sgpr(sgprNumsOfGemm), src1=4)) |
| 1959 |
module.add(SMovB64(dst=sgpr(tmpSgprArgAddress0,2), src=sgpr("KernArgAddress",2))) |
| 1960 |
|
| 1961 |
|
| 1962 |
module.addComment1("Grouped Gemm:: prefetch 1 arg load") |
| 1963 |
module.add(SMovB32(dst=sgpr(tmpSgprLoopCounter), src=1)) |
| 1964 |
module.add(SMovB32(dst=sgpr(tmpSgprAccumTiles), src=0)) |
| 1965 |
module.add(self.argLoader.loadKernArg(tmpSgprM, tmpSgprArgAddress0, sgpr(tmpSgprAddrM), dword=4)) |
| 1966 |
|
| 1967 |
module.add(self.getSCMPKInstruction("EQU32", sgprNumsOfGemm, 1, comment="if gemm_count is 1?")) |
| 1968 |
label_noLoadLoop = Label("wgTable_noLoadLoop", "") |
| 1969 |
module.add(SCBranchSCC1(labelName=label_noLoadLoop.getLabelName())) |
| 1970 |
|
| 1971 |
|
| 1972 |
module.addComment1("Grouped Gemm:: accumulate numTiles for each gemm") |
| 1973 |
module.addComment0("Grouped Gemm:: loop start") |
| 1974 |
label_Loop_gemm_count = Label("Loop_GemmCount", "") |
| 1975 |
module.add(label_Loop_gemm_count) |
| 1976 |
module.add(SWaitCnt(kmcnt=0)) |
| 1977 |
|
| 1978 |
regStateRes = ContinuousRegister(idx=tmpSgpr0, size=2) |
| 1979 |
module.add(scalarStaticCeilDivide(qReg=sgpr(tmpSgprNumWG0), dReg=sgpr(tmpSgprM), divisor=kernel["MacroTile0"], tmpSgprRes=regStateRes)) |
| 1980 |
module.add(scalarStaticCeilDivide(qReg=sgpr(tmpSgprNumWG1), dReg=sgpr(tmpSgprN), divisor=kernel["MacroTile1"], tmpSgprRes=regStateRes)) |
| 1981 |
|
| 1982 |
module.add(SMulI32(dst=sgpr(tmpSgprNumWG0), src0=sgpr(tmpSgprNumWG0), src1=sgpr(tmpSgprNumWG1))) |
| 1983 |
module.add(SMulI32(dst=sgpr(tmpSgprNumWG0), src0=sgpr(tmpSgprNumWG0), src1=sgpr(tmpSgprB))) |
| 1984 |
if kernel["GlobalSplitU"] != 0: |
| 1985 |
module.add(SAndB32(dst=sgpr(tmpSgprNumWG1), src0=sgpr("GSU"), src1=hex(0x3FFF), comment="Restore GSU")) |
| 1986 |
module.add(SMulI32(dst=sgpr(tmpSgprNumWG0), src0=sgpr(tmpSgprNumWG0), src1=sgpr(tmpSgprNumWG1))) |
| 1987 |
module.add(SAddU32(dst=sgpr(tmpSgprAccumTiles), src0=sgpr(tmpSgprAccumTiles), src1=sgpr(tmpSgprNumWG0))) |
| 1988 |
|
| 1989 |
module.add(SCmpLtU32(src0=sgpr("WorkGroup0"), src1=sgpr(tmpSgprAccumTiles))) |
| 1990 |
label_FOUND = Label("FOUND", "") |
| 1991 |
module.add(SCBranchSCC1(labelName=label_FOUND.getLabelName())) |
| 1992 |
|
| 1993 |
gsuComponent = Component.GSU.find(self) |
| 1994 |
module.add(gsuComponent.defineAndResources(self, kernel, tmpSgpr0, tmpSgprM, tmpSgprN, tmpSgprNumWG0, tmpSgprAccumTiles)) |
| 1995 |
|
| 1996 |
module.add(SAddU32(dst=sgpr(tmpSgprAddrM), src0=sgpr(tmpSgprAddrM), src1=sgpr(tmpSgprArgOffsett))) |
| 1997 |
module.add(self.argLoader.loadKernArg(tmpSgprM, tmpSgprArgAddress0, sgpr(tmpSgprAddrM), dword=4)) |
| 1998 |
module.add(SAddU32(dst=sgpr(tmpSgprLoopCounter), src0=sgpr(tmpSgprLoopCounter), src1=1)) |
| 1999 |
|
| 2000 |
module.add(SCmpLtU32(src0=sgpr(tmpSgprLoopCounter), src1=sgpr(sgprNumsOfGemm))) |
| 2001 |
module.add(SCBranchSCC1(labelName=label_Loop_gemm_count.getLabelName())) |
| 2002 |
|
| 2003 |
|
| 2004 |
module.addComment1("Grouped Gemm:: noLoadLoop") |
| 2005 |
module.add(label_noLoadLoop) |
| 2006 |
module.add(SWaitCnt(kmcnt=0)) |
| 2007 |
|
| 2008 |
regStateRes = ContinuousRegister(idx=tmpSgpr0, size=2) |
| 2009 |
module.add(scalarStaticCeilDivide(qReg=sgpr(tmpSgprNumWG0), dReg=sgpr(tmpSgprM), divisor=kernel["MacroTile0"], tmpSgprRes=regStateRes)) |
| 2010 |
module.add(scalarStaticCeilDivide(qReg=sgpr(tmpSgprNumWG1), dReg=sgpr(tmpSgprN), divisor=kernel["MacroTile1"], tmpSgprRes=regStateRes)) |
| 2011 |
|
| 2012 |
tmpSgprGSU = tmpSgpr.idx |
| 2013 |
module.add(SMulI32(dst=sgpr(tmpSgprNumWG0), src0=sgpr(tmpSgprNumWG0), src1=sgpr(tmpSgprNumWG1))) |
| 2014 |
module.add(SMulI32(dst=sgpr(tmpSgprNumWG0), src0=sgpr(tmpSgprNumWG0), src1=sgpr(tmpSgprB))) |
| 2015 |
if kernel["GlobalSplitU"] != 0: |
| 2016 |
module.add(SAndB32(dst=sgpr(tmpSgprGSU), src0=sgpr("GSU"), src1=hex(0x3FFF), comment="Restore GSU")) |
| 2017 |
module.add(SMulI32(dst=sgpr(tmpSgprNumWG0), src0=sgpr(tmpSgprNumWG0), src1=sgpr(tmpSgprGSU))) |
| 2018 |
module.add(SAddU32(dst=sgpr(tmpSgprAccumTiles), src0=sgpr(tmpSgprAccumTiles), src1=sgpr(tmpSgprNumWG0))) |
| 2019 |
|
| 2020 |
|
| 2021 |
tmpSgprWgLeft = tmpSgpr.idx |
| 2022 |
tmpSgprGemmIdxLeft = tmpSgprWgLeft + 1 |
| 2023 |
module.addComment1("Grouped Gemm:: gemmIndex found") |
| 2024 |
module.add(label_FOUND) |
| 2025 |
module.add(SSubU32(dst=sgpr(tmpSgprGemmIdxLeft), src0=sgpr(tmpSgprLoopCounter), src1=1)) |
| 2026 |
module.add(SSubU32(dst=sgpr(tmpSgprWgLeft), src0=sgpr(tmpSgprAccumTiles), src1=sgpr(tmpSgprNumWG0))) |
| 2027 |
|
| 2028 |
|
| 2029 |
|
| 2030 |
|
| 2031 |
module.add(SSubU32(dst=sgpr("WorkGroup0"), src0=sgpr("WorkGroup0"), src1=sgpr(tmpSgprWgLeft))) |
| 2032 |
extLabel = Label(label="LoadExternalStruct", comment="") |
| 2033 |
extLabelEnd = Label(label="LoadExternalStructEnd", comment="") |
| 2034 |
if kernel["ProblemType"]["SupportUserArgs"]: |
| 2035 |
module.addComment0("Check if custom structure pointer is null") |
| 2036 |
module.add(SCmpEQU32(src0=sgpr("ArgType"), src1=2, comment="ArgType == 2 ?")) |
| 2037 |
module.add(SCBranchSCC1(labelName=extLabel.getLabelName(), comment="branch if ArgType == 2")) |
| 2038 |
module.addComment1("Grouped Gemm: offset argument address to gemm") |
| 2039 |
module.addComment0("Grouped Gemm: offset address from wg_table_start to args_start") |
| 2040 |
module.add(SLShiftLeft2AddU32(dst=sgpr("KernArgAddress"), src0=sgpr(sgprNumsOfGemm), src1=sgpr("KernArgAddress"))) |
| 2041 |
module.add(SAddCU32(dst=sgpr("KernArgAddress+1"), src0=sgpr("KernArgAddress+1"), src1=0)) |
| 2042 |
module.addComment0("Grouped Gemm: offset address from args_start to gemm_start") |
| 2043 |
if ((kernel["GlobalSplitUAlgorithm"] == 'MultipleBufferSingleKernel')): |
| 2044 |
module.add(SMulI32(dst=sgpr(tmpSgprGemmIdxLeft), src0=sgpr(tmpSgprGemmIdxLeft),\ |
| 2045 |
src1=(self.argLoader.getOffset() + (numStoreSgprToLoad * 4) + (self.states.numSgprAddressGSUSync)*4))) |
| 2046 |
else: |
| 2047 |
module.add(SMulI32(dst=sgpr(tmpSgprGemmIdxLeft), src0=sgpr(tmpSgprGemmIdxLeft),\ |
| 2048 |
src1=(self.argLoader.getOffset() + (numStoreSgprToLoad * 4)))) |
| 2049 |
module.add(SAddU32(dst=sgpr("KernArgAddress"), src0=sgpr("KernArgAddress"), src1=sgpr(tmpSgprGemmIdxLeft))) |
| 2050 |
module.add(SAddCU32(dst=sgpr("KernArgAddress+1"), src0=sgpr("KernArgAddress+1"), src1=0)) |
| 2051 |
module.add(self.getKernelArgLoadModule(kernel, sgprStart, load, 4)) |
| 2052 |
if kernel["ProblemType"]["SupportUserArgs"]: |
| 2053 |
module.add(SBranch(extLabelEnd.getLabelName())) |
| 2054 |
module.add(extLabel) |
| 2055 |
module.addComment0("Grouped Gemm: offset address from args_start to gemm_start") |
| 2056 |
|
| 2057 |
module.add(SMulI32(dst=sgpr(tmpSgprGemmIdxLeft), src0=sgpr(tmpSgprGemmIdxLeft),src1=self.states.userArgsInfo.totalSize)) |
| 2058 |
module.add(SAddU32(dst=sgpr("KernArgAddress"), src0=sgpr("KernArgAddress"), src1=sgpr(tmpSgprGemmIdxLeft))) |
| 2059 |
module.add(SAddCU32(dst=sgpr("KernArgAddress+1"), src0=sgpr("KernArgAddress+1"), src1=0)) |
| 2060 |
moduleExternalArgs = Module("Load external Arguments") |
| 2061 |
|
| 2062 |
load = load - self.states.numSgprBeta |
| 2063 |
moduleExternalArgs.addModuleAsFlatItems(self.externalArgLoader.loadAllKernArg(sgprStart, "KernArgAddress", load, 4)) |
| 2064 |
offset = self.externalArgLoader.getOffset() + self.states.bpr * (self.states.userArgsInfo.alphaMaxRegisterSize - self.states.numSgprAlpha) |
| 2065 |
self.externalArgLoader.setOffset(offset) |
| 2066 |
moduleExternalArgs.addComment("Read Beta") |
| 2067 |
moduleExternalArgs.addModuleAsFlatItems(self.externalArgLoader.loadAllKernArg(self.sgprs["Beta"], "KernArgAddress", self.states.numSgprBeta)) |
| 2068 |
offset = self.externalArgLoader.getOffset() + self.states.bpr * (self.states.userArgsInfo.betaMaxRegisterSize - self.states.numSgprBeta) |
| 2069 |
if kernel["ProblemType"]["UseScaleAB"] == "Scalar": |
| 2070 |
sgprOffset = self.externalArgLoader.getOffset() |
| 2071 |
for preloadScale, name in zip([self.states.preloadScaleA, self.states.preloadScaleB], ['A','B']): |
| 2072 |
if preloadScale: |
| 2073 |
moduleExternalArgs.add(self.externalArgLoader.loadKernArg("AddressScale%s"%name, "KernArgAddress", sgprOffset=hex(sgprOffset), dword=2)) |
| 2074 |
sgprOffset += self.states.userArgsInfo.scaleASize |
| 2075 |
self.externalArgLoader.setOffset(offset) |
| 2076 |
module.add(moduleExternalArgs) |
| 2077 |
module.add(extLabelEnd) |
| 2078 |
|
| 2079 |
|
| 2080 |
labels = [] |
| 2081 |
for inst in moduleWg.items(): |
| 2082 |
if isinstance(inst, Label): |
| 2083 |
self.labels.getNameInc(inst.label) |
| 2084 |
labels.append([inst.label, self.labels.getNameInc(inst.label)]) |
| 2085 |
inst.label = labels[-1][1] |
| 2086 |
for inst in moduleWg.items(): |
| 2087 |
if isinstance(inst, BranchInstruction): |
| 2088 |
removeIdx = -1 |
| 2089 |
for idx, label in enumerate(labels): |
| 2090 |
if inst.labelName == Label(label=label[0],comment="").getLabelName(): |
| 2091 |
inst.labelName = Label(label=label[1],comment="").getLabelName() |
| 2092 |
removeIdx = idx |
| 2093 |
if removeIdx != -1: |
| 2094 |
del labels[removeIdx] |
| 2095 |
module.add(moduleWg) |
| 2096 |
|
| 2097 |
earlyReturnModule = Module("Early stop if N(SizeFreeJ) == 0") |
| 2098 |
earlyReturnModule.addComment1("Early stop if N(SizeFreeJ) == 0") |
| 2099 |
earlyReturnModule.add(SCmpEQU32(sgpr("SizeJ"), 0)) |
| 2100 |
earlyReturnLabel = Label("EarlyStop_if_N_is_0", "") |
| 2101 |
noEarlyReturnLabel = Label("NoEarlyStop_N0", "") |
| 2102 |
earlyReturnModule.add(SCBranchSCC0(noEarlyReturnLabel.getLabelName())) |
| 2103 |
earlyReturnModule.add(earlyReturnLabel) |
| 2104 |
earlyReturnModule.add(SEndpgm()) |
| 2105 |
earlyReturnModule.add(noEarlyReturnLabel) |
| 2106 |
module.add(earlyReturnModule) |
| 2107 |
if kernel["StreamK"] == 0: |
| 2108 |
module.add(self.remapWgSerial(kernel)) |
| 2109 |
module.addSpaceLine() |
| 2110 |
module.add(labelMultiGemmEnd) |
| 2111 |
|
| 2112 |
|
| 2113 |
if sgprNumsOfGemm: |
| 2114 |
self.sgprPool.checkIn(sgprNumsOfGemm) |
| 2115 |
sgprNumsOfGemm = None |
| 2116 |
|
| 2117 |
|
| 2118 |
module.addModuleAsFlatItems(self.defineVariableSgprs(kernel)) |
| 2119 |
|
| 2120 |
needPackK16 = False |
| 2121 |
needPackK8Lw = False |
| 2122 |
needPackK8Hi = False |
| 2123 |
|
| 2124 |
if kernel["ProblemType"]["DataType"].isHalf() or kernel["ProblemType"]["DataType"].isBFloat16(): |
| 2125 |
if self.states.lrvwTileA > 1 or self.states.lrvwTileB > 1: |
| 2126 |
needPackK16 = True |
| 2127 |
if self.states.lrvwTileMetadata > 1: |
| 2128 |
needPackK8Lw = True |
| 2129 |
if self.states.lrvwTileMetadata > 2: |
| 2130 |
needPackK8Hi = True |
| 2131 |
elif kernel["ProblemType"]["DataType"].isInt8() or kernel["ProblemType"]["DataType"].is8bitFloat(): |
| 2132 |
if self.states.lrvwTileA > 1 or self.states.lrvwTileB > 1 or self.states.lrvwTileMetadata > 1: |
| 2133 |
needPackK8Lw = True |
| 2134 |
if self.states.lrvwTileA > 2 or self.states.lrvwTileB > 2 or self.states.lrvwTileMetadata > 2: |
| 2135 |
needPackK8Hi = True |
| 2136 |
|
| 2137 |
|
| 2138 |
tPackM = "M" if needPackK16 and needPackK8Lw else "" |
| 2139 |
if needPackK16: |
| 2140 |
module.add(SMovB32(dst=sgpr("PackKForV0"), src="0x05040100", comment="")) |
| 2141 |
module.add(SMovB32(dst=sgpr("PackKForV1"), src="0x07060302", comment="")) |
| 2142 |
if needPackK8Lw: |
| 2143 |
module.add(SMovB32(dst=sgpr("PackKFor%sV0"%tPackM), src="0x0c0c0400", comment="")) |
| 2144 |
module.add(SMovB32(dst=sgpr("PackKFor%sV1"%tPackM), src="0x0c0c0501", comment="")) |
| 2145 |
if needPackK8Hi: |
| 2146 |
module.add(SMovB32(dst=sgpr("PackKFor%sV2"%tPackM), src="0x0c0c0602", comment="")) |
| 2147 |
module.add(SMovB32(dst=sgpr("PackKFor%sV3"%tPackM), src="0x0c0c0703", comment="")) |
| 2148 |
|
| 2149 |
|
| 2150 |
if self.states.groOffsetInMacroTile: |
| 2151 |
prePad = self.states.srdShiftLeft["A"] * tPA["bpeGR"] |
| 2152 |
module.add(SSubU32(dst=sgpr("AddressA+0"), src0=sgpr("AddressA+0"), src1=prePad, comment="pre-pad to make room for possible pointer shift")) |
| 2153 |
module.add(SSubBU32(dst=sgpr("AddressA+1"), src0=sgpr("AddressA+1"), src1=0, comment="pre-pad to make room for possible pointer shift")) |
| 2154 |
prePad = self.states.srdShiftLeft["B"] * tPB["bpeGR"] |
| 2155 |
module.add(SSubU32(dst=sgpr("AddressB+0"), src0=sgpr("AddressB+0"), src1=prePad, comment="pre-pad to make room for possible pointer shift")) |
| 2156 |
module.add(SSubBU32(dst=sgpr("AddressB+1"), src0=sgpr("AddressB+1"), src1=0, comment="pre-pad to make room for possible pointer shift")) |
| 2157 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 2158 |
prePad = self.states.srdShiftLeft["Metadata"] * tPM["bpe"] |
| 2159 |
module.add(SSubU32(dst=sgpr("AddressMetadata+0"), src0=sgpr("AddressMetadata+0"), src1=prePad, comment="pre-pad to make room for possible pointer shift")) |
| 2160 |
module.add(SSubBU32(dst=sgpr("AddressMetadata+1"), src0=sgpr("AddressMetadata+1"), src1=0, comment="pre-pad to make room for possible pointer shift")) |
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
|
| 2165 |
if self.do["ApplyAlpha"]: |
| 2166 |
if self.states.useBias == DataDirection.WRITE and (kernel["ProblemType"]["BiasSrc"] == "A" or kernel["ProblemType"]["BiasSrc"] == "B"): |
| 2167 |
|
| 2168 |
pass |
| 2169 |
else: |
| 2170 |
module.addComment1("Short circuit condition if Alpha == 0, then sumDims=0") |
| 2171 |
endCheckLabel = Label("AlphaNonZero", "") |
| 2172 |
module.add(BranchIfNotZero("Alpha", kernel["ProblemType"]["ComputeDataType"].toEnum(), endCheckLabel)) |
| 2173 |
|
| 2174 |
|
| 2175 |
for i in range(0, self.states.numSgprSizesSum): |
| 2176 |
module.add(SMovB32(dst=sgpr("SizesSum+%u"%(i)), src=0, comment="Set summation dim=0 if Alpha == 0")) |
| 2177 |
|
| 2178 |
|
| 2179 |
|
| 2180 |
module.add(endCheckLabel) |
| 2181 |
|
| 2182 |
if kernel["MagicDivAlg"]==2: |
| 2183 |
for idxChar in sorted(set(kernel["PackedC0IdxChars"][:-1] + kernel["PackedC1IdxChars"][:-1])): |
| 2184 |
module.add(SLShiftRightB32(dst=sgpr("MagicAbitSize%s"%idxChar), src=sgpr("MagicShiftSize%s"%idxChar), shiftHex=31, comment="extract abit")) |
| 2185 |
module.add(SAndB32(dst=sgpr("MagicShiftSize%s"%idxChar), src0=sgpr("MagicShiftSize%s"%idxChar), src1=hex(0x7fffffff), comment="remove abit")) |
| 2186 |
|
| 2187 |
|
| 2188 |
|
| 2189 |
if self.debugConfig.debugKernel: |
| 2190 |
module.addComment1("Debug Buffer") |
| 2191 |
|
| 2192 |
|
| 2193 |
nwg0 = self.vgprPool.checkOut(1) |
| 2194 |
tmpVgpr = self.vgprPool.checkOutAligned(2, 2) |
| 2195 |
tmpVgprRes = ContinuousRegister(tmpVgpr, 2) |
| 2196 |
module.addComment("nwg0 = (size%s + MT%s - 1) / MT%s;" \ |
| 2197 |
% (self.states.tileChar0, self.states.tileChar0, self.states.tileChar0)) |
| 2198 |
module.add(VMovB32(dst=vgpr(tmpVgpr), src=hex(kernel["MacroTile0"]-1), comment="MT0-1")) |
| 2199 |
module.add(VAddCOU32(dst=vgpr(nwg0), dst1=VCC(), src0=sgpr("SizesFree+0"), \ |
| 2200 |
src1=vgpr(tmpVgpr), comment="%s = size0+MT0-1"%vgpr(nwg0))) |
| 2201 |
module.add(vectorStaticDivide(nwg0, nwg0, kernel["MacroTile0"], tmpVgprRes)) |
| 2202 |
self.vgprPool.checkIn(tmpVgpr) |
| 2203 |
self.nipt = 16 |
| 2204 |
v = self.vgprPool.checkOut(3) |
| 2205 |
module.add(VMovB32(dst=vgpr(v), src=sgpr("WorkGroup0"), comment="%s=wg0"%vgpr(v) )) |
| 2206 |
module.add(VMovB32(dst=vgpr(v+1), src=sgpr("WorkGroup1"), comment="%s=wg1"%vgpr(v+1) )) |
| 2207 |
module.add(VMulLOU32(dst=vgpr(v+1), src0=vgpr(v+1), src1=vgpr(nwg0), \ |
| 2208 |
comment="%s=wg1*nwg0"%vgpr(v+1) )) |
| 2209 |
module.add(VAddCOU32(dst=vgpr(v), dst1=VCC(), src0=vgpr(v), src1=vgpr(v+1), \ |
| 2210 |
comment="%s=wg1*nwg0+wg0"%vgpr(v) )) |
| 2211 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 2212 |
module.add(vectorStaticMultiply(vgpr(v), vgpr(v), kernel["NumThreads"], tmpSgprInfo)) |
| 2213 |
module.add(VAddCOU32(dst=vgpr(v), dst1=VCC(), src0=vgpr(v), src1=vgpr("Serial"), \ |
| 2214 |
comment="%s=tid+NT*(wg1*nwg0+wg0)=serial"%vgpr(v) )) |
| 2215 |
module.add(VMulLOU32(dst=vgpr(v), src0=hex(self.nipt*4), src1=vgpr(v), \ |
| 2216 |
comment="%s=serial*nipt*4"%vgpr(v) )) |
| 2217 |
module.add(VMovB32(dst=vgpr(v+1), src=0)) |
| 2218 |
module.add(VAddCOU32(dst=vgpr("AddressDbg"), dst1=VCC(), src0=sgpr("AddressDbg"), src1=vgpr(v), \ |
| 2219 |
comment="%s=AddrD* + serial*nipt*4"%vgpr("AddressDbg") )) |
| 2220 |
module.add(VMovB32(dst=vgpr(v+2), src=sgpr("AddressDbg+1"), comment="%s=AddressD1"%vgpr(v+2) )) |
| 2221 |
module.add(VAddCCOU32(dst=vgpr("AddressDbg+1"), dst1=VCC(), src0=vgpr(v+2), \ |
| 2222 |
src1=vgpr(v+1), src2=VCC(), comment="%s=AddrD* + serial*nipt*4"%vgpr("AddressDbg") )) |
| 2223 |
module.add(SMovB32(dst=sgpr("DebugKernelItems"), src=0)) |
| 2224 |
self.vgprPool.checkIn(v) |
| 2225 |
self.vgprPool.checkIn(nwg0) |
| 2226 |
|
| 2227 |
if self.db["InitLds"]: |
| 2228 |
tmp = ContinuousRegister(idx=self.vgprPool.checkOut(2), size=2) |
| 2229 |
module.add(DSInit(tmp, kernel["NumThreads"], kernel["LdsNumBytes"], self.consts.initLdsValue)) |
| 2230 |
self.vgprPool.checkIn(tmp.idx) |
| 2231 |
|
| 2232 |
return module |
| 2233 |
|
| 2234 |
def extractPackedCoord1ToRowStart(self, kernel, packedC1, packedCoordVgpr, storeChar): |
| 2235 |
if kernel["ProblemType"]["UseE"] and ((kernel["GlobalSplitU"] == 1 or kernel["GlobalSplitU"] == -1) or kernel["StreamK"] > 0): |
| 2236 |
printExit("extractPackedCoord1ToRowStart doe not support with output E.") |
| 2237 |
|
| 2238 |
|
| 2239 |
|
| 2240 |
|
| 2241 |
|
| 2242 |
|
| 2243 |
module = Module("extractPackedCoord1ToRowStart") |
| 2244 |
tmpV0 = self.vgprPool.checkOut(4) |
| 2245 |
tmpV1 = tmpV0 + 1 |
| 2246 |
tmpV2 = tmpV0 + 2 |
| 2247 |
tmpV3 = tmpV0 + 3 |
| 2248 |
|
| 2249 |
module.add(VMovB32(dst=vgpr(tmpV0), src=vgpr(packedCoordVgpr), comment="copy coord1 then unpack")) |
| 2250 |
for i,idx in enumerate(packedC1[:-1]): |
| 2251 |
idxChar= INDEX_CHARS[idx] |
| 2252 |
module.addComment0("extract %s"%self.sizeRef(idx)) |
| 2253 |
module.add(MacroInstruction(name="V_MAGIC_DIV", \ |
| 2254 |
args=[tmpV1, vgpr(tmpV0), sgpr("MagicNumberSize%s"%idxChar), \ |
| 2255 |
sgpr("MagicShiftSize%s"%idxChar), (sgpr("MagicAbitSize%s"%idxChar) if kernel["MagicDivAlg"]==2 else "0")])) |
| 2256 |
module.add(VMulLOU32(dst=vgpr(tmpV2), src0=vgpr(tmpV1), src1=self.sizeRef(idx), comment="remainder part 1")) |
| 2257 |
module.add(VSubU32(dst=vgpr(tmpV2), src0=vgpr(tmpV0), src1=vgpr(tmpV2), comment="remainder part 2")) |
| 2258 |
if i==0: |
| 2259 |
module.add(VMulLOU32(dst=vgpr(tmpV3), src0=vgpr(tmpV2), \ |
| 2260 |
src1=self.strideRef(storeChar, idx), comment="addrCalc <- scaled extracted dim")) |
| 2261 |
else: |
| 2262 |
module.add(VMulLOU32(dst=vgpr(tmpV2), src0=vgpr(tmpV2), \ |
| 2263 |
src1=self.strideRef(storeChar, idx), comment="scale extracted dim")) |
| 2264 |
module.add(VAddU32(dst=vgpr(tmpV3), src0=vgpr(tmpV3), \ |
| 2265 |
src1=vgpr(tmpV2), comment="addrCalc += scaled extracted dim ")) |
| 2266 |
|
| 2267 |
if i < len(packedC1)-2: |
| 2268 |
module.add(VMovB32(dst=vgpr(tmpV0), src=vgpr(tmpV1), \ |
| 2269 |
comment="Copy remaining bits for next divide")) |
| 2270 |
|
| 2271 |
module.addComment0("extract final %s"%self.sizeRef(packedC1[-1])) |
| 2272 |
module.add(VMulLOU32(dst=vgpr(tmpV2), src0=vgpr(tmpV1), \ |
| 2273 |
src1=self.strideRef(storeChar, packedC1[-1]), comment="scale final extracted dim")) |
| 2274 |
module.add(VAddU32(dst=vgpr(self.vgprs.coutRowPtrD), src0=vgpr(tmpV3), \ |
| 2275 |
src1=vgpr(tmpV2), comment="rowStart += scaled extracted dim ")) |
| 2276 |
|
| 2277 |
self.vgprPool.checkIn(tmpV0) |
| 2278 |
return module |
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
def graWorkGroup(self, kernel, tPA, tPB): |
| 2284 |
module = Module("graWorkGroup") |
| 2285 |
module.addComment0("graWorkGroup mapping") |
| 2286 |
|
| 2287 |
skComponent = Component.StreamK.find(self) |
| 2288 |
module.add(skComponent.graWorkGroup(self, kernel, tPA, tPB)) |
| 2289 |
|
| 2290 |
gsuComponent = Component.GSU.find(self) |
| 2291 |
module.add(gsuComponent.graWorkGroup(self, kernel)) |
| 2292 |
|
| 2293 |
|
| 2294 |
|
| 2295 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
module.add(SSExtI16toI32(dst=sgpr("WGM"), src=sgpr("WGM"), comment="Restore WGM")) |
| 2299 |
|
| 2300 |
wgmLabel = Label(label=self.labels.getNameInc("WGM"), comment="") |
| 2301 |
wgmLabelPositive = Label(label=self.labels.getNameInc("WGMPositive"), comment="") |
| 2302 |
module.add(SCmpGtI32(src0=sgpr("WGM"), src1=1, comment="WGM > 1 ?")) |
| 2303 |
module.add(SCBranchSCC1(labelName=wgmLabelPositive.getLabelName(), comment="branch if WGM > 1")) |
| 2304 |
with self.allocTmpSgprList(nums=[2,1,1]) as tmpSgprInfoList: |
| 2305 |
wgmDivisor = tmpSgprInfoList[0].idx |
| 2306 |
wgmDivisor2 = tmpSgprInfoList[0].idx + 1 |
| 2307 |
blockId2 = tmpSgprInfoList[1].idx |
| 2308 |
wgSerial2 = tmpSgprInfoList[2].idx |
| 2309 |
wgmDivisorMagicNumber = tmpSgprInfoList[0].idx + 1 |
| 2310 |
|
| 2311 |
|
| 2312 |
if kernel["StreamK"] > 0: |
| 2313 |
tmpSgpr = self.sgprPool.checkOut(1) |
| 2314 |
wgmAbs = tmpSgpr |
| 2315 |
else: |
| 2316 |
wgmAbs = "WGM" |
| 2317 |
|
| 2318 |
tmpVgpr = self.vgprPool.checkOut(2, "div") |
| 2319 |
tmpVgprRes = ContinuousRegister(idx=tmpVgpr, size=2) |
| 2320 |
|
| 2321 |
|
| 2322 |
for wgmType in [True, False]: |
| 2323 |
if wgmType: |
| 2324 |
workgroupFirst = "WorkGroup1" |
| 2325 |
workgroupSecond = "WorkGroup0" |
| 2326 |
numWorkgroupsFirst = "NumWorkGroups1" |
| 2327 |
numWorkgroupsSecond = "NumWorkGroups0" |
| 2328 |
else: |
| 2329 |
workgroupFirst = "WorkGroup0" |
| 2330 |
workgroupSecond = "WorkGroup1" |
| 2331 |
numWorkgroupsFirst = "NumWorkGroups0" |
| 2332 |
numWorkgroupsSecond = "NumWorkGroups1" |
| 2333 |
|
| 2334 |
if not wgmType: |
| 2335 |
module.add(wgmLabelPositive) |
| 2336 |
module.add(SMovB32(dst=sgpr(wgmAbs), src=sgpr("WGM"), comment="WGM")) |
| 2337 |
else: |
| 2338 |
module.add(SCmpGeI32(src0=sgpr("WGM"), src1=0, comment="WGM >= 0 ?")) |
| 2339 |
module.add(SCBranchSCC1(labelName=wgmLabel.getLabelName(), comment="branch if WGM >= 0")) |
| 2340 |
module.add(SAbsI32(dst=sgpr(wgmAbs), src=sgpr("WGM"), comment="abs(WGM)")) |
| 2341 |
|
| 2342 |
module.add(scalarUInt32DivideAndRemainder(qReg=blockId2, dReg=workgroupSecond, divReg=wgmAbs, rReg=wgSerial2, tmpVgprRes=tmpVgprRes, wavewidth=kernel["WavefrontSize"], doRemainder=False, comment="WGM")) |
| 2343 |
module.add(SMulI32(dst=sgpr(wgSerial2), src0=sgpr(blockId2), src1=sgpr(wgmAbs), comment="quotient * non-magic divisor")) |
| 2344 |
module.add(SSubU32(dst=sgpr(wgSerial2), src0=sgpr(workgroupSecond), src1=sgpr(wgSerial2), comment="%s=remainder"%workgroupSecond)) |
| 2345 |
module.add(SMulI32(dst=sgpr(wgSerial2), src0=sgpr(wgSerial2), src1=sgpr(numWorkgroupsFirst), comment="(wg1 %% WGM)*%s"%numWorkgroupsFirst)) |
| 2346 |
module.add(SAddU32(dst=sgpr(wgSerial2), src0=sgpr(wgSerial2), src1=sgpr(workgroupFirst), comment="wgSerial = wg0 + (wg1 %% WGM)*%s"%numWorkgroupsFirst)) |
| 2347 |
|
| 2348 |
module.add(scalarUInt32DivideAndRemainder(qReg=wgmDivisor, dReg=numWorkgroupsSecond, divReg=wgmAbs, rReg=wgSerial2, tmpVgprRes=tmpVgprRes, wavewidth=kernel["WavefrontSize"], doRemainder=False, comment="WGM")) |
| 2349 |
module.add(SMulI32(dst=sgpr(wgmDivisor2), src0=sgpr(wgmAbs), src1=sgpr(wgmDivisor), comment="quotient * non-magic divisor")) |
| 2350 |
module.add(SSubU32(dst=sgpr(wgmDivisorMagicNumber), src0=sgpr(numWorkgroupsSecond), src1=sgpr(wgmDivisor2), comment="%s=remainder"%numWorkgroupsSecond)) |
| 2351 |
module.add(SCmpEQU32(src0=sgpr(wgmDivisorMagicNumber), src1=0, comment="remainder == 0 ?")) |
| 2352 |
module.add(SCMovB32(dst=sgpr(wgmDivisorMagicNumber), src=sgpr(wgmAbs), comment="remainder = WGM if remainder == 0")) |
| 2353 |
|
| 2354 |
module.add(SCmpGeU32(src0=sgpr(blockId2), src1=sgpr(wgmDivisor), comment="blockId >= numFullBlocks ?")) |
| 2355 |
module.add(SCSelectB32(dst=sgpr(wgmDivisor), src0=sgpr(wgmDivisorMagicNumber), src1=sgpr(wgmAbs))) |
| 2356 |
|
| 2357 |
|
| 2358 |
|
| 2359 |
|
| 2360 |
module.add(scalarUInt32DivideAndRemainder(qReg=workgroupFirst, dReg=wgSerial2, divReg=wgmDivisor, rReg=workgroupSecond, tmpVgprRes=tmpVgprRes, wavewidth=kernel["WavefrontSize"])) |
| 2361 |
module.add(SMulI32(dst=sgpr(workgroupSecond), src0=sgpr(workgroupFirst), src1=sgpr(wgmDivisor), comment="quotient * non-magic divisor")) |
| 2362 |
module.add(SSubU32(dst=sgpr(workgroupSecond), src0=sgpr(wgSerial2), src1=sgpr(workgroupSecond), comment="%s=remainder"%workgroupSecond)) |
| 2363 |
module.add(SMulI32(dst=sgpr(blockId2), src0=sgpr(blockId2), src1=sgpr(wgmAbs), comment="blockId * WGM")) |
| 2364 |
module.add(SAddU32(dst=sgpr(workgroupSecond), src0=sgpr(workgroupSecond), src1=sgpr(blockId2), comment="wg1 += blockId * WGM")) |
| 2365 |
if wgmType: |
| 2366 |
module.add(SBranch(wgmLabel.getLabelName())) |
| 2367 |
module.add(wgmLabel) |
| 2368 |
|
| 2369 |
if kernel["StreamK"] > 0: |
| 2370 |
self.sgprPool.checkIn(tmpSgpr) |
| 2371 |
tmpVgprRes = None |
| 2372 |
self.vgprPool.checkIn(tmpVgpr) |
| 2373 |
return module |
| 2374 |
|
| 2375 |
def graMetadataTileAssignment(self, kernel, tP): |
| 2376 |
module = Module("graMetadataTileAssignment") |
| 2377 |
module.addComment0("calculate metadata gra tile assignment") |
| 2378 |
|
| 2379 |
if kernel["DirectToVgprSparseMetadata"]: |
| 2380 |
|
| 2381 |
|
| 2382 |
wReg = self.vgprPool.checkOut(1,"wReg") |
| 2383 |
tReg = self.vgprPool.checkOut(1,"tReg") |
| 2384 |
tmpVgpr = self.vgprPool.checkOutAligned(2,2,"tmpVgpr") |
| 2385 |
dummy = self.vgprPool.checkOut(1,"dummy") |
| 2386 |
|
| 2387 |
tmpVgprRes = ContinuousRegister(tmpVgpr, 2) |
| 2388 |
|
| 2389 |
|
| 2390 |
|
| 2391 |
waveWidth = kernel["WavefrontSize"] |
| 2392 |
|
| 2393 |
|
| 2394 |
num1DBlocks = kernel["MatrixInstBM"] |
| 2395 |
num1DWaves = kernel["MIWaveGroup"][0] |
| 2396 |
dividedForWaveId = waveWidth |
| 2397 |
|
| 2398 |
|
| 2399 |
offsetWave = kernel["MatrixInstM"] * num1DBlocks |
| 2400 |
|
| 2401 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 2402 |
|
| 2403 |
module.add(vectorStaticRemainder(dummy, tReg, "Serial", waveWidth, tmpVgprRes, tmpSgprInfo, \ |
| 2404 |
comment="0. thread id in wave: wtid = tid %% wavelength(%u)" % waveWidth)) |
| 2405 |
module.add(vectorStaticRemainder(dummy, tReg, tReg, kernel["MatrixInstM"], tmpVgprRes, tmpSgprInfo, \ |
| 2406 |
comment="1. tile offset: nIdx = wtid %% MI_N(%u)" % kernel["MatrixInstM"])) |
| 2407 |
|
| 2408 |
|
| 2409 |
if num1DWaves > 1: |
| 2410 |
module.add(vectorStaticDivide(wReg, "Serial", dividedForWaveId, tmpVgprRes, \ |
| 2411 |
"2. wave offset in N dimen: wtid = tid / dividedForWaveId(%u)" % dividedForWaveId)) |
| 2412 |
module.add(vectorStaticRemainder(dummy, wReg, wReg, num1DWaves, tmpVgprRes, tmpSgprInfo, \ |
| 2413 |
"2. wave offset in M dimen: wtid0 = wtid / num1DWaves(%u)" % num1DWaves)) |
| 2414 |
module.add(vectorStaticMultiplyAdd(vgpr("GlobalReadOffsetMetadata"), vgpr(wReg), offsetWave, vgpr(tReg), tmpSgprInfo, \ |
| 2415 |
"2. wave offset in M dimen: wOffset = wtid0 * W0 offset(%u); 2. tile coord = tileOffset + wOffset" % offsetWave)) |
| 2416 |
else: |
| 2417 |
module.add(VMovB32(vgpr("GlobalReadOffsetMetadata"), vgpr(tReg), \ |
| 2418 |
comment="2. tile coord = tileOffset")) |
| 2419 |
|
| 2420 |
for graIdx in range (1, kernel["MIWaveTile"][0]): |
| 2421 |
MIWaveGroup0ShapeSize = kernel["MatrixInstM"] * kernel["MatrixInstBM"] * kernel["MIWaveGroup"][0] |
| 2422 |
vgprGro = "GlobalReadOffsetMetadata+%u"%(graIdx) |
| 2423 |
module.add(VMovB32(vgpr(vgprGro), hex(MIWaveGroup0ShapeSize*graIdx), comment="7. coord offset of WaveTile %u"%graIdx)) |
| 2424 |
module.add(VAddU32(vgpr(vgprGro), vgpr(vgprGro), vgpr("GlobalReadOffsetMetadata"), \ |
| 2425 |
"3. final coord0: tile coord + waveTile coord")) |
| 2426 |
|
| 2427 |
|
| 2428 |
self.vgprPool.checkIn(tReg) |
| 2429 |
self.vgprPool.checkIn(wReg) |
| 2430 |
self.vgprPool.checkIn(tmpVgpr) |
| 2431 |
self.vgprPool.checkIn(dummy) |
| 2432 |
|
| 2433 |
return module |
| 2434 |
|
| 2435 |
|
| 2436 |
|
| 2437 |
|
| 2438 |
|
| 2439 |
def graTileAssignment(self, kernel, tP): |
| 2440 |
module = Module("graTileAssignment") |
| 2441 |
tc = tP["tensorChar"] |
| 2442 |
tReg = tP["gpr"]["lwoT"] |
| 2443 |
|
| 2444 |
module.addComment0("graTileAssignment%s = %s" % (tc, vgpr(tReg))) |
| 2445 |
|
| 2446 |
if self.states.groOffsetInMacroTile: |
| 2447 |
tReg2 = tReg |
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
else: |
| 2452 |
tReg2 = self.vgprPool.checkOut(1, 'treg2', self.states.preventVgprOverflowDuringNewTile) |
| 2453 |
|
| 2454 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 2455 |
if not self.states.groOffsetInMacroTile: |
| 2456 |
tmpVgpr = self.vgprPool.checkOut(1, 'graTA vgpr', self.states.preventVgprOverflowDuringNewTile) |
| 2457 |
|
| 2458 |
|
| 2459 |
module.add(vectorStaticMultiply(vgpr(tmpVgpr), sgpr(tP["wg"]), kernel[tP["mt"]], tmpSgprInfo)) |
| 2460 |
module.add(VAddCOU32(dst=vgpr(tReg2), dst1=VCC(), src0=vgpr(tmpVgpr), \ |
| 2461 |
src1=vgpr(tReg), comment="gro%s-tile = serial%s%s*VW + (wg%s*MT%s)" \ |
| 2462 |
% (tc, tOpStr, divisorName, tc, tc) )) |
| 2463 |
self.vgprPool.checkIn(tmpVgpr) |
| 2464 |
|
| 2465 |
tP["gpr"]["tReg"] = tReg2 |
| 2466 |
|
| 2467 |
return Module("graTileAssignment (Empty)") if self.dontAppendCode else module |
| 2468 |
|
| 2469 |
|
| 2470 |
|
| 2471 |
|
| 2472 |
def graUnrollAssignment(self, kernel, tP): |
| 2473 |
module = Module("graUnrollAssignment") |
| 2474 |
|
| 2475 |
if not self.states.groOffsetInMacroTile: |
| 2476 |
gsuOffset = self.vgprPool.checkOut(1, "gsuOffset", self.states.preventVgprOverflowDuringNewTile) |
| 2477 |
module.add(VMovB32(dst=vgpr(gsuOffset), src=sgpr("GSUSumIdx"), comment="=gsuSumIdx")) |
| 2478 |
|
| 2479 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 2480 |
|
| 2481 |
module.add(vectorStaticMultiply(vgpr(gsuOffset), vgpr(gsuOffset), kernel["DepthU"], tmpSgprInfo)) |
| 2482 |
|
| 2483 |
module.add(VAddCOU32(dst=vgpr(tP["gpr"]["uReg"]), dst1=VCC(), \ |
| 2484 |
src0=vgpr(gsuOffset), src1=vgpr(tP["gpr"]["uReg"]), \ |
| 2485 |
comment="graUnrollAssignment += gsuOffset")) |
| 2486 |
self.vgprPool.checkIn(gsuOffset) |
| 2487 |
else: |
| 2488 |
module.addComment0(str(vgpr(tP["gpr"]["uReg"]))) |
| 2489 |
|
| 2490 |
return Module("graUnrollAssignment (Empty)") if self.dontAppendCode else module |
| 2491 |
|
| 2492 |
|
| 2493 |
|
| 2494 |
|
| 2495 |
def graOtherFreeAssignments(self): |
| 2496 |
module = Module("graOtherFreeAssignments") |
| 2497 |
module.addComment0("s[sgprWorkGroup2]") |
| 2498 |
return module |
| 2499 |
|
| 2500 |
|
| 2501 |
|
| 2502 |
|
| 2503 |
def graOtherSummationAssignments(self, kernel): |
| 2504 |
module = Module("graOtherSummationAssignments") |
| 2505 |
for i in range(0,kernel["ProblemType"]["NumIndicesSummation"]-1): |
| 2506 |
index = i |
| 2507 |
module.add(ValueSet(name="globalReadOffsetA%s" % self.states.indexChars[index], value=0)) |
| 2508 |
module.add(ValueSet(name="globalReadOffsetB%s" % self.states.indexChars[index], value=0)) |
| 2509 |
return module |
| 2510 |
|
| 2511 |
def alignTo(self, dstBase: Union[int, str], srcBase: Union[int, str], alignment: int) -> Module: |
| 2512 |
assert (alignment & (alignment - 1)) == 0 and f"Alignment must be power of two" |
| 2513 |
module = Module() |
| 2514 |
module.addComment(f"Align to {alignment}") |
| 2515 |
module.add(SAddU32(sgpr(dstBase), sgpr(srcBase), alignment-1)) |
| 2516 |
module.add(SLShiftRightB32(dst=sgpr(dstBase), src=sgpr(dstBase), shiftHex=log2(alignment))) |
| 2517 |
module.add(SLShiftLeftB32(dst=sgpr(dstBase), src=sgpr(dstBase), shiftHex=log2(alignment))) |
| 2518 |
return module |
| 2519 |
|
| 2520 |
def setTo(self, dstBase: Union[int, str], srcBase: Union[int, str]) -> Module: |
| 2521 |
module = Module() |
| 2522 |
module.add(SMovB32(dst=sgpr(dstBase), src=sgpr(srcBase), comment="Set to %s"%(srcBase))) |
| 2523 |
return module |
| 2524 |
|
| 2525 |
|
| 2526 |
|
| 2527 |
|
| 2528 |
def graTileOffsets(self, kernel, tP, margin=-1): |
| 2529 |
module = Module("graTileOffsets") |
| 2530 |
tc = tP["tensorChar"] |
| 2531 |
tP["vgprPackedOffsets"] = None |
| 2532 |
tP["vgprTileOffsetsCheckOut"] = False |
| 2533 |
tP["numVgprTileOffsets"] = 0 |
| 2534 |
if kernel["_UseSgprForGRO"]: |
| 2535 |
|
| 2536 |
tP["vgprTileOffsets"] = tP["gpr"]["tReg"] |
| 2537 |
else: |
| 2538 |
numTileOffsets = tP["nrt"] |
| 2539 |
tP["vgprTileOffsets"] = self.vgprPool.checkOut(numTileOffsets, "vgprTileOffsets", self.states.preventVgprOverflowDuringNewTile) |
| 2540 |
tP["vgprTileOffsetsCheckOut"] = True |
| 2541 |
v = tP["vgprTileOffsets"] |
| 2542 |
numExtraPackedOffsetsPerTile = len(tP["PackedIndices"])-1 |
| 2543 |
if numExtraPackedOffsetsPerTile: |
| 2544 |
tP["vgprPackedOffsets"] = self.vgprPool.checkOut(numExtraPackedOffsetsPerTile * numTileOffsets, "vgprPackedOffsets", self.states.preventVgprOverflowDuringNewTile) |
| 2545 |
strideIdx = tP["lsc"] if tP["tlu"] else tP["lsp"] |
| 2546 |
stride = kernel[strideIdx] |
| 2547 |
|
| 2548 |
strideInterleave = False |
| 2549 |
if (tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%c"%tc] and (not tP["tlu"]) and kernel["VectorWidth%s"%tc] > 1: |
| 2550 |
strideInterleave = True |
| 2551 |
stride = stride * kernel["VectorWidth%s"%tc] - (kernel["VectorWidth%s"%tc] - 1) |
| 2552 |
strideMask = (kernel["VectorWidth%s"%tc] - 1) |
| 2553 |
|
| 2554 |
if tP["isM"] and not margin == -1: |
| 2555 |
|
| 2556 |
|
| 2557 |
module.add(VLShiftRightB32(dst=vgpr(v), shiftHex=hex(log2(margin)), src=vgpr(tP["gpr"]["tReg"]), comment="gro%s%s_%u /= %d"%(tP["tensorChar"], tP["tileChar"], 0, margin))) |
| 2558 |
module.add(VLShiftLeftB32(dst=vgpr(v), shiftHex=hex(log2(margin)), src=vgpr(v), comment="gro%s%s_%u *= %d"%(tP["tensorChar"], tP["tileChar"], 0, margin))) |
| 2559 |
else: |
| 2560 |
if not tP["isSwizzled"]: |
| 2561 |
module.add(VMovB32(dst=vgpr(v), src=vgpr(tP["gpr"]["tReg"]), comment="gro%s%s_%u"%(tP["tensorChar"], tP["tileChar"], 0) )) |
| 2562 |
else: |
| 2563 |
lsu = kernel["LocalSplitU"] |
| 2564 |
if tP["isA"]: |
| 2565 |
WvG_MorN = kernel["MIWaveGroup"][0] |
| 2566 |
swzMorN = kernel["MatrixInstM"] |
| 2567 |
elif tP["isB"]: |
| 2568 |
WvG_MorN = kernel["MIWaveGroup"][1] |
| 2569 |
swzMorN = kernel["MatrixInstN"] |
| 2570 |
swzStride = tP["swizzleK"] |
| 2571 |
swzBlockSize = swzMorN * swzStride |
| 2572 |
vw = kernel[f"VectorWidth{tc}"] |
| 2573 |
kPack = tP["swizzlePackK"] |
| 2574 |
laneSize = int(kernel["MatrixInstK"] / 4) * kPack |
| 2575 |
|
| 2576 |
with self.allocTmpSgpr(2) as tmpSgprInfo: |
| 2577 |
swzBlkVWSizeSgpr = tmpSgprInfo.idx |
| 2578 |
swzStridePerWave = swzBlkVWSizeSgpr + 1 |
| 2579 |
|
| 2580 |
module.addComment0("SWZ-%s: calculation of gro%s%s_%u"%(tc, tP["tensorChar"], tP["tileChar"], 0)) |
| 2581 |
module.add(VMovB32(dst=vgpr(v), src=vgpr(tP["gpr"]["tReg"]))) |
| 2582 |
swzBlkVWSizeVgpr = self.vgprPool.checkOut(1) |
| 2583 |
module.add(SMovB32(dst=sgpr(swzBlkVWSizeSgpr), src=hex(swzBlockSize * vw), \ |
| 2584 |
comment="swizzled block * VW = (MI_MorN(%u) * MI_K(%u) * kPack) * VW(%u)" %(swzMorN, int(swzStride / kPack), vw))) |
| 2585 |
module.add(VMovB32(dst=vgpr(swzBlkVWSizeVgpr), src=sgpr(swzBlkVWSizeSgpr))) |
| 2586 |
module.add(VMulU32U24(dst=vgpr(v), src0=vgpr(v), src1=vgpr(swzBlkVWSizeVgpr))) |
| 2587 |
|
| 2588 |
|
| 2589 |
if lsu > 1: |
| 2590 |
tmpVgprRes = None |
| 2591 |
wave_id = self.vgprPool.checkOut(1) |
| 2592 |
|
| 2593 |
du = kernel["DepthU"] |
| 2594 |
lsuStride = du // lsu |
| 2595 |
numWaves = kernel["MIWaveGroup"][0] * kernel["MIWaveGroup"][1] |
| 2596 |
|
| 2597 |
module.add(vectorStaticDivide(wave_id, "Serial", kernel["WavefrontSize"] * numWaves, tmpVgprRes, comment="LSU offset: Get LSU wave_id")) |
| 2598 |
module.add(VLShiftLeftB32(dst=vgpr(wave_id), shiftHex=hex(log2(lsuStride*swzMorN)), src=vgpr(wave_id), comment="LSU offset: LSU_wave_id*MI_MorN(%u)*lsuStrideK(%u)"%(swzMorN, lsuStride))) |
| 2599 |
module.add(VAddU32(dst=vgpr(v), src0=vgpr(wave_id), src1=vgpr(v), comment="tileOffset += LSU offset")) |
| 2600 |
self.vgprPool.checkIn(wave_id) |
| 2601 |
|
| 2602 |
|
| 2603 |
if tP["nrt"] > 1: |
| 2604 |
module.addComment0("swzStridePerWave = (number of swizzle block in K) * WaveGroup_MorN") |
| 2605 |
module.addComment(f"Align to {swzStride}") |
| 2606 |
module.add(SAddU32(sgpr(swzStridePerWave), sgpr("SizesSum"), swzStride-1)), |
| 2607 |
module.add(SLShiftRightB32(dst=sgpr(swzStridePerWave), src=sgpr(swzStridePerWave), shiftHex=hex(log2(swzStride)), |
| 2608 |
comment="numKr = DimK / swizzleK")), |
| 2609 |
module.add(SMulI32(dst=sgpr(swzStridePerWave), src0=hex(WvG_MorN), src1=sgpr(swzStridePerWave), |
| 2610 |
comment="numKr *= MI_WaveGroup, wave-M (SWZ-A) or wave-N (SWZ-B)")) |
| 2611 |
|
| 2612 |
module.addComment0("swzBlkVWOffset = swzBlkWvGSize - laneSize * (VW - 1)") |
| 2613 |
module.add(SMulI32(dst=sgpr(swzBlkVWSizeSgpr), src0=sgpr(swzStridePerWave), src1=sgpr(swzBlkVWSizeSgpr), \ |
| 2614 |
comment="swzBlkWvGSize = numKr * (swzBlockSize * VW)")) |
| 2615 |
module.add(SSubU32(dst=sgpr(swzBlkVWSizeSgpr), src0=sgpr(swzBlkVWSizeSgpr), src1=(laneSize * (vw - 1)), \ |
| 2616 |
comment="swzBlkVWOffset = swzBlkWvGSize - laneSize * (VW - 1)")) |
| 2617 |
module.add(VMovB32(dst=vgpr(swzBlkVWSizeVgpr), src=sgpr(swzBlkVWSizeSgpr)) ) |
| 2618 |
|
| 2619 |
for l in range(1, tP["nrt"]): |
| 2620 |
strideValue = stride |
| 2621 |
if strideInterleave and (l & strideMask) != 0: |
| 2622 |
strideValue = 1 |
| 2623 |
if not tP["isSwizzled"]: |
| 2624 |
module.add(VAddCOU32(dst=vgpr(v+l), dst1=VCC(), src0=strideValue, \ |
| 2625 |
src1=vgpr(v+l-1), comment="gro%s%s_%u += %s"%(tP["tensorChar"], tP["tileChar"], l, strideIdx) )) |
| 2626 |
|
| 2627 |
else: |
| 2628 |
|
| 2629 |
if (strideInterleave and (l & strideMask) != 0): |
| 2630 |
module.add(VAddCOU32(dst=vgpr(v+l), dst1=VCC(), src0=laneSize, \ |
| 2631 |
src1=vgpr(v+l-1), comment="SWZ-%s: gro%s%s_%u"%(tc, tP["tensorChar"], tP["tileChar"], l) )) |
| 2632 |
|
| 2633 |
else: |
| 2634 |
module.add(VAddCOU32(dst=vgpr(v+l), dst1=VCC(), src0=vgpr(swzBlkVWSizeVgpr), \ |
| 2635 |
src1=vgpr(v+l-1), comment="SWZ-%s: gro%s%s_%u"%(tc, tP["tensorChar"], tP["tileChar"], l) )) |
| 2636 |
if tP["isSwizzled"]: |
| 2637 |
self.vgprPool.checkIn(swzBlkVWSizeVgpr) |
| 2638 |
|
| 2639 |
|
| 2640 |
if numExtraPackedOffsetsPerTile: |
| 2641 |
tmpV = self.vgprPool.checkOutAligned(2,2,"packTmp", self.states.preventVgprOverflowDuringNewTile) |
| 2642 |
|
| 2643 |
for l in range(0, tP["nrt"]): |
| 2644 |
lastGroVgpr = vgpr(v+l) |
| 2645 |
lastGroIdx = tP["PackedIndices"][0] |
| 2646 |
module.addSpaceLine() |
| 2647 |
for p in range(0, numExtraPackedOffsetsPerTile): |
| 2648 |
groIdx = tP["PackedIndices"][p+1] |
| 2649 |
groChar = INDEX_CHARS[tP["PackedIndices"][p+1]] |
| 2650 |
groVgpr = vgpr(tP["vgprPackedOffsets"] + l*numExtraPackedOffsetsPerTile + p) |
| 2651 |
pChar = INDEX_CHARS[tP["PackedIndices"][p]] |
| 2652 |
module.add(MacroInstruction(name="V_MAGIC_DIV", \ |
| 2653 |
args=[tmpV, lastGroVgpr, sgpr("MagicNumberSize%s"%pChar), \ |
| 2654 |
sgpr("MagicShiftSize%s"%pChar), (sgpr("MagicAbitSize%s"%pChar) if kernel["MagicDivAlg"]==2 else "0")] )) |
| 2655 |
module.add(VMovB32(dst=groVgpr, src=vgpr(tmpV), comment="extract gro%s%s_%u (%s)"%(tc,groChar,l,groVgpr))) |
| 2656 |
module.add(VMulLOU32(dst=vgpr(tmpV), src0=groVgpr, src1=sgpr("SizesFree+%u"%lastGroIdx), comment="remainder part 1")) |
| 2657 |
module.add(VSubU32(dst=lastGroVgpr, src0=lastGroVgpr, src1=vgpr(tmpV), \ |
| 2658 |
comment="remove extracted bits from gro%s%s_%u (%s)"%(tc, INDEX_CHARS[lastGroIdx], l, lastGroVgpr))) |
| 2659 |
lastGroVgpr = groVgpr |
| 2660 |
lastGroIdx = groIdx |
| 2661 |
self.vgprPool.checkIn(tmpV) |
| 2662 |
|
| 2663 |
|
| 2664 |
if tP["gpr"]["lwoT"] != tP["gpr"]["tReg"] : |
| 2665 |
self.vgprPool.checkIn(tP["gpr"]["tReg"]) |
| 2666 |
tP["gpr"]["tReg"] = None |
| 2667 |
return Module("graTileOffsets (Empty)") if self.dontAppendCode else module |
| 2668 |
|
| 2669 |
|
| 2670 |
|
| 2671 |
|
| 2672 |
def graUnrollOffsets(self, kernel, tP): |
| 2673 |
module = Module("graUnrollOffsets") |
| 2674 |
tc = tP["tensorChar"] |
| 2675 |
if kernel["_UseSgprForGRO"]: |
| 2676 |
tP["gpr"]["unrollOffsets"] = tP["gpr"]["uReg"] |
| 2677 |
|
| 2678 |
elif tP["isSwizzled"]: |
| 2679 |
numUnrollOffsets = tP["nru"] |
| 2680 |
tP["gpr"]["unrollOffsets"] = self.vgprPool.checkOut(numUnrollOffsets, "unrollOffsets", self.states.preventVgprOverflowDuringNewTile) |
| 2681 |
v = tP["gpr"]["unrollOffsets"] |
| 2682 |
|
| 2683 |
|
| 2684 |
|
| 2685 |
|
| 2686 |
|
| 2687 |
|
| 2688 |
|
| 2689 |
|
| 2690 |
|
| 2691 |
|
| 2692 |
|
| 2693 |
|
| 2694 |
|
| 2695 |
|
| 2696 |
|
| 2697 |
|
| 2698 |
if tP["isA"]: |
| 2699 |
swzMorN = kernel["MatrixInstM"] |
| 2700 |
elif tP["isB"]: |
| 2701 |
swzMorN = kernel["MatrixInstN"] |
| 2702 |
swzStride = tP["swizzleK"] |
| 2703 |
vw = kernel[f"VectorWidth{tc}"] |
| 2704 |
kPack = tP["swizzlePackK"] |
| 2705 |
laneSize = int(kernel["MatrixInstK"] / 4) * kPack |
| 2706 |
numElmInSwzBlk = swzMorN * swzStride |
| 2707 |
|
| 2708 |
|
| 2709 |
row = self.vgprPool.checkOut(1) |
| 2710 |
col = self.vgprPool.checkOut(1) |
| 2711 |
tmpVgpr = self.vgprPool.checkOut(1) |
| 2712 |
module.addComment0("SWZ-%s: r = swzRow = (tid / swzMorN(%u)) * laneSize(%u)"%(tc, swzMorN, laneSize)) |
| 2713 |
module.add(VAndB32(dst=vgpr(v), src0=vgpr("Serial"), src1=(kernel["WavefrontSize"]-1), comment="tid")) |
| 2714 |
module.add(VLShiftRightB32(dst=vgpr(row), shiftHex=hex(int(log(swzMorN, 2))), src=vgpr(v))) |
| 2715 |
module.add(VLShiftLeftB32(dst=vgpr(row), shiftHex=hex(int(log(laneSize, 2))), src=vgpr(row))) |
| 2716 |
module.addComment0("SWZ-%s: c = swzCol = [tid mod (swzMorN(%u) / VW(%u))] * VW(%u)"%(tc, swzMorN, vw, vw)) |
| 2717 |
module.add(VAndB32(dst=vgpr(col), src0=vgpr(v), src1=int(swzMorN/vw)-1)) |
| 2718 |
if vw > 1: |
| 2719 |
module.add(VLShiftLeftB32(dst=vgpr(col), shiftHex=hex(int(log(vw, 2))), src=vgpr(col))) |
| 2720 |
|
| 2721 |
module.addComment0("SWZ-%s: F = r mod laneSize + c * laneSize + (r/laneSize) * swzMorN * laneSize"%tc) |
| 2722 |
module.add(VLShiftRightB32(dst=vgpr(tmpVgpr), shiftHex=hex(int(log(laneSize, 2))), src=vgpr(row), comment="tmp = r / laneSize")) |
| 2723 |
module.add(VLShiftLeftB32(dst=vgpr(tmpVgpr), shiftHex=hex(int(log(swzMorN * laneSize, 2))), src=vgpr(tmpVgpr), comment="tmp *= swzMorN * laneSize")) |
| 2724 |
module.add(VMadU32U24(dst=vgpr(tmpVgpr), src0=vgpr(col), src1=laneSize, src2=vgpr(tmpVgpr), comment="tmp += c * laneSize")) |
| 2725 |
module.add(VAndB32(dst=vgpr(row), src0=vgpr(row), src1=laneSize-1, comment="tmp2 = row mod laneSize")) |
| 2726 |
if vw == 1: |
| 2727 |
module.add(VAddU32(dst=vgpr(v), src0=vgpr(tmpVgpr), src1=vgpr(row), comment="offset = tmp + tmp2")) |
| 2728 |
|
| 2729 |
else: |
| 2730 |
fVgpr = self.vgprPool.checkOut(1) |
| 2731 |
module.add(VAddU32(dst=vgpr(fVgpr), src0=vgpr(tmpVgpr), src1=vgpr(row), comment="F = tmp + tmp2")) |
| 2732 |
|
| 2733 |
module.addComment0("SWZ-%s: swzSizeK = numSwzBlkK * numElmInSwzBlk"%tc) |
| 2734 |
swzSizeK = self.vgprPool.checkOut(1) |
| 2735 |
tmpSgpr = self.sgprPool.checkOut(1) |
| 2736 |
module.add(SAddU32(sgpr(tmpSgpr), sgpr("SizesSum"), swzStride - 1, comment="Align to %u"%swzStride)) |
| 2737 |
module.add(SLShiftRightB32(dst=sgpr(tmpSgpr), src=sgpr(tmpSgpr), shiftHex=hex(log2(swzStride)), comment="numKr = DimK / %u"%swzStride)) |
| 2738 |
module.add(SMulI32(dst=sgpr(tmpSgpr, 1), src0=sgpr(tmpSgpr, 1), src1=hex(numElmInSwzBlk))) |
| 2739 |
module.add(VMovB32(dst=vgpr(swzSizeK), src=sgpr(tmpSgpr, 1))) |
| 2740 |
self.sgprPool.checkIn(tmpSgpr) |
| 2741 |
|
| 2742 |
module.addComment0("SWZ-%s: swizzle offset = F + [(tid / (swzMorN(%u) / VW(%u))) mod VW] * swzSizeK"%(tc, swzMorN, vw)) |
| 2743 |
module.add(VLShiftRightB32(dst=vgpr(tmpVgpr), shiftHex=hex(int(log((swzMorN/vw), 2))), src=vgpr(v), comment="tmp = tid / (swzMorN / VW)")) |
| 2744 |
module.add(VAndB32(dst=vgpr(tmpVgpr), src0=vgpr(tmpVgpr), src1=vw-1, comment="tmp = tmp mod VW")) |
| 2745 |
module.add(VMadU32U24(dst=vgpr(v), src0=vgpr(tmpVgpr), src1=vgpr(swzSizeK), src2=vgpr(fVgpr), comment="offset = tmp * swzSizeK + F")) |
| 2746 |
self.vgprPool.checkIn(fVgpr) |
| 2747 |
self.vgprPool.checkIn(swzSizeK) |
| 2748 |
|
| 2749 |
self.vgprPool.checkIn(row) |
| 2750 |
self.vgprPool.checkIn(col) |
| 2751 |
self.vgprPool.checkIn(tmpVgpr) |
| 2752 |
|
| 2753 |
for l in range(1, tP["nru"]): |
| 2754 |
module.add(VAddCOU32(dst=vgpr(v+l), dst1=VCC(), src0=hex(numElmInSwzBlk), \ |
| 2755 |
src1=vgpr(v+l-1), comment="gro%s%s_%u"%(tP["tensorChar"], self.states.unrollChar, l))) |
| 2756 |
|
| 2757 |
else: |
| 2758 |
numUnrollOffsets = tP["nru"] |
| 2759 |
tP["gpr"]["unrollOffsets"] = self.vgprPool.checkOut(numUnrollOffsets, "unrollOffsets", self.states.preventVgprOverflowDuringNewTile) |
| 2760 |
v = tP["gpr"]["unrollOffsets"] |
| 2761 |
strideIdx = (tP["lsp"] if tP["tlu"] else tP["lsc"]) |
| 2762 |
stride = kernel[strideIdx] |
| 2763 |
if (tc == "A" or tc == "B") and kernel["DirectToVgpr%s"%tc] and kernel["LocalSplitU"] > 1: |
| 2764 |
|
| 2765 |
stride = stride // kernel["LocalSplitU"] |
| 2766 |
prevStride = 0 |
| 2767 |
totalStride = 0 |
| 2768 |
dtvKInterval = self.states.dtvKIntervalA if tP["isA"] else self.states.dtvKIntervalB |
| 2769 |
module.add(VMovB32(dst=vgpr(v), src=vgpr(tP["gpr"]["uReg"]), comment="gro%s%s_%u"%(tP["tensorChar"], self.states.unrollChar, 0))) |
| 2770 |
for l in range(1, tP["nru"]): |
| 2771 |
totalStride += stride |
| 2772 |
if dtvKInterval > 1: |
| 2773 |
|
| 2774 |
|
| 2775 |
totalStride = stride * (l - (l % dtvKInterval)) + (l % dtvKInterval) |
| 2776 |
currStride = totalStride - prevStride |
| 2777 |
prevStride = totalStride |
| 2778 |
module.add(VAddCOU32(dst=vgpr(v+l), dst1=VCC(), src0=currStride, \ |
| 2779 |
src1=vgpr(v+l-1), comment="gro%s%s_%u + %s"%(tP["tensorChar"], self.states.unrollChar, l, strideIdx))) |
| 2780 |
|
| 2781 |
return Module("graUnrollOffsets (Empty)") if self.dontAppendCode else module |
| 2782 |
|
| 2783 |
|
| 2784 |
|
| 2785 |
|
| 2786 |
|
| 2787 |
|
| 2788 |
def graMetadataShift(self, kernel, tP): |
| 2789 |
module = Module("graMetadataShift") |
| 2790 |
|
| 2791 |
if kernel["DirectToVgprSparseMetadata"]: |
| 2792 |
margin = tP["glvw"] if tP["rtv"] else 1 |
| 2793 |
|
| 2794 |
|
| 2795 |
with self.allocTmpSgpr(2+self.states.laneSGPRCount) as tmpSgprInfo: |
| 2796 |
edgeSgpr = tmpSgprInfo.idx |
| 2797 |
shiftSgpr = edgeSgpr+1 |
| 2798 |
laneMaskSgpr = edgeSgpr+2 |
| 2799 |
module.add(SMulI32(dst=sgpr(edgeSgpr), src0=sgpr(tP["wg"]), src1=kernel[tP["mt"]], comment="WorkGroup[01] * MT")) |
| 2800 |
module.add(SSubI32(dst=sgpr(edgeSgpr), src0=self.sizeRef(tP["idx"]), src1=sgpr(edgeSgpr), comment="edge = Size%s - WG*MT"%(tP["tileChar"]))) |
| 2801 |
module.add(SAndB32(dst=sgpr(shiftSgpr), src0=sgpr(edgeSgpr), src1=hex(margin-1), comment="edge size & (glvw-1)")) |
| 2802 |
module.add(SSubU32(dst=sgpr(shiftSgpr), src0=hex(margin), src1=sgpr(shiftSgpr), comment="shift size = glvw - (edge size & (glvw-1))")) |
| 2803 |
module.add(SAndN2B32(dst=sgpr(edgeSgpr), src0=sgpr(edgeSgpr), src1=hex(margin-1), comment="edgeCoord = edge & !(glvw-1)")) |
| 2804 |
|
| 2805 |
|
| 2806 |
shiftedCoord = self.vgprPool.checkOut(1, "shiftedCoord", self.states.preventVgprOverflowDuringNewTile) |
| 2807 |
for graIdx in range (0, kernel["MIWaveTile"][0]): |
| 2808 |
vgprGro = "GlobalReadOffsetMetadata+%u"%(graIdx) |
| 2809 |
|
| 2810 |
module.add(VCmpLeI32(dst=sgpr(laneMaskSgpr,self.states.laneSGPRCount), src0=sgpr(edgeSgpr), src1=vgpr(vgprGro), comment="edgeCoord <= coord")) |
| 2811 |
|
| 2812 |
module.add(VSubI32(dst=vgpr(shiftedCoord), src0=vgpr(vgprGro), src1=sgpr(shiftSgpr), comment="shiftedCoord = coord - shift size")) |
| 2813 |
|
| 2814 |
module.add(VCndMaskB32(dst=vgpr(vgprGro), src0=vgpr(vgprGro), src1=vgpr(shiftedCoord), src2=sgpr(laneMaskSgpr,self.states.laneSGPRCount), |
| 2815 |
comment="coord = (cond) ? shifted coord : ori coord")) |
| 2816 |
self.vgprPool.checkIn(shiftedCoord) |
| 2817 |
|
| 2818 |
return module |
| 2819 |
|
| 2820 |
|
| 2821 |
def graShift(self, kernel, tP, margin=-1): |
| 2822 |
|
| 2823 |
|
| 2824 |
|
| 2825 |
assert(not kernel["_UseSgprForGRO"]), "%s"%self.states.kernelName |
| 2826 |
|
| 2827 |
module = Module("graShift") |
| 2828 |
|
| 2829 |
|
| 2830 |
marginO = margin |
| 2831 |
|
| 2832 |
|
| 2833 |
if margin == -1: |
| 2834 |
margin = tP["glvw"] if tP["rtv"] else 1 |
| 2835 |
edge = self.vgprPool.checkOut(1, "edge", self.states.preventVgprOverflowDuringNewTile) |
| 2836 |
|
| 2837 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 2838 |
tmpSgpr = tmpSgprInfo.idx |
| 2839 |
if self.states.groOffsetInMacroTile: |
| 2840 |
|
| 2841 |
module.add(SMulI32(dst=sgpr(tmpSgpr), src0=sgpr(tP["wg"]), src1=kernel[tP["mt"]], comment="WorkGroup[01] * MT")) |
| 2842 |
module.add(SSubU32(dst=sgpr(tmpSgpr), src0=self.sizeRef(tP["idx"]), src1=sgpr(tmpSgpr), \ |
| 2843 |
comment="edge = Size%s - WG*MT"%(tP["tileChar"]))) |
| 2844 |
|
| 2845 |
|
| 2846 |
|
| 2847 |
|
| 2848 |
|
| 2849 |
|
| 2850 |
module.add(SSubU32(dst=sgpr(tmpSgpr), src0=sgpr(tmpSgpr), src1=margin, comment="edge -= margin(%u)"%(margin))) |
| 2851 |
module.add(VMovB32(dst=vgpr(edge), src=sgpr(tmpSgpr), comment="edge vgpr = Size%s- WG*MT - margin(%u)"%(tP["tileChar"], margin) )) |
| 2852 |
|
| 2853 |
|
| 2854 |
|
| 2855 |
else: |
| 2856 |
module.add(SSubU32(dst=sgpr(tmpSgpr), src0=self.sizeRef(tP["idx"]), src1=margin, \ |
| 2857 |
comment="edge = Size%s-%u"%(tP["tileChar"], margin) )) |
| 2858 |
module.add(VMovB32(dst=vgpr(edge), src=sgpr(tmpSgpr), \ |
| 2859 |
comment="edge vgpr = Size%s-%u"%(tP["tileChar"], margin) )) |
| 2860 |
|
| 2861 |
|
| 2862 |
vSrc = tP["vgprTileOffsets"] |
| 2863 |
vDst = tP["vgprTileOffsets"] |
| 2864 |
with self.allocTmpSgpr(self.states.laneSGPRCount) as tmpSgprInfo: |
| 2865 |
tmpSgpr = tmpSgprInfo.idx |
| 2866 |
for l in range(0, tP["nrt"]): |
| 2867 |
|
| 2868 |
cmpCommentText = "offset < edge" |
| 2869 |
if self.states.groOffsetInMacroTile: |
| 2870 |
module.add(VMinI32(dst=vgpr(vDst+l), src0=vgpr(edge), src1=vgpr(vSrc+l), |
| 2871 |
comment="offset = (%s) ? offset(v%u) : edge(v%u)"%(cmpCommentText, vSrc+l, edge))) |
| 2872 |
else: |
| 2873 |
module.add(VCmpLtU32(dst=sgpr(tmpSgpr,self.states.laneSGPRCount), src0=vgpr(vSrc+l), src1=vgpr(edge), |
| 2874 |
comment="shiftedOffset < shiftedEdge")) |
| 2875 |
|
| 2876 |
module.add(VCndMaskB32(dst=vgpr(vDst+l), src0=vgpr(edge), src1=vgpr(vSrc+l), src2=sgpr(tmpSgpr,self.states.laneSGPRCount), |
| 2877 |
comment="offset = (%s) ? offset(v%u) : edge(v%u)"%(cmpCommentText, vSrc+l, edge))) |
| 2878 |
|
| 2879 |
if tP["isM"] and not marginO == -1: |
| 2880 |
module.add(VAndB32(dst=vgpr(edge), src0=(margin-1), src1=vgpr(tP["gpr"]["tReg"]), comment="shifTailOffstet = tailOffset %% %d"%(margin))) |
| 2881 |
for l in range(0, tP["nrt"]): |
| 2882 |
module.add(VAddU32(dst=vgpr(vDst+l), src0=vgpr(edge), src1=vgpr(vSrc+l), |
| 2883 |
comment="offset += shifTailOffstet")) |
| 2884 |
self.vgprPool.checkIn(edge) |
| 2885 |
return module |
| 2886 |
|
| 2887 |
|
| 2888 |
|
| 2889 |
|
| 2890 |
def graMetadataFinalOffsets(self, kernel, tP): |
| 2891 |
module = Module("graMetadataFinalOffsets") |
| 2892 |
module.addComment1("calculate metadata gra final offset") |
| 2893 |
|
| 2894 |
|
| 2895 |
|
| 2896 |
kReg = self.vgprPool.checkOut(1,"kReg") |
| 2897 |
tmpVgpr = self.vgprPool.checkOutAligned(2,2,"tmpVgpr") |
| 2898 |
dummy = self.vgprPool.checkOut(1,"dummy") |
| 2899 |
|
| 2900 |
tmpVgprRes = ContinuousRegister(tmpVgpr, 2) |
| 2901 |
|
| 2902 |
|
| 2903 |
tc = tP["tensorChar"] |
| 2904 |
waveWidth = kernel["WavefrontSize"] |
| 2905 |
inputPerThread = kernel["MIInputPerThread"] |
| 2906 |
|
| 2907 |
|
| 2908 |
dividendForKId = kernel["MatrixInstM"] |
| 2909 |
unrollSummation = [ i for i in tP["ia"] if i in kernel["ProblemType"]["IndicesSummation"] ] |
| 2910 |
|
| 2911 |
|
| 2912 |
strideTile = self.sizeRef(unrollSummation[-1]) |
| 2913 |
strideK = inputPerThread |
| 2914 |
|
| 2915 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 2916 |
|
| 2917 |
module.add(vectorStaticRemainder(dummy, kReg, "Serial", waveWidth, tmpVgprRes, tmpSgprInfo, \ |
| 2918 |
"0. thread id in wave: wtid = tid %% wavelength(%u)" % waveWidth)) |
| 2919 |
module.add(vectorStaticDivide(kReg, kReg, dividendForKId, tmpVgprRes, \ |
| 2920 |
"1. unroll offset: kIdx = wtid / (MIN(%u) )" % (kernel["MatrixInstN"]))) |
| 2921 |
module.add(vectorStaticMultiply(vgpr(kReg), vgpr(kReg), strideK, tmpSgprInfo, \ |
| 2922 |
"1. unroll offset: grKOffset = kIdx * mStride(%u)" % strideK)) |
| 2923 |
|
| 2924 |
|
| 2925 |
for graIdx in range (0, kernel["MIWaveTile"][0]): |
| 2926 |
vgprGro = "GlobalReadOffsetMetadata+%u"%(graIdx) |
| 2927 |
module.add(VMulLOU32(vgpr(vgprGro), vgpr(vgprGro), strideTile, \ |
| 2928 |
"2. tile offset: tile coord * sizeL")) |
| 2929 |
module.add(VAddU32(vgpr(vgprGro), vgpr(vgprGro), vgpr(kReg), \ |
| 2930 |
"2. final global read offset: fgrOffset = tile Offset + unroll Offset")) |
| 2931 |
|
| 2932 |
module.add(vectorStaticDivide(vgprGro, vgprGro, 8, tmpVgprRes, \ |
| 2933 |
" 3. bytes offset : bnIdx = global read elememt offset / 8")) |
| 2934 |
|
| 2935 |
module.add(Label("graFinalMeta", "")) |
| 2936 |
|
| 2937 |
|
| 2938 |
self.vgprPool.checkIn(kReg) |
| 2939 |
self.vgprPool.checkIn(tmpVgpr) |
| 2940 |
self.vgprPool.checkIn(dummy) |
| 2941 |
|
| 2942 |
return module |
| 2943 |
|
| 2944 |
|
| 2945 |
|
| 2946 |
|
| 2947 |
def graFinalOffsets(self, kernel, tP): |
| 2948 |
module = Module("graFinalOffsets") |
| 2949 |
tc = tP["tensorChar"] |
| 2950 |
tmp = self.vgprPool.checkOut(3, "tmp", self.states.preventVgprOverflowDuringNewTile) |
| 2951 |
graIdx = 0 |
| 2952 |
swapPerpPara = (((tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc]) and (not tP["tlu"]) and tP["nrp"] > 1) |
| 2953 |
|
| 2954 |
|
| 2955 |
if ((tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc]) and kernel["_UseSgprForGRO"]: |
| 2956 |
if tP["tlu"]: |
| 2957 |
sizeOfaPart = kernel["LocalReadVectorWidth"] |
| 2958 |
numOfVW = tP["nrp"]//sizeOfaPart |
| 2959 |
for sPerp in range(0, tP["nrpv"]*numOfVW): |
| 2960 |
for perp in range(0, sizeOfaPart): |
| 2961 |
for para in range(0, tP["nrc"]): |
| 2962 |
for sPara in range(0, tP["nrcv"]//tP["nrcvpi"]): |
| 2963 |
|
| 2964 |
singleModule, graIdx = self.graFinalOffsetsSingleLoop(kernel, tP, tc, tmp, graIdx, perp, sPerp, para, sPara) |
| 2965 |
module.add(singleModule) |
| 2966 |
else: |
| 2967 |
numOfVW = kernel["MIWaveTile%c"%tc]//kernel["VectorWidth%s"%tc] if kernel["VectorWidth%c"%tc]>1 else 1 |
| 2968 |
sizeOfaPart = tP["nrp"]//numOfVW |
| 2969 |
|
| 2970 |
for para in range(0, tP["nrc"]): |
| 2971 |
for sPara in range(0, int(tP["nrcv"]/tP["nrcvpi"])): |
| 2972 |
for sPerp in range(0, tP["nrpv"]*numOfVW): |
| 2973 |
for perp in range(0, sizeOfaPart): |
| 2974 |
|
| 2975 |
singleModule, graIdx = self.graFinalOffsetsSingleLoop(kernel, tP, tc, tmp, graIdx, perp, sPerp, para, sPara) |
| 2976 |
module.add(singleModule) |
| 2977 |
|
| 2978 |
elif (not swapPerpPara): |
| 2979 |
for perp in range(0, tP["nrp"]): |
| 2980 |
for sPerp in range(0, tP["nrpv"]): |
| 2981 |
for para in range(0, tP["nrc"]): |
| 2982 |
for sPara in range(0, tP["nrcv"]//tP["nrcvpi"]): |
| 2983 |
|
| 2984 |
singleModule, graIdx = self.graFinalOffsetsSingleLoop(kernel, tP, tc, tmp, graIdx, perp, sPerp, para, sPara) |
| 2985 |
module.add(singleModule) |
| 2986 |
else: |
| 2987 |
|
| 2988 |
for para in range(0, tP["nrc"]): |
| 2989 |
for sPara in range(0, int(tP["nrcv"]/tP["nrcvpi"])): |
| 2990 |
for perp in range(0, tP["nrp"]): |
| 2991 |
for sPerp in range(0, tP["nrpv"]): |
| 2992 |
|
| 2993 |
singleModule, graIdx = self.graFinalOffsetsSingleLoop(kernel, tP, tc, tmp, graIdx, perp, sPerp, para, sPara) |
| 2994 |
module.add(singleModule) |
| 2995 |
|
| 2996 |
self.vgprPool.checkIn(tP["gpr"]["lwoT"]) |
| 2997 |
tP["gpr"]["lwoT"] = None |
| 2998 |
|
| 2999 |
self.vgprPool.checkIn(tP["gpr"]["uReg2"]) |
| 3000 |
tP["gpr"]["uReg2"] = None |
| 3001 |
|
| 3002 |
self.vgprPool.checkIn(tP["gpr"]["uReg"]) |
| 3003 |
tP["gpr"]["uReg"] = None |
| 3004 |
if "subIterReg" in tP["gpr"]: |
| 3005 |
if tP["gpr"]["subIterReg"] is not None: |
| 3006 |
self.vgprPool.checkIn(tP["gpr"]["subIterReg"]) |
| 3007 |
tP["gpr"]["subIterReg"] = None |
| 3008 |
|
| 3009 |
if tP["vgprTileOffsetsCheckOut"]: |
| 3010 |
self.vgprPool.checkIn(tP["vgprTileOffsets"]) |
| 3011 |
tP["vgprTileOffsets"] = None |
| 3012 |
tP["vgprTileOffsetsCheckOut"] = False |
| 3013 |
|
| 3014 |
|
| 3015 |
|
| 3016 |
|
| 3017 |
if not kernel["_UseSgprForGRO"]: |
| 3018 |
self.vgprPool.checkIn(tP["gpr"]["unrollOffsets"]) |
| 3019 |
tP["gpr"]["unrollOffsets"] = None |
| 3020 |
|
| 3021 |
if tP["vgprPackedOffsets"] != None: |
| 3022 |
self.vgprPool.checkIn(tP["vgprPackedOffsets"]) |
| 3023 |
tP["vgprPackedOffsets"] = None |
| 3024 |
|
| 3025 |
self.vgprPool.checkIn(tmp) |
| 3026 |
|
| 3027 |
return Module("Global Read Addresses: Final Offsets A/B (Empty)") if self.dontAppendCode else module |
| 3028 |
|
| 3029 |
|
| 3030 |
|
| 3031 |
|
| 3032 |
def graFinalOffsetsSingleLoop(self, kernel, tP, tc, tmp, graIdx, perp, sPerp, para, sPara): |
| 3033 |
module = Module("graFinalOffsetsSingleLoop") |
| 3034 |
problemType = kernel["ProblemType"] |
| 3035 |
tVW = 1 |
| 3036 |
tVS = 0 |
| 3037 |
uVW = 1 |
| 3038 |
uVS = 0 |
| 3039 |
|
| 3040 |
|
| 3041 |
|
| 3042 |
if tP["tlu"]: |
| 3043 |
vgprTile = tP["vgprTileOffsets"] + para*tVW + sPara*tVS |
| 3044 |
vgprUnroll = tP["gpr"]["unrollOffsets"] + perp*uVW + sPerp*uVS |
| 3045 |
else: |
| 3046 |
vgprTile = tP["vgprTileOffsets"] + perp*tVW + sPara*tVS |
| 3047 |
vgprUnroll = tP["gpr"]["unrollOffsets"] + para*uVW + sPerp*uVS |
| 3048 |
|
| 3049 |
if graIdx==0 or not kernel["_UseSgprForGRO"]: |
| 3050 |
|
| 3051 |
|
| 3052 |
if kernel["BufferLoad"]: |
| 3053 |
if tP["isSwizzled"]: |
| 3054 |
bfName = "GLOBAL_OFFSET_%s_SWIZZLED" % tP["tensorChar"] |
| 3055 |
else: |
| 3056 |
bfName = "GLOBAL_OFFSET_%s" % tP["tensorChar"] |
| 3057 |
bfArgs = ["vgprGlobalReadOffset%s+%u"%(tP["tensorChar"], graIdx)] |
| 3058 |
else: |
| 3059 |
bfName = "GLOBAL_OFFSET_%s" % tP["tensorChar"] |
| 3060 |
bfArgs = ["vgprGlobalReadAddr%s+%u"%(tP["tensorChar"], graIdx)] |
| 3061 |
packedIter = 0 |
| 3062 |
iaToGpr = [None] * problemType["TotalIndices"] |
| 3063 |
for i in tP["ia"]: |
| 3064 |
if i < problemType["NumIndicesC"]: |
| 3065 |
if i == tP["tileIdx"]: |
| 3066 |
iaToGpr[i] = vgprTile |
| 3067 |
bfArgs.append( "%2u" % iaToGpr[i] ) |
| 3068 |
else: |
| 3069 |
if isPackedIndex(kernel,i): |
| 3070 |
iaToGpr[i] = tP["vgprPackedOffsets"] + \ |
| 3071 |
(vgprTile-tP["vgprTileOffsets"])*(len(tP["PackedIndices"])-1) + \ |
| 3072 |
packedIter |
| 3073 |
bfArgs.append( "%2u" % (iaToGpr[i]) ) |
| 3074 |
packedIter += 1 |
| 3075 |
else: |
| 3076 |
|
| 3077 |
if not kernel["BufferLoad"]: |
| 3078 |
bfArgs.append( "sgprWorkGroup%u"%i ) |
| 3079 |
else: |
| 3080 |
if i == problemType["IndexUnroll"]: |
| 3081 |
iaToGpr[i] = vgprUnroll |
| 3082 |
bfArgs.append( "%2u" % iaToGpr[i] ) |
| 3083 |
|
| 3084 |
|
| 3085 |
bfArgs.append( "%u" % tmp ) |
| 3086 |
bfComment = "gRO%s_%u_%u_%u_%u" % (tP["tensorChar"], para, sPara, perp, sPerp) |
| 3087 |
module.add(MacroInstruction(name=bfName, args=bfArgs, comment=bfComment)) |
| 3088 |
|
| 3089 |
with self.allocTmpSgpr(2) as tmpSgprInfo: |
| 3090 |
tmpSgpr = tmpSgprInfo.idx |
| 3091 |
|
| 3092 |
|
| 3093 |
if (not kernel["_UseSgprForGRO"]) and kernel["DirectToLds%s"%tc] and kernel["UseInstOffsetForGRO"]: |
| 3094 |
|
| 3095 |
groVgpr = "GlobalReadOffset%s+%u" % (tP["tensorChar"], graIdx) |
| 3096 |
module.add(SMovB32(dst=sgpr(tmpSgpr), src=self.buff_load_inst_offset_max)) |
| 3097 |
module.add(VAddU32(dst=vgpr(groVgpr), src0=vgpr(groVgpr), src1=sgpr(tmpSgpr), comment="shift for UseInstOffsetForGRO")) |
| 3098 |
|
| 3099 |
ldsInc = (self.states.kernel["WavefrontSize"] if kernel["WaveSeparateGlobalRead%c"%tc] else kernel["NumThreads"]) * kernel["GlobalReadVectorWidth%c"%tc] * tP["bpeGR"] |
| 3100 |
if kernel["LdsBlockSizePerPad%s"%tc] != 0: |
| 3101 |
ldsInc += (ldsInc // kernel["LdsBlockSizePerPad%s"%tc]) * kernel["LdsPad%s"%tc] * tP["bpeGR"] |
| 3102 |
else: |
| 3103 |
padInterval = (self.states.kernel["WavefrontSize"] if kernel["WaveSeparateGlobalRead%c"%tc] else kernel["NumThreads"]) * self.states.bpr |
| 3104 |
ldsInc += (ldsInc // padInterval) * kernel["LdsPad%s"%tc] * tP["bpeGR"] |
| 3105 |
|
| 3106 |
|
| 3107 |
|
| 3108 |
|
| 3109 |
|
| 3110 |
ldsInc = (ldsInc * graIdx) % self.buff_load_inst_offset_max |
| 3111 |
if (ldsInc != 0): |
| 3112 |
module.add(SMovB32(dst=sgpr(tmpSgpr), src=ldsInc)) |
| 3113 |
module.add(VSubU32(dst=vgpr(groVgpr), src0=vgpr(groVgpr), src1=sgpr(tmpSgpr), comment="sub offset for buffer_load instoffset")) |
| 3114 |
|
| 3115 |
def computeScalarGroImpl(scalarGro): |
| 3116 |
|
| 3117 |
|
| 3118 |
|
| 3119 |
stride1 = "Stride%s%s"%(tc,self.states.indexChars[tP["idx"]]) |
| 3120 |
if tP["tlu"]: |
| 3121 |
tileStride = kernel[tP["lsc"]] * (para*tVW + sPara*tVS) |
| 3122 |
unrollStride = 0 |
| 3123 |
|
| 3124 |
if (tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc]: |
| 3125 |
unrollStride = perp*uVW + (kernel[tP["lsp"]] * sPerp * kernel["LocalReadVectorWidth"]) |
| 3126 |
else: |
| 3127 |
unrollStride = kernel[tP["lsp"]] * (perp*uVW + sPerp*uVS) |
| 3128 |
|
| 3129 |
unrollSummation = [ i for i in tP["ia"] if i in problemType["IndicesSummation"] ] |
| 3130 |
strideU = "Stride%s%s"%(tc,self.states.indexChars[unrollSummation[-1]]) |
| 3131 |
module.add(SMulI32(dst=sgpr(scalarGro), src0=sgpr(strideU), src1=unrollStride, \ |
| 3132 |
comment="compute offset diff (scaled unrollDim)")) |
| 3133 |
if tileStride: |
| 3134 |
module.add(SAddU32(dst=sgpr(scalarGro), src0=sgpr(scalarGro), src1=tileStride, \ |
| 3135 |
comment="compute offset diff (tileDim)")) |
| 3136 |
else: |
| 3137 |
tileStride = 0 |
| 3138 |
if (tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc] and kernel["VectorWidth%s"%tc] > 1: |
| 3139 |
tileStride = perp*tVW + (kernel[tP["lsp"]] * sPerp * kernel["VectorWidth%s"%tc]) |
| 3140 |
else: |
| 3141 |
tileStride = kernel[tP["lsp"]] * (perp*tVW + sPara*tVS) |
| 3142 |
|
| 3143 |
unrollStride = kernel[tP["lsc"]] * (para*uVW + sPerp*uVS) |
| 3144 |
strideF = "Stride%s%s"%(tc,self.states.indexChars[tP['tileIdx']]) |
| 3145 |
module.add(SMulI32(dst=sgpr(scalarGro), src0=sgpr(strideF), src1=tileStride, \ |
| 3146 |
comment="compute offset diff (scaled tileDim)")) |
| 3147 |
if unrollStride: |
| 3148 |
module.add(SAddU32(dst=sgpr(scalarGro), src0=sgpr(scalarGro), src1=unrollStride, \ |
| 3149 |
comment="compute offset diff (unrollDim)")) |
| 3150 |
|
| 3151 |
|
| 3152 |
|
| 3153 |
if log2(tP["bpeGR"]) > 0: |
| 3154 |
module.add(SLShiftLeftB32( |
| 3155 |
dst=sgpr(scalarGro), \ |
| 3156 |
src=sgpr(scalarGro), \ |
| 3157 |
shiftHex=hex(log2(tP["bpeGR"])), \ |
| 3158 |
comment="scalar offset *= bytes/element")) |
| 3159 |
else: |
| 3160 |
module.addCommentAlign("scalar offset *= bytes/element (multiplier is 1, do nothing)") |
| 3161 |
|
| 3162 |
if kernel["DirectToLds%s"%tc] and kernel["UseInstOffsetForGRO"]: |
| 3163 |
|
| 3164 |
module.add(SAddU32(dst=sgpr(scalarGro), src0=sgpr(scalarGro), src1=self.buff_load_inst_offset_max, comment="shift for UseInstOffsetForGRO")) |
| 3165 |
|
| 3166 |
ldsInc = (self.states.kernel["WavefrontSize"] if kernel["WaveSeparateGlobalRead%c"%tc] else kernel["NumThreads"]) * kernel["GlobalReadVectorWidth%c"%tc] * tP["bpeGR"] |
| 3167 |
if kernel["LdsBlockSizePerPad%s"%tc] != 0: |
| 3168 |
ldsInc += (ldsInc // kernel["LdsBlockSizePerPad%s"%tc]) * kernel["LdsPad%s"%tc] * tP["bpeGR"] |
| 3169 |
else: |
| 3170 |
padInterval = (self.states.kernel["WavefrontSize"] if kernel["WaveSeparateGlobalRead%c"%tc] else kernel["NumThreads"]) * self.states.bpr |
| 3171 |
ldsInc += (ldsInc // padInterval) * kernel["LdsPad%s"%tc] * tP["bpeGR"] |
| 3172 |
|
| 3173 |
|
| 3174 |
|
| 3175 |
|
| 3176 |
|
| 3177 |
ldsInc = (ldsInc * graIdx) % self.buff_load_inst_offset_max |
| 3178 |
if (ldsInc != 0): |
| 3179 |
module.add(SSubU32(dst=sgpr(scalarGro), src0=sgpr(scalarGro), src1=ldsInc, comment="sub offset for buffer_load instoffset")) |
| 3180 |
|
| 3181 |
if self.states.checkGRO: |
| 3182 |
|
| 3183 |
print(tc, "tileStride=", tileStride, "unrollStride=", unrollStride, \ |
| 3184 |
"stride=%s"%(stride1)) |
| 3185 |
|
| 3186 |
module.add(self.getVectorDiffAssert(vgpr("GlobalReadOffset%s+%u"%(tc,0)), \ |
| 3187 |
vgpr("GlobalReadOffset%s+%u"%(tc,graIdx)), \ |
| 3188 |
sgpr(scalarGro))) |
| 3189 |
|
| 3190 |
needFirstSgprOffset = kernel["DirectToLds%s"%tc] and kernel["UseInstOffsetForGRO"] |
| 3191 |
|
| 3192 |
if (kernel["_UseSgprForGRO"] or self.states.checkGRO) and (needFirstSgprOffset or graIdx > 0): |
| 3193 |
|
| 3194 |
if kernel["_UseSgprForGRO"]: |
| 3195 |
tmpIdx = graIdx if needFirstSgprOffset else graIdx-1 |
| 3196 |
scalarGro = "ScalarGlobalReadOffset%s+%u"%(tc, tmpIdx) |
| 3197 |
computeScalarGroImpl(scalarGro) |
| 3198 |
else: |
| 3199 |
|
| 3200 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 3201 |
scalarGro = tmpSgprInfo.idx |
| 3202 |
computeScalarGroImpl(scalarGro) |
| 3203 |
|
| 3204 |
graIdx += self.states.rpgo if kernel["BufferLoad"] else self.states.rpga |
| 3205 |
|
| 3206 |
return module, graIdx |
| 3207 |
|
| 3208 |
def computeMetaDataSrd(self, kernel, tP, tc, indices): |
| 3209 |
module = Module("computeMetaDataSrd") |
| 3210 |
|
| 3211 |
wroteTileStart = True |
| 3212 |
with self.allocTmpSgpr(2 + 2 + 2) as tmpSgprInfo: |
| 3213 |
stmp = tmpSgprInfo.idx |
| 3214 |
gsuoffset = stmp |
| 3215 |
tensorSize = stmp |
| 3216 |
tileStart = stmp+2 |
| 3217 |
blockOffset = stmp+4 |
| 3218 |
actualBatchSize = stmp+5 |
| 3219 |
actualBatchIndex = stmp+5 |
| 3220 |
|
| 3221 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(tileStart+0), sgpr(tileStart+1), sgpr(tP["wg"]), kernel[tP["mt"]], comment="WorkGroup[01] * MT")) |
| 3222 |
|
| 3223 |
unrollSummation = [ i for i in tP["ia"] if i in kernel["ProblemType"]["IndicesSummation"] ] |
| 3224 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(tileStart), sgpr(tileStart+1), sgpr(tileStart+0), self.sizeRef(unrollSummation[-1]), \ |
| 3225 |
comment="scaled tile-offset by Summation size")) |
| 3226 |
|
| 3227 |
depthU = kernel["DepthU"] |
| 3228 |
gsucLabel = Label(label=self.labels.getNameInc("GSUC_M"), comment="") |
| 3229 |
gsucLabelEnd = Label(label=self.labels.getNameInc("GSUC_M_End"), comment="") |
| 3230 |
module.add(SAndB32(dst=sgpr(tmpSgprInfo.idx), src0=sgpr("GSU"), src1=hex(0x8000), comment="SCC = (GSUC == 1) ?")) |
| 3231 |
gsuOffsetStr = "gsuOffset = DepthU*GSUSumIdx" |
| 3232 |
module.add(SCBranchSCC1(labelName=gsucLabel.getLabelName(), comment="branch if GSUC == 1")) |
| 3233 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(stmp+0), sgpr(stmp+1), depthU, sgpr("GSUSumIdx"), comment=gsuOffsetStr)) |
| 3234 |
module.add(SBranch(gsucLabelEnd.getLabelName())) |
| 3235 |
module.add(gsucLabel) |
| 3236 |
gsuOffsetStr = "gsuOffset = DepthU*accumulatedNumOfLoopCounterL" |
| 3237 |
loopCounterName = self.loopCounterName(kernel, self.states.unrollIdx) |
| 3238 |
module.add(SLShiftRightB32(dst=sgpr(loopCounterName), src=sgpr("SizesSum"), shiftHex=log2(depthU), \ |
| 3239 |
comment="s[%s] = s[sgprSizesSum] / %s"%(loopCounterName, depthU))) |
| 3240 |
module.add(self.calculateLoopNumIterOffsetGsu(kernel, loopCounterName, tmpSgprInfo)) |
| 3241 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(stmp+0), sgpr(stmp+1), sgpr(stmp+0), depthU, comment=gsuOffsetStr)) |
| 3242 |
module.add(gsucLabelEnd) |
| 3243 |
module.add(SAddU32(dst=sgpr(tileStart+0), src0=sgpr(tileStart+0), src1=sgpr(stmp+0), comment="accum GsuOffet term to tilestart")) |
| 3244 |
module.add(SAddCU32(dst=sgpr(tileStart+1), src0=sgpr(tileStart+1), src1=sgpr(stmp+1), comment="accum GsuOffet term to tilestart")) |
| 3245 |
|
| 3246 |
sizeIndex = [ dim for dim in tP["ia"] ] |
| 3247 |
assert(len(sizeIndex) >= 2) |
| 3248 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(blockOffset), sgpr(blockOffset+1), self.sizeRef(sizeIndex[0]), self.sizeRef(sizeIndex[1]), \ |
| 3249 |
comment="calculate metadata tensor size")) |
| 3250 |
for dim in sizeIndex[2:]: |
| 3251 |
module.add(SCmpEQU32(src0=sgpr("Stride%s%s"%(tc,self.states.indexChars[tP['ia'][dim]])), src1=0, comment="broadcast %s?"%tc)) |
| 3252 |
module.add(SCSelectB32(dst=sgpr(actualBatchSize), src0=hex(1) , src1=self.sizeRef(sizeIndex[dim]), comment="set batchSize as 1 for boardcast %s"%tc)) |
| 3253 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(tensorSize), sgpr(tensorSize+1), sgpr(blockOffset), sgpr(actualBatchSize), \ |
| 3254 |
comment="calculate metadata tensor size")) |
| 3255 |
module.add(SCSelectB32(dst=sgpr(actualBatchIndex), src0=0 , src1=sgpr("WorkGroup2"), comment="set batchIndex as 0 for boardcast %s"%tc)) |
| 3256 |
|
| 3257 |
if self.states.use64bShadowLimit: |
| 3258 |
limitTmp0 = "ShadowLimitMetadata" |
| 3259 |
limitTmp1 = "ShadowLimitMetadata+1" |
| 3260 |
else: |
| 3261 |
limitTmp0 = stmp+0 |
| 3262 |
limitTmp1 = stmp+1 |
| 3263 |
|
| 3264 |
module.add(SSubU32(sgpr(limitTmp0), sgpr(tensorSize), sgpr(tileStart+0), "sub tileStart")) |
| 3265 |
module.add(SSubBU32(sgpr(limitTmp1), sgpr(tensorSize+1), sgpr(tileStart+1), "sub tileStart")) |
| 3266 |
|
| 3267 |
if self.states.use64bShadowLimit: |
| 3268 |
module.add(SLShiftRightB64(sgpr(limitTmp0,2), hex(log2(8)), sgpr(limitTmp0,2), "Set limit to use bytes")) |
| 3269 |
module.add(SCmpEQU32(sgpr(limitTmp1), 0, "are we within 2^32?")) |
| 3270 |
module.add(SCSelectB32(sgpr("SrdMetadata+2"), sgpr(limitTmp0), "BufferLimit", "Move shadow to real if we are within 2^32")) |
| 3271 |
else: |
| 3272 |
module.add(SLShiftRightB32(sgpr("SrdMetadata+2"), hex(log2(8)), sgpr(limitTmp0), "Set limit to use bytes")) |
| 3273 |
|
| 3274 |
numDim = len(indices) |
| 3275 |
wg=2 |
| 3276 |
for i in range(0, numDim): |
| 3277 |
idx = indices[i] |
| 3278 |
if not ( idx == kernel["ProblemType"]["Index0"] \ |
| 3279 |
or idx == kernel["ProblemType"]["Index1"] \ |
| 3280 |
or idx in kernel["ProblemType"]["IndicesSummation"] \ |
| 3281 |
or isPackedIndex(kernel, idx)): |
| 3282 |
assert(wg==2) |
| 3283 |
if not wroteTileStart: |
| 3284 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(tileStart+0), sgpr(tileStart+1), sgpr(blockOffset), sgpr("WorkGroup2"), comment="block offset*WG")) |
| 3285 |
wroteTileStart = True |
| 3286 |
else: |
| 3287 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(stmp+0), sgpr(stmp+1), sgpr(blockOffset), sgpr(actualBatchIndex), comment="block offset*WG")) |
| 3288 |
module.add(SAddU32(sgpr(tileStart+0), sgpr(tileStart+0), sgpr(stmp+0), "accum wg term to tilestart")) |
| 3289 |
module.add(SAddCU32(sgpr(tileStart+1), sgpr(tileStart+1), sgpr(stmp+1), "accum wg term to tilestart")) |
| 3290 |
|
| 3291 |
if wroteTileStart: |
| 3292 |
module.add(SLShiftRightB64(sgpr(tileStart,2), hex(log2(8)), sgpr(tileStart,2), "Set limit to use bytes")) |
| 3293 |
module.add(SAddU32(sgpr("SrdMetadata+0"), sgpr("AddressMetadata+0"), sgpr(tileStart+0), "SRD base = Address+ tileStart0")) |
| 3294 |
module.add(SAddCU32(sgpr("SrdMetadata+1"), sgpr("AddressMetadata+1"), sgpr(tileStart+1), "SRD base = Address+ tileStart1")) |
| 3295 |
else: |
| 3296 |
module.add(SMovB64(sgpr("SrdMetadata+0", 2), sgpr("AddressMetadata+0", 2), "init SRD base address" )) |
| 3297 |
|
| 3298 |
module.add(SMovB32(sgpr("SrdMetadata+3"), "Srd127_96", "Set bits 127_96 in SRD")) |
| 3299 |
return module |
| 3300 |
|
| 3301 |
|
| 3302 |
|
| 3303 |
|
| 3304 |
|
| 3305 |
|
| 3306 |
|
| 3307 |
|
| 3308 |
def computeLoadSrd(self, kernel, tP, tc, indices, bpe): |
| 3309 |
module = Module("computeLoadSrd") |
| 3310 |
with self.allocTmpSgpr(2 + 2 + (0 if self.states.use64bShadowLimit else 2)) as tmpSgprInfo: |
| 3311 |
stmp = tmpSgprInfo.idx |
| 3312 |
tileStart = stmp+2 |
| 3313 |
if self.states.use64bShadowLimit: |
| 3314 |
tensor2dSize0 = "ShadowLimit%s+0"%tc |
| 3315 |
tensor2dSize1 = "ShadowLimit%s+1"%tc |
| 3316 |
else: |
| 3317 |
tensor2dSize0 = stmp+4 |
| 3318 |
tensor2dSize1 = stmp+5 |
| 3319 |
wroteTileStart = False |
| 3320 |
|
| 3321 |
|
| 3322 |
|
| 3323 |
if self.states.groOffsetInMacroTile: |
| 3324 |
|
| 3325 |
assert(len(kernel["PackedC0IndicesX"])==1) |
| 3326 |
assert(len(kernel["PackedC1IndicesX"])==1) |
| 3327 |
|
| 3328 |
wroteTileStart = True |
| 3329 |
|
| 3330 |
|
| 3331 |
|
| 3332 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(tileStart+0), sgpr(tileStart+1), sgpr(tP["wg"]), kernel[tP["mt"]], comment="WorkGroup[01] * MT")) |
| 3333 |
strideF = self.strideRef(tc, tP['tileIdx']) |
| 3334 |
if not self.isConstUnitStride(strideF): |
| 3335 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(tileStart), sgpr(tileStart+1), sgpr(tileStart+0), \ |
| 3336 |
strideF, comment="tlu=0, scaled tile-offset by stride")) |
| 3337 |
|
| 3338 |
skComponent = Component.StreamK.find(self) |
| 3339 |
module.add(skComponent.computeLoadSrd(self, kernel, tc, stmp)) |
| 3340 |
|
| 3341 |
gsuComponent = Component.GSU.find(self) |
| 3342 |
module.add(gsuComponent.computeLoadSrd(self, kernel, tP, stmp, tileStart)) |
| 3343 |
|
| 3344 |
|
| 3345 |
|
| 3346 |
|
| 3347 |
|
| 3348 |
|
| 3349 |
|
| 3350 |
|
| 3351 |
prePad = self.states.srdShiftLeft[tc] * tP["bpeGR"] |
| 3352 |
|
| 3353 |
if not wroteTileStart: |
| 3354 |
module.add(SMovB64(dst=sgpr(tileStart, 2), src=0, comment="set default tileStart")) |
| 3355 |
|
| 3356 |
|
| 3357 |
if self.states.use64bShadowLimit or ((not self.states.use64bShadowLimit) and tensor2dSize0 % 2 == 0): |
| 3358 |
module.add(SMovB64(dst=sgpr(tensor2dSize0, 2), src=0x1, comment="Init tensor size")) |
| 3359 |
else: |
| 3360 |
module.add(SMovB32(dst=sgpr(tensor2dSize0), src=0x1, comment="Init tensor size")) |
| 3361 |
module.add(SMovB32(dst=sgpr(tensor2dSize1), src=0x0, comment="init tensor size")) |
| 3362 |
|
| 3363 |
|
| 3364 |
numDim = len(indices) |
| 3365 |
for i in range(0, numDim): |
| 3366 |
idx = indices[i] |
| 3367 |
if idx == kernel["ProblemType"]["Index0"] \ |
| 3368 |
or idx == kernel["ProblemType"]["Index1"] \ |
| 3369 |
or idx in kernel["ProblemType"]["IndicesSummation"] \ |
| 3370 |
or isPackedIndex(kernel, idx): |
| 3371 |
stride = self.strideRef(tc,idx) |
| 3372 |
size = self.sizeRef(idx) |
| 3373 |
|
| 3374 |
if (idx in kernel["ProblemType"]["IndicesSummation"]) and \ |
| 3375 |
((tP["isA"] and kernel["ProblemType"]["Sparse"] == 1) or \ |
| 3376 |
(tP["isB"] and kernel["ProblemType"]["Sparse"] == 2)) : |
| 3377 |
module.add(SLShiftRightB32(dst=sgpr(stmp), src=size, shiftHex=0x1, comment="(size/2)")) |
| 3378 |
module.add(SSubU32(dst=sgpr(stmp), src0=sgpr(stmp), src1=0x1, comment="(size/2-1)")) |
| 3379 |
else: |
| 3380 |
if tP["isSwizzled"]: |
| 3381 |
if idx in kernel["ProblemType"]["IndicesSummation"]: |
| 3382 |
module.addModuleAsFlatItems(self.alignTo(stmp, "SizeL", tP["swizzleK"])) |
| 3383 |
module.add(SSubU32(dst=sgpr(stmp), src0=sgpr(stmp), src1=1, comment="SWZ-%s align: (sizeL-1)"%tc)) |
| 3384 |
elif tP["isA"] and idx == kernel["ProblemType"]["Index0"]: |
| 3385 |
module.addModuleAsFlatItems(self.alignTo(stmp, "SizeI", 16)) |
| 3386 |
module.add(SSubU32(dst=sgpr(stmp), src0=sgpr(stmp), src1=1, comment="SWZ-%s align: (sizeM-1)"%tc)) |
| 3387 |
elif tP["isB"] and idx == kernel["ProblemType"]["Index1"]: |
| 3388 |
module.addModuleAsFlatItems(self.alignTo(stmp, "SizeJ", 16)) |
| 3389 |
module.add(SSubU32(dst=sgpr(stmp), src0=sgpr(stmp), src1=1, comment="SWZ-%s align: (sizeN-1)"%tc)) |
| 3390 |
else: |
| 3391 |
module.add(SSubU32(dst=sgpr(stmp), src0=size, src1=0x1, comment="(size-1)")) |
| 3392 |
else: |
| 3393 |
module.add(SSubU32(dst=sgpr(stmp), src0=size, src1=0x1, comment="(size-1)")) |
| 3394 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(stmp), sgpr(stmp+1), stride, \ |
| 3395 |
sgpr(stmp), comment="stride x (size-1)")) |
| 3396 |
module.add(SAddU32(dst=sgpr(tensor2dSize0), src0=sgpr(tensor2dSize0), src1=sgpr(stmp+0), comment="sum tensor size")) |
| 3397 |
module.add(SAddCU32(dst=sgpr(tensor2dSize1), src0=sgpr(tensor2dSize1), src1=sgpr(stmp+1), comment="sum tensor size")) |
| 3398 |
|
| 3399 |
if self.states.use64bShadowLimit: |
| 3400 |
limitTmp0 = "ShadowLimit%s+0"%tc |
| 3401 |
limitTmp1 = "ShadowLimit%s+1"%tc |
| 3402 |
else: |
| 3403 |
limitTmp0 = stmp+0 |
| 3404 |
limitTmp1 = stmp+1 |
| 3405 |
|
| 3406 |
module.add(SSubU32(dst=sgpr(limitTmp0), src0=sgpr(tensor2dSize0), src1=sgpr(tileStart+0), comment="sub tileStart")) |
| 3407 |
module.add(SSubBU32(dst=sgpr(limitTmp1), src0=sgpr(tensor2dSize1), src1=sgpr(tileStart+1), comment="sub tileStart")) |
| 3408 |
|
| 3409 |
if self.states.use64bShadowLimit: |
| 3410 |
|
| 3411 |
|
| 3412 |
|
| 3413 |
|
| 3414 |
|
| 3415 |
if log2(tP["bpeGR"]) > 0: |
| 3416 |
module.add(SLShiftLeftB64(dst=sgpr("ShadowLimit%s"%tc,2), src=sgpr("ShadowLimit%s"%tc,2), \ |
| 3417 |
shiftHex=hex(log2(tP["bpeGR"])), comment="Set limit to use bytes")) |
| 3418 |
else: |
| 3419 |
module.addCommentAlign("Set limit to use bytes (byte is 1, do nothing)") |
| 3420 |
if prePad: |
| 3421 |
module.add(SAddU32(dst=sgpr("ShadowLimit%s+0"%tc), src0=sgpr("ShadowLimit%s+0"%tc), src1=prePad, comment="extend limit for pre-pad")) |
| 3422 |
module.add(SAddCU32(dst=sgpr("ShadowLimit%s+1"%tc), src0=sgpr("ShadowLimit%s+1"%tc), src1=0, comment="extend limit for pre-pad")) |
| 3423 |
|
| 3424 |
if kernel["DirectToLds%s"%tc] and kernel["UseInstOffsetForGRO"]: |
| 3425 |
module.add(SAddU32(dst=sgpr("ShadowLimit%s+0"%tc), src0=sgpr("ShadowLimit%s+0"%tc), src1=self.buff_load_inst_offset_max, comment="extend limit for directToLDS instruction offset")) |
| 3426 |
module.add(SAddCU32(dst=sgpr("ShadowLimit%s+1"%tc), src0=sgpr("ShadowLimit%s+1"%tc), src1=0, comment="extend limit for directToLDS instruction offset")) |
| 3427 |
|
| 3428 |
module.add(SCmpEQU32(src0=sgpr("ShadowLimit%s+1"%tc), src1=0, comment="are we within 2^32?")) |
| 3429 |
module.add(SCSelectB32(dst=sgpr("Srd%s+2"%tc), src0=sgpr("ShadowLimit%s+0"%tc), src1="BufferLimit", comment="Move shadow to real if we are within 2^32")) |
| 3430 |
else: |
| 3431 |
|
| 3432 |
if log2(tP["bpeGR"]) > 0: |
| 3433 |
module.add(SLShiftLeftB32(dst=sgpr("Srd%s+2"%tc), src=sgpr(stmp+0), shiftHex=hex(log2(tP["bpeGR"])), comment="Set limit to use bytes")) |
| 3434 |
else: |
| 3435 |
module.addCommentAlign("Set limit to use bytes (byte is 1, do nothing)") |
| 3436 |
module.add(SAddU32(dst=sgpr("Srd%s+2"%tc), src0=sgpr("Srd%s+2"%tc), src1=prePad, comment="extend limit for pre-pad")) |
| 3437 |
|
| 3438 |
|
| 3439 |
if kernel["ProblemType"]["StridedBatched"]: |
| 3440 |
wg=2 |
| 3441 |
for i in range(1, numDim): |
| 3442 |
idx = indices[i] |
| 3443 |
if idx == kernel["ProblemType"]["Index0"] \ |
| 3444 |
or idx == kernel["ProblemType"]["Index1"] \ |
| 3445 |
or idx in kernel["ProblemType"]["IndicesSummation"] \ |
| 3446 |
or isPackedIndex(kernel, idx): |
| 3447 |
continue |
| 3448 |
else: |
| 3449 |
assert(wg==2) |
| 3450 |
stride = "Stride%s%s"%(tc,self.states.indexChars[tP['ia'][i]]) |
| 3451 |
if not wroteTileStart: |
| 3452 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(tileStart+0), sgpr(tileStart+1), sgpr(stride), sgpr("WorkGroup2"), comment="Stride*WG")) |
| 3453 |
wroteTileStart = True |
| 3454 |
else: |
| 3455 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(stmp+0), sgpr(stmp+1), sgpr(stride), sgpr("WorkGroup2"), comment="Stride*WG")) |
| 3456 |
module.add(SAddU32(dst=sgpr(tileStart+0), src0=sgpr(tileStart+0), src1=sgpr(stmp+0), comment="accum wg term to tilestart")) |
| 3457 |
module.add(SAddCU32(dst=sgpr(tileStart+1), src0=sgpr(tileStart+1), src1=sgpr(stmp+1), comment="accum wg term to tilestart")) |
| 3458 |
wg+=1 |
| 3459 |
|
| 3460 |
|
| 3461 |
if wroteTileStart: |
| 3462 |
module.add(scalarStaticMultiply64(sgpr(tileStart,2), sgpr(tileStart,2), tP["bpeGR"], None, "tileStart *= BPE")) |
| 3463 |
module.add(SAddU32(dst=sgpr("Srd%s+0"%tc), src0=sgpr("Address%s+0"%tc), src1=sgpr(tileStart+0), comment="SRD base = Address+ tileStart0")) |
| 3464 |
module.add(SAddCU32(dst=sgpr("Srd%s+1"%tc), src0=sgpr("Address%s+1"%tc), src1=sgpr(tileStart+1), comment="SRD base = Address+ tileStart1")) |
| 3465 |
else: |
| 3466 |
module.add(SMovB64(dst=sgpr("Srd%s"%tc, 2), src=sgpr("Address%s"%tc, 2), comment="init SRD base address")) |
| 3467 |
|
| 3468 |
|
| 3469 |
if prePad and self.states.groOffsetInMacroTile == 0: |
| 3470 |
module.add(SSubU32(dst=sgpr("Srd%s+0"%tc), src0=sgpr("Srd%s+0"%tc), src1=prePad, comment="pre-pad to make room for possible pointer shift")) |
| 3471 |
module.add(SSubBU32(dst=sgpr("Srd%s+1"%tc), src0=sgpr("Srd%s+1"%tc), src1=0, comment="pre-pad to make room for possible pointer shift")) |
| 3472 |
|
| 3473 |
if kernel["DirectToLds%s"%tc] and kernel["UseInstOffsetForGRO"]: |
| 3474 |
module.add(SSubU32(dst=sgpr("Srd%s+0"%tc), src0=sgpr("Srd%s+0"%tc), src1=self.buff_load_inst_offset_max, comment="make room for directToLDS instruction offset")) |
| 3475 |
module.add(SSubBU32(dst=sgpr("Srd%s+1"%tc), src0=sgpr("Srd%s+1"%tc), src1=0, comment="make room for directToLDS instruction offset")) |
| 3476 |
|
| 3477 |
module.add(SMovB32(dst=sgpr("Srd%s+3"%tc), src="Srd127_96", comment="Set bits 127_96 in SRD")) |
| 3478 |
|
| 3479 |
|
| 3480 |
|
| 3481 |
|
| 3482 |
return module |
| 3483 |
|
| 3484 |
|
| 3485 |
|
| 3486 |
|
| 3487 |
def graAddresses(self, kernel, tP): |
| 3488 |
module = Module("graAddresses") |
| 3489 |
tc = tP["tensorChar"] |
| 3490 |
graIdx = 0 |
| 3491 |
|
| 3492 |
if tP["isSwizzled"]: |
| 3493 |
|
| 3494 |
strideName = "Stride%s%s"%(tc,self.states.indexChars[tP["idx"]]) |
| 3495 |
module.addModuleAsFlatItems(self.setTo(strideName, "SizeL")) |
| 3496 |
module.addModuleAsFlatItems(self.alignTo(strideName, strideName, tP["swizzleK"])) |
| 3497 |
|
| 3498 |
if kernel["BufferLoad"]: |
| 3499 |
|
| 3500 |
module.addComment0("max read offset = size[n] * stride[n-1]") |
| 3501 |
module.add(self.computeLoadSrd(kernel, tP, tc, kernel["ProblemType"]["IndexAssignments%s"%tc], tP["bpeGR"])) |
| 3502 |
|
| 3503 |
if kernel["ProblemType"]["Sparse"] and kernel["DirectToVgprSparseMetadata"]: |
| 3504 |
if (kernel["ProblemType"]["Sparse"] == 2 and tP["isB"]) or (kernel["ProblemType"]["Sparse"] == 1 and tP["isA"]): |
| 3505 |
module.add(self.computeMetaDataSrd(kernel, tP, tc, kernel["ProblemType"]["IndexAssignments%s"%tc])) |
| 3506 |
|
| 3507 |
else: |
| 3508 |
tmp = self.vgprPool.checkOut(2, "tmp", self.states.preventVgprOverflowDuringNewTile) |
| 3509 |
|
| 3510 |
skComponent = Component.StreamK.find(self) |
| 3511 |
module.add(skComponent.graAddresses(self, kernel, tc, tmp)) |
| 3512 |
|
| 3513 |
for perp in range(0, tP["nrp"]): |
| 3514 |
for sPerp in range(0, tP["nrpv"]): |
| 3515 |
for para in range(0, tP["nrc"]): |
| 3516 |
for sPara in range(0, tP["nrcv"]//tP["nrcvpi"]): |
| 3517 |
|
| 3518 |
comment = "gRA%s_%u_%u_%u_%u = addr%s+grO%s_%u_%u_%u_%u" \ |
| 3519 |
% (tP["tensorChar"], para, sPara, perp, sPerp, \ |
| 3520 |
tP["tensorChar"], tP["tensorChar"], \ |
| 3521 |
para, sPara, perp, sPerp ) |
| 3522 |
module.add(VAddCOU32( |
| 3523 |
dst=vgpr("GlobalReadAddr%s+%u+0"%(tP["tensorChar"], graIdx)), \ |
| 3524 |
dst1=VCC(), \ |
| 3525 |
src0=vgpr("GlobalReadAddr%s+%u+0"%(tP["tensorChar"], graIdx)), \ |
| 3526 |
src1=vgpr(tmp+0), \ |
| 3527 |
comment=comment+" (lower)")) |
| 3528 |
module.add(VAddCCOU32( |
| 3529 |
dst=vgpr("GlobalReadAddr%s+%u+1"%(tP["tensorChar"], graIdx)), \ |
| 3530 |
dst1=VCC(), \ |
| 3531 |
src0=vgpr("GlobalReadAddr%s+%u+1"%(tP["tensorChar"], graIdx)), \ |
| 3532 |
src1=vgpr(tmp+1), \ |
| 3533 |
src2=VCC(), \ |
| 3534 |
comment=comment+" (upper)")) |
| 3535 |
graIdx += self.states.rpga |
| 3536 |
self.vgprPool.checkIn(tmp) |
| 3537 |
|
| 3538 |
return module |
| 3539 |
|
| 3540 |
|
| 3541 |
|
| 3542 |
|
| 3543 |
|
| 3544 |
def graIncrements(self, kernel, loopIdx, tP): |
| 3545 |
module = Module("graIncrements") |
| 3546 |
tc = tP["tensorChar"] |
| 3547 |
|
| 3548 |
dimIdx = kernel["ProblemType"]["IndicesSummation"][loopIdx] |
| 3549 |
loopChar = self.states.indexChars[dimIdx] |
| 3550 |
|
| 3551 |
stride = self.strideRef(tc, dimIdx) |
| 3552 |
isMirrorIdx = dimIdx in kernel["ProblemType"]["MirrorDims%s"%tc] |
| 3553 |
|
| 3554 |
|
| 3555 |
|
| 3556 |
assert(self.states.unrollIdx == kernel["ProblemType"]["NumIndicesSummation"]-1) |
| 3557 |
if loopIdx==self.states.unrollIdx: |
| 3558 |
gsuComponent = Component.GSU.find(self) |
| 3559 |
module.add(gsuComponent.graIncrements(self, kernel, loopIdx, tP)) |
| 3560 |
else: |
| 3561 |
|
| 3562 |
if self.states.globalReadIncsUseVgpr: |
| 3563 |
printExit("NumIndicesSummation=%u not yet supported in assembly unless globalReadIncsUseVgpr==0" \ |
| 3564 |
% kernel["ProblemType"]["NumIndicesSummation"] ) |
| 3565 |
else: |
| 3566 |
graInc = "GlobalReadIncs%s+%u"%(tc, loopIdx) |
| 3567 |
|
| 3568 |
|
| 3569 |
loopIdxPrev = loopIdx + 1 |
| 3570 |
dimIdxPrev = kernel["ProblemType"]["IndicesSummation"][loopIdxPrev] |
| 3571 |
loopCharPrev = self.states.indexChars[dimIdxPrev] |
| 3572 |
stridePrev = self.strideRef(tc, dimIdxPrev) |
| 3573 |
isMirrorIdxPrev = dimIdxPrev in kernel["ProblemType"]["MirrorDims%s"%tc] |
| 3574 |
|
| 3575 |
module.addComment1("compute globalReadInc for higher-level loop") |
| 3576 |
|
| 3577 |
with self.allocTmpSgpr(3) as tmpSgprInfo: |
| 3578 |
tmpSgpr = tmpSgprInfo.idx |
| 3579 |
|
| 3580 |
if loopIdxPrev==self.states.unrollIdx: |
| 3581 |
loopCounterName= self.loopCounterName(kernel, self.states.unrollIdx) |
| 3582 |
if tP["isA"]: |
| 3583 |
quotient = loopCounterName |
| 3584 |
dividend = "SizesSum+%u"%self.states.unrollIdx |
| 3585 |
divisor = kernel["DepthU"] |
| 3586 |
if kernel["NoTailLoop"] and kernel["AssertSummationElementMultiple"] % kernel["DepthU"] != 0: |
| 3587 |
|
| 3588 |
module.add(SAddI32(dst=sgpr(quotient), src0=(divisor - 1), src1=sgpr(dividend), \ |
| 3589 |
comment="round up SizeSum / DepthU" )) |
| 3590 |
module.add(scalarStaticDivideAndRemainder(quotient, None, quotient, \ |
| 3591 |
divisor, tmpSgprInfo, 0)) |
| 3592 |
else: |
| 3593 |
module.add(scalarStaticDivideAndRemainder(quotient, None, dividend, \ |
| 3594 |
divisor, tmpSgprInfo, 0)) |
| 3595 |
|
| 3596 |
gsuComponent = Component.GSU.find(self) |
| 3597 |
if kernel["GlobalSplitU"] > 1 or kernel["GlobalSplitU"] == -1: |
| 3598 |
module.add(gsuComponent.calculateLoopNumIterGsu(self, kernel, loopCounterName, tmpSgprInfo)) |
| 3599 |
module.add(gsuComponent.graIncrementsRestore(self, kernel, loopCounterName)) |
| 3600 |
|
| 3601 |
module.add(SMulI32(dst=sgpr(graInc), src0=stridePrev, src1=sgpr(loopCounterName), \ |
| 3602 |
comment="tmp <- stride%s%s * myWgUnrollIters" %(tc, loopCharPrev))) |
| 3603 |
else: |
| 3604 |
module.add(SMulI32(dst=sgpr(graInc), src0=stridePrev, src1=self.sizeRef(dimIdxPrev), \ |
| 3605 |
comment="tmp <- stride%s%s * size%s%s" %(tc, loopCharPrev, tc, loopCharPrev))) |
| 3606 |
|
| 3607 |
|
| 3608 |
|
| 3609 |
if isMirrorIdx and isMirrorIdxPrev: |
| 3610 |
module.add(SSubI32(dst=sgpr(graInc), \ |
| 3611 |
src0=sgpr(graInc), \ |
| 3612 |
src1=stride, \ |
| 3613 |
comment="incr%s%s = <prev-incs> - stride%s%s"%(tc, loopChar, tc, loopChar) )) |
| 3614 |
elif isMirrorIdx: |
| 3615 |
module.add(SAddI32(dst=sgpr(graInc), \ |
| 3616 |
src0=stride, \ |
| 3617 |
src1=sgpr(graInc), \ |
| 3618 |
comment="incr%s%s = stride%s%s + <prev-incs>"%(tc, loopChar, tc, loopChar) )) |
| 3619 |
module.add(SSubI32(dst=sgpr(graInc), \ |
| 3620 |
src0=0, \ |
| 3621 |
src1=sgpr(graInc), \ |
| 3622 |
comment="incr%s%s = - (stride%s%s + <prev-incs>)"%(tc, loopChar, tc, loopChar) )) |
| 3623 |
elif isMirrorIdxPrev: |
| 3624 |
module.add(SAddI32(dst=sgpr(graInc), \ |
| 3625 |
src0=stride, \ |
| 3626 |
src1=sgpr(graInc), \ |
| 3627 |
comment="incr%s%s = stride%s%s + <prev-incs>"%(tc, loopChar, tc, loopChar) )) |
| 3628 |
else: |
| 3629 |
module.add(SSubI32(dst=sgpr(graInc), \ |
| 3630 |
src0=stride, \ |
| 3631 |
src1=sgpr(graInc), \ |
| 3632 |
comment="incr%s%s = stride%s%s - <prev-incs>"%(tc, loopChar, tc, loopChar) )) |
| 3633 |
|
| 3634 |
module.add(SLShiftLeftB32( |
| 3635 |
dst=sgpr(graInc), \ |
| 3636 |
src=sgpr(graInc), \ |
| 3637 |
shiftHex="BpeGR%sLog2"%tc, |
| 3638 |
comment="<- scale by bpeDS")) |
| 3639 |
|
| 3640 |
return Module("graIncrements (Empty)") if self.dontAppendCode else module |
| 3641 |
|
| 3642 |
|
| 3643 |
|
| 3644 |
|
| 3645 |
def lwaTileAssignment(self, kernel, tP): |
| 3646 |
module = Module("lwaTileAssignment") |
| 3647 |
tc = tP["tensorChar"] |
| 3648 |
|
| 3649 |
divisorName = tP["lvc"] |
| 3650 |
divisor = kernel[divisorName] |
| 3651 |
|
| 3652 |
|
| 3653 |
isDTVAB = (tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc] |
| 3654 |
isTr = (tc == "A" or tc == "B") and kernel["enableGLTr%s"%tc] |
| 3655 |
isSwizzledOrTr = tP["isSwizzled"] or isTr |
| 3656 |
swizzledOrTrName = "" |
| 3657 |
if tP["isSwizzled"]: |
| 3658 |
swizzledOrTrName = "SwizzleTensor%s" % tc |
| 3659 |
elif isTr: |
| 3660 |
swizzledOrTrName = "GLTr%s"%tc |
| 3661 |
|
| 3662 |
|
| 3663 |
if (tP["tlu"] or isDTVAB) and (not isSwizzledOrTr): |
| 3664 |
rReg = self.vgprPool.checkOut(1, "lwaTA rReg0", self.states.preventVgprOverflowDuringNewTile) |
| 3665 |
qReg = self.vgprPool.checkOut(1, "lwaTA qReg0", self.states.preventVgprOverflowDuringNewTile) |
| 3666 |
tReg = rReg |
| 3667 |
uReg = qReg |
| 3668 |
tOpStr = "%" |
| 3669 |
uOpStr = "/" |
| 3670 |
else: |
| 3671 |
qReg = self.vgprPool.checkOut(1, 'lwaTA qReg1', self.states.preventVgprOverflowDuringNewTile) |
| 3672 |
rReg = self.vgprPool.checkOut(1, 'lwaTA rReg1', self.states.preventVgprOverflowDuringNewTile) |
| 3673 |
tReg = qReg |
| 3674 |
uReg = rReg |
| 3675 |
tOpStr = "/" |
| 3676 |
uOpStr = "%" |
| 3677 |
|
| 3678 |
module.addComment0("%s = %u" % (divisorName, kernel[divisorName])) |
| 3679 |
module.addComment0("%s = %s-unroll = serial%s%s" \ |
| 3680 |
% (vgpr(uReg), tc, uOpStr, divisorName) ) |
| 3681 |
|
| 3682 |
tmpVgpr = self.vgprPool.checkOutAligned(2, 2, 'lwaTA vgpr', self.states.preventVgprOverflowDuringNewTile) |
| 3683 |
tmpVgprRes = ContinuousRegister(tmpVgpr, 2) |
| 3684 |
|
| 3685 |
dividendReg = "Serial" |
| 3686 |
|
| 3687 |
if kernel["WaveSeparateGlobalRead%s"%tc]: |
| 3688 |
dividendReg = self.vgprPool.checkOut(1, "idInWave", self.states.preventVgprOverflowDuringNewTile) |
| 3689 |
dummy = self.vgprPool.checkOut(1, "dummy", self.states.preventVgprOverflowDuringNewTile) |
| 3690 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 3691 |
module.add(vectorStaticRemainder(dummy, dividendReg, "Serial", kernel["WavefrontSize"], tmpVgprRes, tmpSgprInfo)) |
| 3692 |
|
| 3693 |
|
| 3694 |
dtvKInterval = 1 |
| 3695 |
|
| 3696 |
|
| 3697 |
if isSwizzledOrTr: |
| 3698 |
module.addComment0("TileAssignment for DirectToVgpr%s and %s" % (tc, swizzledOrTrName)) |
| 3699 |
module.add(vectorStaticDivideAndRemainder(qReg, rReg, dividendReg, kernel["WavefrontSize"], tmpVgprRes)) |
| 3700 |
|
| 3701 |
|
| 3702 |
if tP["isSwizzled"]: |
| 3703 |
tmp = self.sgprPool.checkOut(1) |
| 3704 |
numKr = sgpr(tmp) |
| 3705 |
swzStride = tP["swizzleK"] |
| 3706 |
module.addComment(f"Align to {swzStride}") |
| 3707 |
module.add(SAddU32(numKr, sgpr("SizesSum"), swzStride-1)) |
| 3708 |
module.add(SLShiftRightB32(dst=numKr, shiftHex=hex(log2(swzStride)), src=numKr, comment="%s: numKr = DimK / %s"%(swizzledOrTrName, swzStride))) |
| 3709 |
elif isTr: |
| 3710 |
numKr = kernel["MatrixInstK"] // tP["glvw"] |
| 3711 |
|
| 3712 |
tmp = self.vgprPool.checkOut(2) |
| 3713 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 3714 |
module.add(vectorStaticDivide(tmp, rReg, tP["bpeGR"]*8, tmpVgprRes, comment="%s: %s = %s / %s"%(swizzledOrTrName, vgpr(tmp), vgpr(rReg), tP["bpeGR"]*8))) |
| 3715 |
module.add(vectorStaticMultiply(vgpr(tmp), vgpr(tmp), tP["bpeGR"]*4, tmpSgprInfo, comment="%s: %s = %s * %s"%(swizzledOrTrName, vgpr(tmp), vgpr(tmp), tP["bpeGR"]*4))) |
| 3716 |
module.add(vectorStaticRemainder(tmp+1, rReg, rReg, tP["bpeGR"]*4, tmpVgprRes, tmpSgprInfo, comment="%s: %s = %s %% %s"%(swizzledOrTrName, vgpr(tmp), vgpr(rReg), tP["bpeGR"]*4))) |
| 3717 |
module.add(VAddU32(dst=vgpr(rReg), src0=vgpr(tmp), src1=vgpr(rReg), comment="%s: %s = %s + %s"%(swizzledOrTrName, vgpr(rReg), vgpr(rReg), vgpr(tmp)))) |
| 3718 |
|
| 3719 |
WvG_M = kernel["MIWaveGroup"][0] |
| 3720 |
if tP["isA"]: |
| 3721 |
module.add(VAndB32(dst=vgpr(qReg), src0=hex(WvG_M-1), src1=vgpr(qReg), comment="%s: wave_id (along_M) %%= MIWG[0]"%(swizzledOrTrName))) |
| 3722 |
module.add(VMulU32U24(dst=vgpr(qReg), src0=numKr, src1=vgpr(qReg), comment="%s: wave_id (along_M) *= numKr"%(swizzledOrTrName))) |
| 3723 |
elif tP["isB"]: |
| 3724 |
|
| 3725 |
|
| 3726 |
module.add(VLShiftRightB32(dst=vgpr(qReg), shiftHex=log2(WvG_M), src=vgpr(qReg), comment="%s: wave_id (along_N) /= MIWG[0]"%(swizzledOrTrName))) |
| 3727 |
if kernel["LocalSplitU"] > 1: |
| 3728 |
|
| 3729 |
WvG_N = kernel["MIWaveGroup"][1] |
| 3730 |
module.add(VAndB32(dst=vgpr(qReg), src0=hex(WvG_N-1), src1=vgpr(qReg), comment="%s: LSU Case: wave_id (along_N) %%= MIWG[1]"%(swizzledOrTrName))) |
| 3731 |
module.add(VMulU32U24(dst=vgpr(qReg), src0=numKr, src1=vgpr(qReg), comment="%s: wave_id (along_N) *= numKr"%(swizzledOrTrName))) |
| 3732 |
|
| 3733 |
if tP["isSwizzled"]: |
| 3734 |
self.sgprPool.checkIn(tmp) |
| 3735 |
elif isTr: |
| 3736 |
module.add(VBfeU32(dst=vgpr(tmp), src0=vgpr(dividendReg), src1=tP["bpeGR"]+1, src2=1, comment="%s: offset for the right half of the tile"%(swizzledOrTrName))) |
| 3737 |
module.add(VAddU32(dst=vgpr(qReg), src0=vgpr(tmp), src1=vgpr(qReg), comment="%s: wave_id += offset for the right half of the tile"%(swizzledOrTrName))) |
| 3738 |
self.vgprPool.checkIn(tmp) |
| 3739 |
elif isDTVAB: |
| 3740 |
|
| 3741 |
|
| 3742 |
module.addComment0("TileAssignment for DirectToVgpr%s" % tc) |
| 3743 |
component = Component.LraTileAssignment.find(self) |
| 3744 |
module.add(component.LraTileAssignmentCode(self, kernel, tP, tReg, uReg, tmpVgprRes, dividendReg=dividendReg, isDTVAB=True)) |
| 3745 |
|
| 3746 |
|
| 3747 |
if tP["isA"]: |
| 3748 |
|
| 3749 |
tluOther = kernel["ProblemType"]["TLUB"] |
| 3750 |
if tluOther: |
| 3751 |
lrvwOther = self.states.lrvwTileB |
| 3752 |
else: |
| 3753 |
lrvwOther = self.states.lrvwUnrollB |
| 3754 |
else: |
| 3755 |
|
| 3756 |
tluOther = kernel["ProblemType"]["TLUA"] |
| 3757 |
if tluOther: |
| 3758 |
lrvwOther = self.states.lrvwTileA |
| 3759 |
else: |
| 3760 |
lrvwOther = self.states.lrvwUnrollA |
| 3761 |
if lrvwOther >= 2 and (not tluOther) and tP["tlu"]: |
| 3762 |
|
| 3763 |
dtvKInterval = lrvwOther |
| 3764 |
if tluOther and tP["tlu"]: |
| 3765 |
|
| 3766 |
dtvKInterval = kernel["MIInputPerThread"] |
| 3767 |
module.add(vectorStaticMultiply(vgpr(qReg), vgpr(qReg), dtvKInterval, None)) |
| 3768 |
|
| 3769 |
|
| 3770 |
if kernel["LocalSplitU"] > 1: |
| 3771 |
|
| 3772 |
wave_id = self.vgprPool.checkOut(1) |
| 3773 |
|
| 3774 |
lsu = kernel["LocalSplitU"] |
| 3775 |
du = kernel["DepthU"] |
| 3776 |
lsuStride = du // lsu |
| 3777 |
numWaves = kernel["MIWaveGroup"][0] * kernel["MIWaveGroup"][1] |
| 3778 |
|
| 3779 |
module.add(vectorStaticDivide(wave_id, "Serial", kernel["WavefrontSize"] * numWaves, tmpVgprRes, comment="LSU offset: Get LSU wave_id")) |
| 3780 |
module.add(VMovB32(dst=vgpr(tmpVgpr), src=hex(lsuStride))) |
| 3781 |
module.add(VMulLOU32(dst=vgpr(wave_id), src0=vgpr(tmpVgpr), src1=vgpr(wave_id), \ |
| 3782 |
comment="LSU offset: lsuoffset = wave_id*lsuStride(%u)" % (lsuStride))) |
| 3783 |
module.add(VAddU32(dst=vgpr(qReg), src0=vgpr(wave_id), src1=vgpr(qReg), \ |
| 3784 |
comment="LSU Offset: offset += lsuoffset" )) |
| 3785 |
self.vgprPool.checkIn(wave_id) |
| 3786 |
|
| 3787 |
else: |
| 3788 |
module.add(vectorStaticDivideAndRemainder(qReg, rReg, dividendReg, divisor, tmpVgprRes)) |
| 3789 |
|
| 3790 |
if kernel["WaveSeparateGlobalRead%s"%tc] == 1: |
| 3791 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 3792 |
tmpSgpr = tmpSgprInfo.idx |
| 3793 |
module.add(VReadfirstlaneB32(dst=sgpr(tmpSgpr), src=vgpr("Serial"), comment="WaveIdxWavefrontWidth")) |
| 3794 |
module.add(SLShiftRightB32(dst=sgpr(tmpSgpr), src=sgpr(tmpSgpr), shiftHex=hex(log2(kernel["WavefrontSize"])), comment="WaveId")) |
| 3795 |
module.add(SMulI32(dst=sgpr(tmpSgpr), src0=sgpr(tmpSgpr), src1=(kernel[tP["lsp"]] * tP["nrp"]), \ |
| 3796 |
comment="Each wave loads continuous lsp(%u)*nrp(%u) columns" % (kernel[tP["lsp"]], tP["nrp"]))) |
| 3797 |
module.add(VAddU32(dst=vgpr(qReg), src0=sgpr(tmpSgpr), src1=vgpr(qReg), \ |
| 3798 |
comment="Add back to column index")) |
| 3799 |
self.vgprPool.checkIn(dividendReg) |
| 3800 |
self.vgprPool.checkIn(dummy) |
| 3801 |
elif kernel["WaveSeparateGlobalRead%s"%tc] == 2: |
| 3802 |
module.add(VLShiftRightB32(vgpr(dividendReg), hex(log2(kernel["WavefrontSize"])), vgpr("Serial"), "WaveID")) |
| 3803 |
module.add(VMovB32(vgpr(dummy), kernel["NumLoadsPerpendicular%s"%tc]*kernel["NumThreads"]//kernel["WavefrontSize"], comment="Global Read Wave: add back to cloumn index")) |
| 3804 |
module.add(VMulLOU32(vgpr(qReg), vgpr(dummy), vgpr(qReg), "Global Read Wave: add back to cloumn index")) |
| 3805 |
module.add(VAddU32(vgpr(qReg), vgpr(dividendReg), vgpr(qReg), "Global Read Wave: add back to cloumn index")) |
| 3806 |
self.vgprPool.checkIn(dividendReg) |
| 3807 |
self.vgprPool.checkIn(dummy) |
| 3808 |
|
| 3809 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 3810 |
if tP["glvw"] > 1: |
| 3811 |
if tP["tlu"]: |
| 3812 |
module.addComment0("tile *= glvw") |
| 3813 |
module.add(vectorStaticMultiply(vgpr(tReg), vgpr(tReg), tP["glvw"], tmpSgprInfo)) |
| 3814 |
else: |
| 3815 |
module.addComment0("unroll *= glvw") |
| 3816 |
module.add(vectorStaticMultiply(vgpr(uReg), vgpr(uReg), tP["glvw"], tmpSgprInfo)) |
| 3817 |
|
| 3818 |
|
| 3819 |
uReg2 = self.vgprPool.checkOut(1, "uReg2", self.states.preventVgprOverflowDuringNewTile) |
| 3820 |
module.add(VMovB32(dst=vgpr(uReg2), src=vgpr(uReg), comment="copy for GlobalSplitU")) |
| 3821 |
tP["gpr"]["uReg2"] = uReg2 |
| 3822 |
tP["gpr"]["lwoT"] = tReg |
| 3823 |
tP["gpr"]["uReg"] = uReg |
| 3824 |
self.vgprPool.checkIn(tmpVgpr) |
| 3825 |
|
| 3826 |
|
| 3827 |
if tP["isA"]: |
| 3828 |
self.states.dtvKIntervalA = dtvKInterval |
| 3829 |
elif tP["isB"]: |
| 3830 |
self.states.dtvKIntervalB = dtvKInterval |
| 3831 |
|
| 3832 |
return module |
| 3833 |
|
| 3834 |
|
| 3835 |
|
| 3836 |
|
| 3837 |
def lwaUnrollAssignment(self, kernel, tP): |
| 3838 |
module = Module("lwaUnrollAssignment") |
| 3839 |
uReg = tP["gpr"]["uReg2"] |
| 3840 |
module.addComment0("lwaUnrollAssignment%s = %s" % (tP["tensorChar"], vgpr(uReg))) |
| 3841 |
return module |
| 3842 |
|
| 3843 |
|
| 3844 |
|
| 3845 |
|
| 3846 |
def lwaFirstOffset(self, kernel, tP): |
| 3847 |
module = Module("lwaFirstOffset") |
| 3848 |
tc = tP["tensorChar"] |
| 3849 |
LdsPad = kernel["LdsPad%s"%tc] if kernel["LdsBlockSizePerPad%s"%tc] == 0 else 0 |
| 3850 |
|
| 3851 |
|
| 3852 |
uReg = tP["gpr"]["uReg2"] |
| 3853 |
if kernel["LocalWriteUseSgpr%s"%tc]: |
| 3854 |
destVgpr = self.vgprPool.checkOut(1, "destVgpr", self.states.preventVgprOverflowDuringNewTile) |
| 3855 |
else: |
| 3856 |
destVgpr = "LocalWriteAddr%s"%tc |
| 3857 |
|
| 3858 |
if kernel["UnrollMajorLDS%s" % tc]: |
| 3859 |
lds_stride = kernel["_DepthU%s"%tc] + LdsPad |
| 3860 |
module.add(VMulU32U24(dst=vgpr(destVgpr), src0=hex(lds_stride), src1=vgpr(tP["gpr"]["lwoT"]), \ |
| 3861 |
comment="lw%s%s**(DepthU_Compute + PAD)"%(tc, self.states.unrollChar))) |
| 3862 |
if log2(tP["bpeDS"]) > 0: |
| 3863 |
module.add(VAddLShiftLeftU32(dst=vgpr(destVgpr), src0=vgpr(uReg), src1=vgpr(destVgpr), shiftHex=hex(log2(tP["bpeDS"])), \ |
| 3864 |
comment="lwFO%s = (lw%s%s + lw%s%s*(DepthU+PAD))*bpeDS" % (tc, tc, tc, tc, self.states.unrollChar) )) |
| 3865 |
else: |
| 3866 |
module.add(VAddU32(dst=vgpr(destVgpr), src0=vgpr(uReg), src1=vgpr(destVgpr), \ |
| 3867 |
comment="lwFO%s = (lw%s%s + lw%s%s*(DepthU+PAD))*bpeDS(1)" % (tc, tc, tc, tc, self.states.unrollChar) )) |
| 3868 |
else: |
| 3869 |
lds_stride = kernel["MacroTile%s"%tc] + LdsPad |
| 3870 |
module.add(VMulU32U24(dst=vgpr(destVgpr), src0=hex(lds_stride), src1=vgpr(uReg), \ |
| 3871 |
comment="lw%s%s**(MT%s + PAD)"%(tc, self.states.unrollChar, tc))) |
| 3872 |
if log2(tP["bpeDS"]) > 0: |
| 3873 |
module.add(VAddLShiftLeftU32(dst=vgpr(destVgpr), src0=vgpr(tP["gpr"]["lwoT"]), src1=vgpr(destVgpr), shiftHex=hex(log2(tP["bpeDS"])), \ |
| 3874 |
comment="lwFO%s = (lw%s%s + lw%s%s*(MT%s+PAD))*bpeDS" % (tc, tc, tc, tc, self.states.unrollChar, tP["tileChar"]) )) |
| 3875 |
else: |
| 3876 |
module.add(VAddU32(dst=vgpr(destVgpr), src0=vgpr(tP["gpr"]["lwoT"]), src1=vgpr(destVgpr), \ |
| 3877 |
comment="lwFO%s = (lw%s%s + lw%s%s*(MT%s+PAD))*bpeDS(1)" % (tc, tc, tc, tc, self.states.unrollChar, tP["tileChar"]) )) |
| 3878 |
|
| 3879 |
|
| 3880 |
if kernel["LdsBlockSizePerPad%s"%tc] != 0 and kernel["LdsPad%s"%tc] != 0: |
| 3881 |
tmpVgpr = self.vgprPool.checkOutAligned(2, 2) |
| 3882 |
tmpVgprRes = ContinuousRegister(tmpVgpr, 2) |
| 3883 |
module.add(vectorStaticDivide(tmpVgpr, destVgpr, kernel["LdsBlockSizePerPad%s"%tc], tmpVgprRes, \ |
| 3884 |
"padding %u per block %u" % (kernel["LdsPad%s"%tc] * tP["bpeDS"], kernel["LdsBlockSizePerPad%s"%tc]))) |
| 3885 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 3886 |
module.add(vectorStaticMultiplyAdd(vgpr(destVgpr), vgpr(tmpVgpr), kernel["LdsPad%s"%tc] * tP["bpeDS"], vgpr(destVgpr), tmpSgprInfo, \ |
| 3887 |
"padding %u per block %u" % (kernel["LdsPad%s"%tc] * tP["bpeDS"], kernel["LdsBlockSizePerPad%s"%tc]))) |
| 3888 |
self.vgprPool.checkIn(tmpVgpr) |
| 3889 |
|
| 3890 |
if tP["isB"]: |
| 3891 |
if kernel["LdsOffsetB"] != 0: |
| 3892 |
module.add(VAddCOU32( |
| 3893 |
dst=vgpr(destVgpr), \ |
| 3894 |
dst1=VCC(), \ |
| 3895 |
src0=hex(kernel["LdsOffsetB"]), \ |
| 3896 |
src1=vgpr(destVgpr), \ |
| 3897 |
comment="lwFOB = lwB%s + lwB%s*MT%s + LDS_OFFSET_B=%u" % (tP["tileChar"], \ |
| 3898 |
self.states.unrollChar, tP["tileChar"], kernel["LdsOffsetB"]) )) |
| 3899 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"] and tP["isM"]: |
| 3900 |
if kernel["LdsOffsetMetadata"] != 0: |
| 3901 |
module.add(VAddCOU32( |
| 3902 |
dst=vgpr(destVgpr), \ |
| 3903 |
dst1=VCC(), \ |
| 3904 |
src0=hex(kernel["LdsOffsetMetadata"]), \ |
| 3905 |
src1=vgpr(destVgpr), \ |
| 3906 |
comment="lwFOB = lwB%s + lwB%s*MT%s + LDS_OFFSET_METADATA=%u" % (tP["tileChar"], \ |
| 3907 |
self.states.unrollChar, tP["tileChar"], kernel["LdsOffsetMetadata"]))) |
| 3908 |
|
| 3909 |
numLwa = 0 |
| 3910 |
if tP["isA"]: |
| 3911 |
numLwa = self.states.a.numVgprLocalWriteAddr |
| 3912 |
elif tP["isB"]: |
| 3913 |
numLwa = self.states.b.numVgprLocalWriteAddr |
| 3914 |
elif tP["isM"]: |
| 3915 |
numLwa = self.states.m.numVgprLocalWriteAddr |
| 3916 |
|
| 3917 |
if numLwa > 1: |
| 3918 |
finalVgpr64K = vgpr("LocalWriteAddr%s+1"%tc) |
| 3919 |
module.add(VAddU32(dst=finalVgpr64K, src0=0x10000, src1= vgpr(destVgpr), \ |
| 3920 |
comment="Final Offset Plus 64K" )) |
| 3921 |
if numLwa > 2: |
| 3922 |
finalVgpr128K = vgpr("LocalWriteAddr%s+2"%tc) |
| 3923 |
module.add(VAddU32(dst=finalVgpr128K, src0= 0x20000, src1= vgpr(destVgpr), \ |
| 3924 |
comment="Final Offset Plus 128K" )) |
| 3925 |
|
| 3926 |
|
| 3927 |
numBytesPerElement = kernel["ProblemType"]["DataType"].numBytes() |
| 3928 |
validWIPerLoad = kernel[tP["lsc"]] * kernel[tP["lsp"]]//tP["glvw"] |
| 3929 |
validBytesPerLoad = kernel[tP["lsc"]] * kernel[tP["lsp"]] * numBytesPerElement |
| 3930 |
maxBytesPerLoad = kernel["NumThreads"] * tP["glvw"] * numBytesPerElement |
| 3931 |
|
| 3932 |
if kernel["WaveSeparateGlobalRead%s"%tc] == 1: |
| 3933 |
validBytesPerLoad *= (kernel["NumThreads"] // self.states.kernel["WavefrontSize"]) |
| 3934 |
elif kernel["WaveSeparateGlobalRead%s"%tc] == 2: |
| 3935 |
if kernel["ProblemType"]["TLU%s"%tc]: |
| 3936 |
validBytesPerLoad *= (kernel["DepthU"] // kernel["NumLoadsPerpendicular%s"%tc] // (kernel["NumThreads"] // kernel["WavefrontSize"])) |
| 3937 |
else: |
| 3938 |
validBytesPerLoad *= (kernel["MacroTile%s"%tc] // kernel["NumLoadsPerpendicular%s"%tc] // (kernel["NumThreads"] // kernel["WavefrontSize"])) |
| 3939 |
|
| 3940 |
isDTVAB = ((tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc]) |
| 3941 |
assert (validBytesPerLoad <= maxBytesPerLoad) or isDTVAB |
| 3942 |
assert (kernel[tP["lsc"]] * kernel[tP["lsp"]] % tP["glvw"] == 0) or isDTVAB |
| 3943 |
|
| 3944 |
if validBytesPerLoad != maxBytesPerLoad: |
| 3945 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 3946 |
tmpSgpr = tmpSgprInfo.idx |
| 3947 |
module.add(SMovB32(dst=sgpr(tmpSgpr), src=validWIPerLoad, \ |
| 3948 |
comment="lsc*lsp=%u*%u"%(kernel[tP["lsc"]],kernel[tP["lsp"]] ))) |
| 3949 |
module.add(VCmpLtU32( |
| 3950 |
dst=VCC(), \ |
| 3951 |
src0=vgpr("Serial"), \ |
| 3952 |
src1=sgpr(tmpSgpr), \ |
| 3953 |
comment="fractional: ensure tid < global read tile elements")) |
| 3954 |
tmpVgpr = self.vgprPool.checkOut(1, "tmpVgpr", self.states.preventVgprOverflowDuringNewTile) |
| 3955 |
module.add(VMovB32(dst=vgpr(tmpVgpr), src=hex(self.consts.ldsOOB))) |
| 3956 |
module.add(VCndMaskB32( |
| 3957 |
dst=vgpr(destVgpr), \ |
| 3958 |
src0=vgpr(tmpVgpr), \ |
| 3959 |
src1=vgpr(destVgpr), \ |
| 3960 |
comment="Mask load so out-of-gr-tile bounds returns 0")) |
| 3961 |
self.vgprPool.checkIn(tmpVgpr) |
| 3962 |
|
| 3963 |
if kernel["LocalWriteUseSgpr%s"%tc]: |
| 3964 |
|
| 3965 |
if self.states.archCaps["CrosslaneWait"]: |
| 3966 |
module.add(SNop(waitState=0, comment="1 wait states required before reading vgpr by lane")) |
| 3967 |
module.add(VReadfirstlaneB32( |
| 3968 |
dst=sgpr("LocalWriteAddr%s"%tc), \ |
| 3969 |
src=vgpr(destVgpr), \ |
| 3970 |
comment="Copy lds write address VGPR to SGPR")) |
| 3971 |
self.vgprPool.checkIn(destVgpr) |
| 3972 |
|
| 3973 |
if kernel["StoreSwapAddr"]: |
| 3974 |
if kernel["LocalWriteUseSgpr%s"%tc]: |
| 3975 |
|
| 3976 |
if self.states.archCaps["CrosslaneWait"]: |
| 3977 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 3978 |
module.add(SAddU32(dst=sgpr("Swap%s"%tc), src0=sgpr("LocalWriteAddr%s"%tc), src1=kernel["LdsOffsetA_Blk"], comment="Calculate starting lds addr of second buffer")) |
| 3979 |
module.add(SXorB32(dst=sgpr("Swap%s"%tc), src0=sgpr("Swap%s"%tc), src1=sgpr("LocalWriteAddr%s"%tc), comment="xor both lds buffer offsets to enable swapping")) |
| 3980 |
else: |
| 3981 |
module.add(VAddU32(dst=vgpr("LocalWriteSwapAddr%s"%tc), src0=kernel["LdsOffsetA_Blk"], src1=vgpr("LocalWriteAddr%s"%tc), \ |
| 3982 |
comment="starting lds addr of second buffer" )) |
| 3983 |
module.add(VXorB32(dst=vgpr("LocalWriteSwapAddr%s"%tc), \ |
| 3984 |
src0=vgpr("LocalWriteSwapAddr%s"%tc), \ |
| 3985 |
src1=vgpr("LocalWriteAddr%s"%tc), \ |
| 3986 |
comment="xor both lds offsets to enable swapping")) |
| 3987 |
|
| 3988 |
return Module("lwaUnrollAssignment (Empty)") if self.dontAppendCode or isDTVAB else module |
| 3989 |
|
| 3990 |
|
| 3991 |
|
| 3992 |
|
| 3993 |
def lraTileAssignment(self, kernel, tPA, tPB): |
| 3994 |
module = Module("lraTileAssignment") |
| 3995 |
|
| 3996 |
component = Component.LraTileAssignment.find(self) |
| 3997 |
|
| 3998 |
tP0 = tPA if tPB["tile01Idx"] else tPB |
| 3999 |
tP1 = tPB if tPB["tile01Idx"] else tPA |
| 4000 |
|
| 4001 |
if component: |
| 4002 |
|
| 4003 |
tc = tP0["tensorChar"] |
| 4004 |
if not kernel["DirectToVgpr%s"%tc]: |
| 4005 |
module.add(component(self, kernel, tP0)) |
| 4006 |
|
| 4007 |
tc = tP1["tensorChar"] |
| 4008 |
if not kernel["DirectToVgpr%s"%tc]: |
| 4009 |
module.add(component(self, kernel, tP1)) |
| 4010 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 4011 |
tPM = tPA["tpsMetadata"] if tPA["is_sparse"] else tPB["tpsMetadata"] |
| 4012 |
module.add(component(self, kernel, tPM)) |
| 4013 |
|
| 4014 |
return module |
| 4015 |
|
| 4016 |
|
| 4017 |
|
| 4018 |
|
| 4019 |
def lraFinalOffset(self, kernel, tP): |
| 4020 |
module = Module("lraFinalOffset") |
| 4021 |
|
| 4022 |
tc = tP["tensorChar"] |
| 4023 |
|
| 4024 |
if (tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc]: |
| 4025 |
return Module("lraFinalOffset (Empty)") |
| 4026 |
|
| 4027 |
if kernel["EnableMatrixInstruction"]: |
| 4028 |
|
| 4029 |
wave_id = self.vgprPool.checkOut(1) |
| 4030 |
rReg = self.vgprPool.checkOut(1) |
| 4031 |
tmpVgpr = self.vgprPool.checkOutAligned(2, 2,"tmpVgpr") |
| 4032 |
tmpVgprRes = ContinuousRegister(tmpVgpr, 2) |
| 4033 |
|
| 4034 |
|
| 4035 |
tc = tP["tensorChar"] |
| 4036 |
tile01 = tP["tile01Idx"] |
| 4037 |
LdsPad = kernel["LdsPad%s" % tc] if kernel["LdsBlockSizePerPad%s" % tc] == 0 else 0 |
| 4038 |
mtAddPad = kernel["MacroTile%u" % tile01] + LdsPad |
| 4039 |
umlds = kernel["UnrollMajorLDS%s" % tc] |
| 4040 |
lsu = kernel["LocalSplitU"] |
| 4041 |
du = kernel["DepthU"] |
| 4042 |
lsuStride = du // lsu |
| 4043 |
numWaves = kernel["MIWaveGroup"][0] * kernel["MIWaveGroup"][1] |
| 4044 |
|
| 4045 |
|
| 4046 |
module.add(vectorStaticDivide(wave_id, "Serial", kernel["WavefrontSize"], tmpVgprRes)) |
| 4047 |
module.add(vectorStaticDivide(wave_id, wave_id, numWaves, tmpVgprRes, comment="LSU offset: Get LSU wave_id")) |
| 4048 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 4049 |
tmpSgpr = tmpSgprInfo.idx |
| 4050 |
if umlds == False: |
| 4051 |
module.add(SMovB32(dst=sgpr(tmpSgpr), src=mtAddPad*lsuStride, \ |
| 4052 |
comment="LSU offset: stride = lsuStride(%u)*(MT%u(%u) + PAD%u(%u))" % (lsuStride,tile01, kernel["MacroTile%u" % tile01], tile01, LdsPad))) |
| 4053 |
else: |
| 4054 |
module.add(SMovB32(dst=sgpr(tmpSgpr), src=lsuStride, \ |
| 4055 |
comment="LSU offset: stride = lsuStride(%u) when umlds==True" % (lsuStride))) |
| 4056 |
module.add(VMulLOU32(dst=vgpr(wave_id), src0=sgpr(tmpSgpr), src1=vgpr(wave_id), \ |
| 4057 |
comment="LSU offset: lsuoffset = wave_id*lsuStride*(MT%u+PAD)"%tile01)) |
| 4058 |
|
| 4059 |
|
| 4060 |
finalVgpr = vgpr("LocalReadAddr%s"%tc) |
| 4061 |
if log2(tP["bpeDS"]) > 0: |
| 4062 |
module.add(VAddLShiftLeftU32(dst=finalVgpr, src0=vgpr(wave_id), src1=vgpr(tP["gpr"]["lro"]), shiftHex=hex(log2(tP["bpeDS"])), \ |
| 4063 |
comment="Final Offset: offset = (lro%s+lsuoffset)*bpeDS" % tile01 )) |
| 4064 |
else: |
| 4065 |
module.add(VAddU32(dst=finalVgpr, src0=vgpr(wave_id), src1=vgpr(tP["gpr"]["lro"]), \ |
| 4066 |
comment="Final Offset: offset = (lro%s+lsuoffset)*bpeDS(1)" % tile01 )) |
| 4067 |
|
| 4068 |
|
| 4069 |
if kernel["LdsBlockSizePerPad%s"%tc] != 0 and kernel["LdsPad%s"%tc] !=0: |
| 4070 |
module.add(vectorStaticDivide(rReg, "LocalReadAddr%s"%tc, kernel["LdsBlockSizePerPad%s"%tc], tmpVgprRes, \ |
| 4071 |
"Final Offset: padding %u per block %u" % (kernel["LdsPad%s"%tc] * tP["bpeDS"], kernel["LdsBlockSizePerPad%s"%tc]))) |
| 4072 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 4073 |
module.add(vectorStaticMultiplyAdd(vgpr("LocalReadAddr%s"%tc), vgpr(rReg), kernel["LdsPad%s"%tc] * tP["bpeDS"], vgpr("LocalReadAddr%s"%tc), tmpSgprInfo, \ |
| 4074 |
"Final Offset: padding %u per block %u" % (kernel["LdsPad%s"%tc] * tP["bpeDS"], kernel["LdsBlockSizePerPad%s"%tc]))) |
| 4075 |
|
| 4076 |
|
| 4077 |
self.vgprPool.checkIn(tmpVgpr) |
| 4078 |
self.vgprPool.checkIn(wave_id) |
| 4079 |
self.vgprPool.checkIn(rReg) |
| 4080 |
self.vgprPool.checkIn(tP["gpr"]["lro"]) |
| 4081 |
|
| 4082 |
else: |
| 4083 |
|
| 4084 |
|
| 4085 |
tile01 = tP["tile01Idx"] |
| 4086 |
LdsPad = kernel["LdsPad%s" % tc] if kernel["LdsBlockSizePerPad%s" % tc] == 0 else 0 |
| 4087 |
divisor = kernel["SubGroup0"] * kernel["SubGroup1"] |
| 4088 |
mtAddPad = kernel["MacroTile%u" % tile01] + LdsPad |
| 4089 |
lrvw = self.states.lrvwUnrollA if tc == 'A' else self.states.lrvwUnrollB |
| 4090 |
|
| 4091 |
|
| 4092 |
finalVgpr = vgpr("LocalReadAddr%s"%tc) |
| 4093 |
|
| 4094 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 4095 |
tmpSgpr = tmpSgprInfo.idx |
| 4096 |
if kernel["UseDotInstruction"]: |
| 4097 |
kidx = self.vgprPool.checkOut(1) |
| 4098 |
module.add(VAndB32(dst=vgpr(kidx), src0=(kernel["NumWaveSplitK"]-1), src1=vgpr("Serial"), comment="kidx = Serial % NumWaveSplitK")) |
| 4099 |
module.add(vectorStaticMultiply(vgpr(kidx), vgpr(kidx), lrvw, tmpSgprInfo, \ |
| 4100 |
"*= lrvw")) |
| 4101 |
|
| 4102 |
module.add(VAddLShiftLeftU32(dst=finalVgpr, shiftHex=hex(log2(tP["bpe"])), src0=vgpr(kidx), src1=vgpr(tP["gpr"]["lro"]), \ |
| 4103 |
comment="Final Offset: add padding %u per block %u" % (kernel["LdsPad%s"%tc] * tP["bpeDS"], kernel["LdsBlockSizePerPad%s"%tc]))) |
| 4104 |
self.vgprPool.checkIn(kidx) |
| 4105 |
else: |
| 4106 |
sgid = self.vgprPool.checkOut(1) |
| 4107 |
vtmp = self.vgprPool.checkOut(1) |
| 4108 |
vCont = ContinuousRegister(vtmp, 1) |
| 4109 |
module.add(vectorStaticDivide(sgid, "Serial", divisor, vCont, \ |
| 4110 |
"LSU offset: sgid = Serial / subGroup(%u)" % divisor)) |
| 4111 |
module.add(vectorStaticMultiply(vgpr(sgid), vgpr(sgid), mtAddPad, tmpSgprInfo, \ |
| 4112 |
"LSU offset: lsuoffset = sgid*(MT%u+PAD)"%tile01)) |
| 4113 |
|
| 4114 |
|
| 4115 |
module.add(vectorStaticMultiply(vgpr(tP["gpr"]["lro"]), vgpr(tP["gpr"]["lro"]), kernel["VectorWidthB"], tmpSgprInfo, \ |
| 4116 |
"Final Offset: lr%sOffset * VW" % tc)) |
| 4117 |
module.add(VAddLShiftLeftU32(dst=finalVgpr, shiftHex=hex(log2(tP["bpe"])), src0=vgpr(sgid), src1=vgpr(tP["gpr"]["lro"]), \ |
| 4118 |
comment="Final Offset: add padding %u per block %u" % (kernel["LdsPad%s"%tc] * tP["bpeDS"], kernel["LdsBlockSizePerPad%s"%tc]))) |
| 4119 |
self.vgprPool.checkIn(sgid) |
| 4120 |
self.vgprPool.checkIn(vtmp) |
| 4121 |
|
| 4122 |
|
| 4123 |
self.vgprPool.checkIn(tP["gpr"]["lro"]) |
| 4124 |
|
| 4125 |
|
| 4126 |
if kernel["LdsBlockSizePerPad%s"%tc] != 0 and kernel["LdsPad%s"%tc] !=0: |
| 4127 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 4128 |
tmpSgpr = tmpSgprInfo.idx |
| 4129 |
rReg = self.vgprPool.checkOut(1) |
| 4130 |
module.add(vectorStaticDivide(rReg, "LocalReadAddr%s"%tc, kernel["LdsBlockSizePerPad%s"%tc], tmpSgpr, \ |
| 4131 |
"Final Offset: padding %u per block %u" % (kernel["LdsPad%s"%tc], kernel["LdsBlockSizePerPad%s"%tc]))) |
| 4132 |
module.add(vectorStaticMultiplyAdd(vgpr("LocalReadAddr%s"%tc), vgpr(rReg), kernel["LdsPad%s"%tc] * tP["bpe"], vgpr("LocalReadAddr%s"%tc), tmpSgprInfo, \ |
| 4133 |
"Final Offset: padding %u per block %u" % (kernel["LdsPad%s"%tc] * tP["bpeDS"], kernel["LdsBlockSizePerPad%s"%tc]))) |
| 4134 |
self.vgprPool.checkIn(rReg) |
| 4135 |
|
| 4136 |
return module |
| 4137 |
|
| 4138 |
|
| 4139 |
|
| 4140 |
|
| 4141 |
def lraOffsetConversionForDTLandNLC(self, kernel, tP, offset_val, generateAsm=False, \ |
| 4142 |
finalVgpr=None, tmp1=None, tmp2=None): |
| 4143 |
module = Module("lraOffsetConversionForDTLandNLC") |
| 4144 |
|
| 4145 |
divisorName = tP["lvc"] |
| 4146 |
divisor = kernel[divisorName] |
| 4147 |
width = kernel["WavefrontSize"] if tP["tlu"] else kernel["DepthU"] |
| 4148 |
if divisor < width: |
| 4149 |
|
| 4150 |
lowerScale = tP["nrc"] |
| 4151 |
upperScale = (kernel["WavefrontSize"] // divisor) |
| 4152 |
|
| 4153 |
if lowerScale > 1: |
| 4154 |
tile01 = tP["tile01Idx"] |
| 4155 |
rightShift = int(log2(lowerScale)) |
| 4156 |
leftShift = int(log2(upperScale)) |
| 4157 |
line = kernel["MacroTile%u" % tile01] if tP["tlu"] else kernel["DepthU"] |
| 4158 |
ldsLineSize = line * tP["bpe"] // lowerScale |
| 4159 |
maskBitsLow = (lowerScale - 1) * ldsLineSize |
| 4160 |
maskBitsHigh = (upperScale - 1) * lowerScale * ldsLineSize |
| 4161 |
maskBitsAll = (maskBitsLow | maskBitsHigh) |
| 4162 |
|
| 4163 |
|
| 4164 |
low = offset_val & maskBitsLow |
| 4165 |
high = offset_val & maskBitsHigh |
| 4166 |
low <<= leftShift |
| 4167 |
high >>= rightShift |
| 4168 |
val = low | high |
| 4169 |
offset_val = (offset_val & (~maskBitsAll)) | val |
| 4170 |
|
| 4171 |
|
| 4172 |
if generateAsm: |
| 4173 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 4174 |
tmpSgpr2 = tmpSgprInfo.idx |
| 4175 |
module.add(VAndB32(dst=vgpr(tmp1), src0=hex(maskBitsLow), src1=finalVgpr, \ |
| 4176 |
comment="Offset rotation for DirectToLds + %s > 1"%tP["lsc"])) |
| 4177 |
module.add(VAndB32(dst=vgpr(tmp2), src0=hex(maskBitsHigh), src1=finalVgpr)) |
| 4178 |
module.add(VLShiftLeftB32(dst=vgpr(tmp1), shiftHex=hex(leftShift), src=vgpr(tmp1))) |
| 4179 |
module.add(VLShiftRightB32(dst=vgpr(tmp2), shiftHex=hex(rightShift), src=vgpr(tmp2))) |
| 4180 |
module.add(VOrB32(dst=vgpr(tmp1), src0=vgpr(tmp1), src1=vgpr(tmp2))) |
| 4181 |
module.add(SMovB32(dst=sgpr(tmpSgpr2), src=hex(maskBitsAll))) |
| 4182 |
module.add(VNotB32(dst=vgpr(tmp2), src=sgpr(tmpSgpr2))) |
| 4183 |
module.add(VAndB32(dst=finalVgpr, src0=vgpr(tmp2), src1=finalVgpr)) |
| 4184 |
module.add(VOrB32(dst=finalVgpr, src0=vgpr(tmp1), src1=finalVgpr)) |
| 4185 |
|
| 4186 |
return module, offset_val |
| 4187 |
|
| 4188 |
|
| 4189 |
|
| 4190 |
|
| 4191 |
def lraDeclareAddresses(self, kernel, tP): |
| 4192 |
module = Module("lraDeclareAddresses") |
| 4193 |
if tP["isA"]: |
| 4194 |
module.addComment0("N/A") |
| 4195 |
|
| 4196 |
else: |
| 4197 |
|
| 4198 |
if kernel["LdsOffset%s"%tP["tensorChar"]] == 0 or tP["isB"] and kernel["DirectToVgprB"]: |
| 4199 |
module = Module("lraDeclareAddresses (Empty)") |
| 4200 |
else: |
| 4201 |
module.add(VAddCOU32( |
| 4202 |
dst=vgpr("LocalReadAddr%s+0"%tP["tensorChar"]), \ |
| 4203 |
dst1=VCC(), \ |
| 4204 |
src0=hex(kernel["LdsOffset%s"%tP["tensorChar"]]), \ |
| 4205 |
src1=vgpr("LocalReadAddr%s+0"%tP["tensorChar"]), \ |
| 4206 |
comment=" += LdsOffset%s (lower)"%tP["tensorChar"])) |
| 4207 |
numLra = 0 |
| 4208 |
if tP["isA"]: |
| 4209 |
numLra = self.states.a.numVgprLocalReadAddr |
| 4210 |
elif tP["isB"]: |
| 4211 |
numLra = self.states.b.numVgprLocalReadAddr |
| 4212 |
elif tP["isM"]: |
| 4213 |
numLra = self.states.m.numVgprLocalReadAddr |
| 4214 |
|
| 4215 |
if numLra > 1: |
| 4216 |
finalVgpr64K = vgpr("LocalReadAddr%s+1"%tP["tensorChar"]) |
| 4217 |
module.add(VAddU32(dst=finalVgpr64K, src0=0x10000, src1= vgpr("LocalReadAddr%s+0"%tP["tensorChar"]), \ |
| 4218 |
comment="Final vgprLocalReadAddr%s+1 Offset Plus 64K"%tP["tensorChar"] )) |
| 4219 |
if numLra > 2: |
| 4220 |
finalVgpr128K = vgpr("LocalReadAddr%s+2"%tP["tensorChar"]) |
| 4221 |
module.add(VAddU32(dst=finalVgpr128K, src0=0x20000, src1= vgpr("LocalReadAddr%s+0"%tP["tensorChar"]), \ |
| 4222 |
comment="Final vgprLocalReadAddr%s+2 Offset Plus 128K"%tP["tensorChar"] )) |
| 4223 |
return module |
| 4224 |
|
| 4225 |
def lraSwapAddressesForDTLPad(self, kernel, tP): |
| 4226 |
module = Module("lraSwapAddressesForDTLPad") |
| 4227 |
|
| 4228 |
tc = tP["tensorChar"] |
| 4229 |
if kernel["StoreSwapAddr"]: |
| 4230 |
module.add(VAddU32(dst=vgpr("LocalReadSwapAddr%s"%tc), src0=kernel["LdsOffsetA_Blk"], src1=vgpr("LocalReadAddr%s"%tc), \ |
| 4231 |
comment="Calculate starting lds addr of second buffer" )) |
| 4232 |
module.add(VXorB32(dst=vgpr("LocalReadSwapAddr%s"%tc), \ |
| 4233 |
src0=vgpr("LocalReadSwapAddr%s"%tc), \ |
| 4234 |
src1=vgpr("LocalReadAddr%s"%tc), \ |
| 4235 |
comment="xor both lds buffer offsets to enable swapping")) |
| 4236 |
return module |
| 4237 |
|
| 4238 |
|
| 4239 |
def lwaInitAddressesForDTLTailLoop(self, kernel, tP): |
| 4240 |
module = Module("lwaInitAddressesForDTLTailLoop") |
| 4241 |
|
| 4242 |
tc = tP["tensorChar"] |
| 4243 |
waveSize = kernel["WavefrontSize"] |
| 4244 |
if kernel["DirectToLds%c"%tc] and kernel["NonDTLTailLoop%s"%tc]: |
| 4245 |
module.addComment0("Set local write offsets for %c to be same as DTL %uB load"%(tc, kernel["GlobalReadVectorWidth%c"%tc] * tP["bpe"])) |
| 4246 |
module.add(VAndB32(dst=vgpr("LocalWriteAddr%c"%tc), src0=(waveSize - 1), src1=vgpr("Serial"), comment="Serial % wavesize")) |
| 4247 |
module.add(VLShiftLeftB32(dst=vgpr("LocalWriteAddr%c"%tc), shiftHex=hex(log2(kernel["GlobalReadVectorWidth%c"%tc] * tP["bpe"])), src=vgpr("LocalWriteAddr%c"%tc), comment="")) |
| 4248 |
module.add(VAddU32(dst=vgpr("LocalWriteAddr%s"%tc), src0=sgpr("LocalWriteAddr%c"%tc), src1=vgpr("LocalWriteAddr%s"%tc), \ |
| 4249 |
comment="" )) |
| 4250 |
numLw = 0 |
| 4251 |
if tP["isA"]: |
| 4252 |
numLw = self.states.a.numVgprLocalWriteAddrTailLoop |
| 4253 |
elif tP["isB"]: |
| 4254 |
numLw = self.states.b.numVgprLocalWriteAddrTailLoop |
| 4255 |
for i in range(1,numLw): |
| 4256 |
module.add(VAddU32(dst=vgpr("LocalWriteAddr%s + %s"%(tc, i)), src0=(i * 0x10000), \ |
| 4257 |
src1=vgpr("LocalWriteAddr%s"%tc), comment="" )) |
| 4258 |
return module |
| 4259 |
|
| 4260 |
|
| 4261 |
|
| 4262 |
|
| 4263 |
|
| 4264 |
|
| 4265 |
def openShadowInit(self): |
| 4266 |
module = Module("openShadowInit") |
| 4267 |
module.add(Label("ShadowInitStart", "")) |
| 4268 |
return module |
| 4269 |
|
| 4270 |
|
| 4271 |
|
| 4272 |
|
| 4273 |
|
| 4274 |
|
| 4275 |
def closeShadowInit(self, kernel): |
| 4276 |
module = Module("closeShadowInit") |
| 4277 |
assert(self.states.doShadowInit and kernel["PrefetchGlobalRead"]) |
| 4278 |
|
| 4279 |
module.add(self.checkLastIter(kernel)) |
| 4280 |
if kernel["SuppressNoLoadLoop"]: |
| 4281 |
loopChar = self.states.indexChars[ \ |
| 4282 |
kernel["ProblemType"]["IndicesSummation"][self.states.unrollIdx]] |
| 4283 |
lastIterEnd = Label("LoopEnd%s"%loopChar, "") |
| 4284 |
else: |
| 4285 |
lastIterEnd = Label("PrefetchGlobalLastIterEnd", "") |
| 4286 |
|
| 4287 |
|
| 4288 |
module.addComment1("after InitC, skip to end of prefetch last iter if numIter==0") |
| 4289 |
|
| 4290 |
with self.allocTmpSgpr(3) as tmpSgprInfo: |
| 4291 |
module.add(self.longBranchScc1(lastIterEnd, posNeg=1, tmpSgprInfo=tmpSgprInfo)) |
| 4292 |
|
| 4293 |
return module |
| 4294 |
|
| 4295 |
|
| 4296 |
|
| 4297 |
|
| 4298 |
def initC(self, kernel): |
| 4299 |
module = Module("initC") |
| 4300 |
self.vgprPool.remove(self.states.c.startVgprValu, self.states.c.numVgprValu, "ValuC") |
| 4301 |
module.addComment1("initC: remove ValuC vgpr buffer [%u...%u) from pool"%(self.states.c.startVgprValu, self.states.c.startVgprValu+self.states.c.numVgprValu)) |
| 4302 |
numAccvgprs = self.states.totalAgprs |
| 4303 |
self.agprPool.remove(0, numAccvgprs, "ValuC") |
| 4304 |
module.addComment1("initC: remove acc vgpr buffer [%u...%u) from pool"%(0, numAccvgprs)) |
| 4305 |
self.vgprPool.remove(self.states.a.startVgprValu , self.states.lastValuAB - self.states.a.startVgprValu , "ValuAB") |
| 4306 |
module.addComment1("initC: remove ValuA/B vgpr buffer [%u...%u) from pool"%(self.states.a.startVgprValu , self.states.lastValuAB)) |
| 4307 |
numCVgpr = self.states.c.numVgprValu + numAccvgprs |
| 4308 |
|
| 4309 |
if kernel["MfmaInitCVgprs"] == True and self.states.asmCaps["HasMFMA_f8f6f4"] and numCVgpr >= 32: |
| 4310 |
tmpVgpr = self.vgprPool.checkOutAligned(2,2,"tmp vgpr for lds init C registers") |
| 4311 |
module.add(VMovB64(dst=vgpr(tmpVgpr,2), src=0, comment="A/B=0")) |
| 4312 |
|
| 4313 |
chunkSize = 16 |
| 4314 |
for i in range(0, min(16, numCVgpr)): |
| 4315 |
copyInst = VMovB32 if i >= numAccvgprs else VAccvgprWrite |
| 4316 |
regStr = vgpr("ValuC+%u"%(i-numAccvgprs)) if i >= numAccvgprs else accvgpr(i) |
| 4317 |
module.add(copyInst(dst=regStr, src=0, comment="initC")) |
| 4318 |
|
| 4319 |
for i in range(min(16, numCVgpr), numCVgpr, 16): |
| 4320 |
if i + 16 <= numCVgpr: |
| 4321 |
module.add(MFMAInstruction(instType=InstType.INST_I8, accType=InstType.INST_I32, variant=[32,32,16,1], mfma1k=False, \ |
| 4322 |
acc=accvgpr(i,16), a=vgpr(tmpVgpr,2), b=vgpr(tmpVgpr,2), acc2=accvgpr(0,16), \ |
| 4323 |
comment="initC: [%u, %u]"%(i, i+15))) |
| 4324 |
else: |
| 4325 |
for j in range(i,min(i+16, numCVgpr)): |
| 4326 |
copyInst = VMovB32 if j >= numAccvgprs else VAccvgprWrite |
| 4327 |
regStr = vgpr("ValuC+%u"%(j-numAccvgprs)) if j >= numAccvgprs else accvgpr(j) |
| 4328 |
module.add(copyInst(dst=regStr, src=0, comment="initC")) |
| 4329 |
|
| 4330 |
self.vgprPool.checkIn(tmpVgpr) |
| 4331 |
else: |
| 4332 |
if kernel["LdsInitCVgprs"]: |
| 4333 |
tmpAddr = self.vgprPool.checkOut(1,"tmp vgpr for lds init C registers") |
| 4334 |
module.add(VMovB32(dst=vgpr(tmpAddr), src=self.consts.ldsOOB, comment="set out-of-bound addr")) |
| 4335 |
|
| 4336 |
for i in range(0, numCVgpr): |
| 4337 |
copyInst = VMovB32 if i >= numAccvgprs else VAccvgprWrite |
| 4338 |
regStr = vgpr("ValuC+%u"%(i-numAccvgprs)) if i >= numAccvgprs else accvgpr(i) |
| 4339 |
if not kernel["LdsInitCVgprs"]: |
| 4340 |
module.add(copyInst(dst=regStr, src=0, comment="initC")) |
| 4341 |
else: |
| 4342 |
module.add(DSLoadB32(dst=regStr, src=vgpr(tmpAddr), ds=DSModifiers(offset=0), comment="initC")) |
| 4343 |
|
| 4344 |
if kernel["LdsInitCVgprs"]: |
| 4345 |
self.vgprPool.checkIn(tmpAddr) |
| 4346 |
|
| 4347 |
return module |
| 4348 |
|
| 4349 |
def initSumUnroll(self, kernel): |
| 4350 |
return self.exclasses.biasSumUnroll.initSumUnroll(self, kernel) |
| 4351 |
|
| 4352 |
|
| 4353 |
|
| 4354 |
|
| 4355 |
|
| 4356 |
def declareStaggerParms(self, kernel): |
| 4357 |
module = Module("declareStaggerParms") |
| 4358 |
|
| 4359 |
with self.allocTmpSgpr(4) as tmpSgprInfo: |
| 4360 |
beginStaggerUIterLabel = Label("beginStaggerUIter",comment="") |
| 4361 |
endStaggerUIterLabel = Label("endStaggerUIter", comment="") |
| 4362 |
tmpSgpr = tmpSgprInfo.idx |
| 4363 |
currentStaggerU = tmpSgpr |
| 4364 |
shiftedStaggerU = tmpSgpr + 1 |
| 4365 |
staggerUMask = tmpSgpr + 1 |
| 4366 |
staggerUStrideShift = tmpSgpr + 2 |
| 4367 |
staggerUMapping = tmpSgpr + 3 |
| 4368 |
module.add(SAndB32(dst=sgpr(staggerUStrideShift), src0=sgpr("StaggerU"), src1=hex(0x1F00))) |
| 4369 |
module.add(SLShiftRightB32(dst=sgpr(staggerUStrideShift), shiftHex=hex(8), src=sgpr(staggerUStrideShift))) |
| 4370 |
module.add(SAndB32(dst=sgpr(staggerUMapping), src0=sgpr("StaggerU"), src1=hex(0xE000))) |
| 4371 |
module.add(SAndB32(dst=sgpr("StaggerU"), src0=sgpr("StaggerU"), src1=hex(0xFF))) |
| 4372 |
module.add(SMovB32(dst=sgpr(currentStaggerU), src=sgpr("StaggerU"), comment="init staggerU")) |
| 4373 |
module.add(beginStaggerUIterLabel) |
| 4374 |
module.add(SLShiftLeftB32(dst=sgpr(shiftedStaggerU), src=sgpr(currentStaggerU), \ |
| 4375 |
shiftHex=sgpr(staggerUStrideShift), comment="shift by StaggerUStride")) |
| 4376 |
module.add(SCmpGeU32(src0=sgpr("OrigLoopCounter"), src1=sgpr(shiftedStaggerU), \ |
| 4377 |
comment="loopCount >= current shift Count" )) |
| 4378 |
module.add(SCBranchSCC1(labelName=endStaggerUIterLabel.getLabelName(), comment="jump to end")) |
| 4379 |
module.add(SLShiftRightB32(dst=sgpr(currentStaggerU), src=sgpr(currentStaggerU), \ |
| 4380 |
shiftHex=1, comment="step down to smaller stagger")) |
| 4381 |
module.add(SBranch(labelName=beginStaggerUIterLabel.getLabelName(), comment="jump to begin")) |
| 4382 |
module.add(endStaggerUIterLabel) |
| 4383 |
module.add(SSubU32(dst=sgpr(staggerUMask), src0=sgpr(currentStaggerU), src1=1, comment="staggerU mask")) |
| 4384 |
module.add(SCmpGeU32(src0=sgpr(currentStaggerU), src1=1, \ |
| 4385 |
comment="if current staggerU >= 1" )) |
| 4386 |
module.add(SCSelectB32(dst=sgpr("StaggerUIter"), src0=sgpr(staggerUMask), src1=0, comment="set Mask")) |
| 4387 |
|
| 4388 |
staggerInput = tmpSgpr |
| 4389 |
staggerLabel = Label("staggerInputEnd", comment="") |
| 4390 |
for i in range(0, 5): |
| 4391 |
label = Label("StaggerUMapping_%d"%(i + 1), comment="") |
| 4392 |
module.add(SCmpEQU32(src0=sgpr(staggerUMapping), src1=hex(i << 13))) |
| 4393 |
if i != 4: |
| 4394 |
module.add(SCBranchSCC1(labelName=label.getLabelName())) |
| 4395 |
else: |
| 4396 |
module.add(SCBranchSCC1(labelName=staggerLabel.getLabelName())) |
| 4397 |
if i == 0: |
| 4398 |
module.add(SMovB32(dst=sgpr(staggerInput), src=sgpr("WorkGroup0"))) |
| 4399 |
elif i == 1: |
| 4400 |
module.add(SMovB32(dst=sgpr(staggerInput), src=sgpr("WorkGroup1"))) |
| 4401 |
elif i == 2 and len(kernel["ProblemType"]["IndicesBatch"]) > 2: |
| 4402 |
module.add(SMovB32(dst=sgpr(staggerInput), src=sgpr("WorkGroup2"))) |
| 4403 |
elif i == 3: |
| 4404 |
wgSerial = staggerInput |
| 4405 |
tmp = tmpSgpr+1 |
| 4406 |
if len(kernel["ProblemType"]["IndicesBatch"]) > 2: |
| 4407 |
module.add(SMulI32(dst=sgpr(wgSerial), src0=sgpr("NumWorkGroups0"), src1=sgpr("NumWorkGroups1"), \ |
| 4408 |
comment="wgSerial = (nwg0*ngw1)*wg2 + (nwg0)*wg1 + wg0")) |
| 4409 |
module.add(SMulI32(dst=sgpr(wgSerial), src0=sgpr(wgSerial), src1=sgpr("WorkGroup2"))) |
| 4410 |
module.add(SMulI32(dst=sgpr(tmp), src0=sgpr("NumWorkGroups0"), src1=sgpr("WorkGroup1"))) |
| 4411 |
module.add(SAddU32(dst=sgpr(wgSerial), src0=sgpr(wgSerial), src1=sgpr(tmp))) |
| 4412 |
module.add(SAddU32(dst=sgpr(wgSerial), src0=sgpr(wgSerial), src1=sgpr("WorkGroup0"))) |
| 4413 |
else: |
| 4414 |
module.add(SMovB32(dst=sgpr(staggerInput), src=hex(-1))) |
| 4415 |
module.add(SBranch(staggerLabel.getLabelName())) |
| 4416 |
if i != 4: |
| 4417 |
module.add(label) |
| 4418 |
else: |
| 4419 |
module.add(staggerLabel) |
| 4420 |
|
| 4421 |
module.add(SAndB32(dst=sgpr("StaggerUIter"), src0=sgpr("StaggerUIter"), \ |
| 4422 |
src1=sgpr(staggerInput), \ |
| 4423 |
comment="Compute actual stagger start for this tile")) |
| 4424 |
module.add(SLShiftLeftB32(dst=sgpr("StaggerUIter"), src=sgpr("StaggerUIter"), \ |
| 4425 |
shiftHex=sgpr(staggerUStrideShift), comment="shift by StaggerUStride")) |
| 4426 |
|
| 4427 |
skComponent = Component.StreamK.find(self) |
| 4428 |
module.add(skComponent.declareStaggerParms(self, kernel)) |
| 4429 |
|
| 4430 |
return module |
| 4431 |
|
| 4432 |
|
| 4433 |
|
| 4434 |
|
| 4435 |
def calculateStagger(self, kernel, tP): |
| 4436 |
imod = Module("calculateStagger") |
| 4437 |
tc = tP["tensorChar"] |
| 4438 |
|
| 4439 |
assert (kernel["BufferLoad"]) |
| 4440 |
|
| 4441 |
with self.allocTmpSgpr(3) as tmpSgprInfo: |
| 4442 |
staggerTmp = tmpSgprInfo.idx |
| 4443 |
incSparseSgpr = tmpSgprInfo.idx + 2 |
| 4444 |
|
| 4445 |
|
| 4446 |
imod.addComment1("SRDs += (StaggerUIter) * GlobalReadIncs%s+%u"% (tc, self.states.unrollIdx)) |
| 4447 |
|
| 4448 |
|
| 4449 |
imod.addModuleAsFlatItems(self.s_mul_i64_i32( |
| 4450 |
sgpr(staggerTmp), sgpr(staggerTmp+1), \ |
| 4451 |
sgpr("StaggerUIter"), sgpr("GlobalReadIncs%s+%u"%(tc, self.states.unrollIdx)), \ |
| 4452 |
" stagger byte offset")) |
| 4453 |
|
| 4454 |
|
| 4455 |
|
| 4456 |
imod.addModuleAsFlatItems(self.s_mul_i64_i32(sgpr("WrapU%s+0"%tc), sgpr("WrapU%s+1"%tc), \ |
| 4457 |
self.loopCounter(kernel, self.states.unrollIdx), sgpr("GlobalReadIncs%s+%u"%(tc,self.states.unrollIdx)), \ |
| 4458 |
"Number of bytes accessed by the unroll loop")) |
| 4459 |
|
| 4460 |
imod.add(SSubU32(dst=sgpr("WrapU%s+0"%tc), \ |
| 4461 |
src0=sgpr("GlobalReadIncs%s+%u"%(tc,self.states.unrollIdx)), \ |
| 4462 |
src1=sgpr("WrapU%s+0"%tc), \ |
| 4463 |
comment="remove one iteration")) |
| 4464 |
imod.add(SSubBU32(dst=sgpr("WrapU%s+1"%tc), \ |
| 4465 |
src0=0, \ |
| 4466 |
src1=sgpr("WrapU%s+1"%tc), \ |
| 4467 |
comment="remove one iteration")) |
| 4468 |
|
| 4469 |
imod.add(self.incrementSrd(tP, sgpr(staggerTmp), sgpr(staggerTmp+1))) |
| 4470 |
|
| 4471 |
if kernel["ProblemType"]["Sparse"] and kernel["DirectToVgprSparseMetadata"] and \ |
| 4472 |
((kernel["ProblemType"]["Sparse"] == 2 and tP["isB"]) or (kernel["ProblemType"]["Sparse"] == 1 and tP["isA"])): |
| 4473 |
imod.addComment1("SRDs += (StaggerUIter) * GlobalReadIncsMetadata") |
| 4474 |
|
| 4475 |
tc = "Metadata" |
| 4476 |
if kernel["DirectToVgprSparseMetadata"]: |
| 4477 |
incSparse = incSparseSgpr |
| 4478 |
imod.add(self.calculateIncrementMetadata(kernel, incSparse)) |
| 4479 |
else: |
| 4480 |
incSparse = "GlobalReadIncsMetadata+%u"%(self.states.unrollIdx) |
| 4481 |
imod.addModuleAsFlatItems(self.s_mul_i64_i32( \ |
| 4482 |
sgpr(staggerTmp), sgpr(staggerTmp+1), \ |
| 4483 |
sgpr("StaggerUIter"), sgpr(incSparse), " stagger byte offset of metadata")) |
| 4484 |
|
| 4485 |
|
| 4486 |
imod.addModuleAsFlatItems(self.s_mul_i64_i32( \ |
| 4487 |
sgpr("WrapU%s+0"%tc), sgpr("WrapU%s+1"%tc), \ |
| 4488 |
self.loopCounter(kernel, self.states.unrollIdx), sgpr(incSparse), \ |
| 4489 |
"Number of bytes accessed by the unroll loop")) |
| 4490 |
|
| 4491 |
imod.add(SSubU32(sgpr("WrapU%s+0"%tc), sgpr(incSparse), sgpr("WrapU%s+0"%tc), " remove one iteration")) |
| 4492 |
imod.add(SSubBU32(sgpr("WrapU%s+1"%tc), 0, sgpr("WrapU%s+1"%tc), " remove one iteration")) |
| 4493 |
|
| 4494 |
if kernel["DirectToVgprSparseMetadata"]: |
| 4495 |
imod.add(self.incrementMetadataSrd(sgpr(staggerTmp), sgpr(staggerTmp+1))) |
| 4496 |
else: |
| 4497 |
imod.add(self.incrementSrd(tP["tpsMetadata"], sgpr(staggerTmp), sgpr(staggerTmp+1))) |
| 4498 |
|
| 4499 |
if tP["isB"]: |
| 4500 |
isDTVAorB = (kernel["DirectToVgprA"] != kernel["DirectToVgprB"]) |
| 4501 |
if kernel["PrefetchGlobalRead"] == 2 and isDTVAorB: |
| 4502 |
|
| 4503 |
imod.add(SAddU32(dst=sgpr("StaggerUIterDTV"), src0=sgpr("StaggerUIter"), \ |
| 4504 |
src1=(1), \ |
| 4505 |
comment="Subtract (PGR-1); StaggerUIter now contains target iteration to wrap")) |
| 4506 |
|
| 4507 |
imod.add(SAddU32(dst=sgpr("StaggerUIter"), src0=sgpr("StaggerUIter"), \ |
| 4508 |
src1=(2 if kernel["PrefetchGlobalRead"] else 1), \ |
| 4509 |
comment="Subtract (PGR-1); StaggerUIter now contains target iteration to wrap")) |
| 4510 |
return imod |
| 4511 |
|
| 4512 |
|
| 4513 |
|
| 4514 |
|
| 4515 |
|
| 4516 |
|
| 4517 |
|
| 4518 |
|
| 4519 |
|
| 4520 |
|
| 4521 |
|
| 4522 |
|
| 4523 |
|
| 4524 |
|
| 4525 |
|
| 4526 |
|
| 4527 |
|
| 4528 |
|
| 4529 |
|
| 4530 |
|
| 4531 |
|
| 4532 |
|
| 4533 |
|
| 4534 |
|
| 4535 |
|
| 4536 |
|
| 4537 |
|
| 4538 |
def removeStagger(self, kernel, tP): |
| 4539 |
imod = Module("removeStagger") |
| 4540 |
tc = tP["tensorChar"] |
| 4541 |
with self.allocTmpSgpr(3) as tmpSgprInfo: |
| 4542 |
tmp = tmpSgprInfo.idx |
| 4543 |
tmpIncSparse = tmpSgprInfo.idx + 2 |
| 4544 |
|
| 4545 |
imod.add(SSubI32(dst=sgpr(tmp), src0=3 if kernel["PrefetchGlobalRead"] else 2, \ |
| 4546 |
src1=sgpr("StaggerUIter"))) |
| 4547 |
imod.addModuleAsFlatItems(self.s_mul_i64_i32_u32(sgpr(tmp), sgpr(tmp+1), \ |
| 4548 |
sgpr(tmp), sgpr("GlobalReadIncs%s+%u"%(tc,self.states.unrollIdx)), \ |
| 4549 |
"start offset S in bytes")) |
| 4550 |
imod.add(SSubU32(dst=sgpr(tmp), src0=sgpr(tmp), src1=sgpr("WrapU%s"%tc), comment="S - WrapU")) |
| 4551 |
imod.add(SSubBU32(dst=sgpr(tmp+1), src0=sgpr(tmp+1), src1=sgpr("WrapU%s+1"%(tc)), comment="S - WrapU")) |
| 4552 |
|
| 4553 |
imod.add(self.incrementSrd(tP, sgpr(tmp), sgpr(tmp+1))) |
| 4554 |
|
| 4555 |
if kernel["ProblemType"]["Sparse"] and \ |
| 4556 |
((kernel["ProblemType"]["Sparse"] == 2 and tP["isB"]) or (kernel["ProblemType"]["Sparse"] == 1 and tP["isA"])): |
| 4557 |
tc = "Metadata" |
| 4558 |
if kernel["DirectToVgprSparseMetadata"]: |
| 4559 |
incSparse = tmpIncSparse |
| 4560 |
imod.add(self.calculateIncrementMetadata(kernel, incSparse)) |
| 4561 |
else: |
| 4562 |
incSparse = "GlobalReadIncs%s+%u"%(tc,self.states.unrollIdx) |
| 4563 |
|
| 4564 |
|
| 4565 |
imod.add(SSubI32(dst=sgpr(tmp), src0=3 if kernel["PrefetchGlobalRead"] else 2, \ |
| 4566 |
src1=sgpr("StaggerUIter"))) |
| 4567 |
imod.addModuleAsFlatItems(self.s_mul_i64_i32_u32(sgpr(tmp), sgpr(tmp+1), \ |
| 4568 |
sgpr(tmp), sgpr(incSparse), \ |
| 4569 |
"start offset S in bytes")) |
| 4570 |
imod.add(SSubU32(sgpr(tmp), sgpr(tmp), sgpr("WrapU%s"%tc), "S - WrapU")) |
| 4571 |
imod.add(SSubBU32(sgpr(tmp+1), sgpr(tmp+1), sgpr("WrapU%s+1"%(tc)), "S - WrapU")) |
| 4572 |
|
| 4573 |
if kernel["DirectToVgprSparseMetadata"]: |
| 4574 |
imod.add(self.incrementMetadataSrd(sgpr(tmp), sgpr(tmp+1))) |
| 4575 |
else: |
| 4576 |
imod.add(self.incrementSrd(tP["tpsMetadata"], sgpr(tmp), sgpr(tmp+1))) |
| 4577 |
|
| 4578 |
return imod |
| 4579 |
|
| 4580 |
|
| 4581 |
|
| 4582 |
|
| 4583 |
def undefineMacroModule(self, macroModule): |
| 4584 |
imod = Module() |
| 4585 |
for inst in macroModule.items(): |
| 4586 |
if isinstance(inst, (RegSet, ValueSet)): |
| 4587 |
imod.add(ValueSet(name=inst.name, value="UNDEF", format = -1)) |
| 4588 |
elif isinstance(inst, Module): |
| 4589 |
imod.add(self.undefineMacroModule(inst)) |
| 4590 |
return imod |
| 4591 |
|
| 4592 |
|
| 4593 |
|
| 4594 |
|
| 4595 |
def tailLoopFreeVgpr(self, vgprBase, moduleMacro): |
| 4596 |
imod = Module("tailLoopFreeVgpr") |
| 4597 |
if vgprBase != -1: |
| 4598 |
imod.add(self.undefineMacroModule(moduleMacro)) |
| 4599 |
self.vgprPool.checkIn(vgprBase) |
| 4600 |
return imod |
| 4601 |
|
| 4602 |
|
| 4603 |
|
| 4604 |
|
| 4605 |
|
| 4606 |
|
| 4607 |
|
| 4608 |
|
| 4609 |
|
| 4610 |
def tailLoopAllocValuVgpr(self, kernel, tensorParametersA, tensorParametersB, tensorParametersM): |
| 4611 |
imodA = Module("tailLoopAllocValuAVgpr") |
| 4612 |
vgprBaseA = -1 |
| 4613 |
numValuA = 0 |
| 4614 |
numVgprValuPackA = 0 |
| 4615 |
if self.states.a.numVgprValu > 0 and not kernel["DirectToVgprA"]: |
| 4616 |
numValuA = self.states.a.numVgprValu |
| 4617 |
if tensorParametersA["bpe"] < 4 and not kernel["UnrollMajorLDSA"] and not kernel["enableLDSTrA"]: |
| 4618 |
if self.states.lrvwTileA > 1: |
| 4619 |
numVgprValuPackA = ceil(kernel["VectorWidthA"] * tensorParametersA["bpe"] / self.states.bpr) * kernel["MIWaveTileA"] // kernel["VectorWidthA"] * kernel["InnerUnroll"] * self.states.numVgprBuffer * kernel["MIInputPerThreadA"] |
| 4620 |
if self.states.packDTVA: |
| 4621 |
|
| 4622 |
numVgprValuPackA *= 2 |
| 4623 |
elif (kernel["UsePLRPack"] and self.states.numItersPLR): |
| 4624 |
numVgprValuPackA //= 2 |
| 4625 |
else: |
| 4626 |
numVgprValuPackA = self.states.a.numVgprValuPerBlock * kernel["InnerUnroll"] * self.states.numVgprBufferPackA * (int(4/tensorParametersA["bpeDS"]) - 1) |
| 4627 |
vgprBaseA = self.vgprPool.checkOutAligned(numValuA + numVgprValuPackA, 2) |
| 4628 |
imodA.add(RegSet("v", "vgprValuA_X0_I0_BASE", vgprBaseA)) |
| 4629 |
imodA.add(self.moduleVgprMacroValuA) |
| 4630 |
if numVgprValuPackA > 0: |
| 4631 |
imodA.add(RegSet("v", "vgprValuA_X0_I0_D0_PACK", vgprBaseA + numValuA)) |
| 4632 |
imodA.add(self.moduleVgprMacroValuAPack) |
| 4633 |
|
| 4634 |
imodB = Module("tailLoopAllocValuBVgpr") |
| 4635 |
vgprBaseB = -1 |
| 4636 |
numValuB = 0 |
| 4637 |
numVgprValuPackB = 0 |
| 4638 |
if self.states.b.numVgprValu > 0 and not kernel["DirectToVgprB"]: |
| 4639 |
numValuB = self.states.b.numVgprValu |
| 4640 |
if tensorParametersB["bpe"] < 4 and not kernel["UnrollMajorLDSB"] and not kernel["enableLDSTrB"]: |
| 4641 |
if self.states.lrvwTileB > 1: |
| 4642 |
numVgprValuPackB = ceil(kernel["VectorWidthB"] * tensorParametersB["bpe"] / self.states.bpr) * kernel["MIWaveTileB"] // kernel["VectorWidthB"] * kernel["InnerUnroll"] * self.states.numVgprBuffer * kernel["MIInputPerThreadB"] |
| 4643 |
if self.states.packDTVB: |
| 4644 |
|
| 4645 |
numVgprValuPackB *= 2 |
| 4646 |
elif (kernel["UsePLRPack"] and self.states.numItersPLR): |
| 4647 |
numVgprValuPackB //= 2 |
| 4648 |
else: |
| 4649 |
numVgprValuPackB = self.states.b.numVgprValuPerBlock * kernel["InnerUnroll"] * self.states.numVgprBufferPackB * (int(4/tensorParametersB["bpeDS"]) - 1) |
| 4650 |
|
| 4651 |
vgprBaseB = self.vgprPool.checkOutAligned(numValuB + numVgprValuPackB, 2) |
| 4652 |
imodB.add(RegSet("v", "vgprValuB_X0_I0_BASE", vgprBaseB)) |
| 4653 |
imodB.add(self.moduleVgprMacroValuB) |
| 4654 |
if numVgprValuPackB > 0: |
| 4655 |
imodB.add(RegSet("v", "vgprValuB_X0_I0_D0_PACK", vgprBaseB + numValuB)) |
| 4656 |
imodB.add(self.moduleVgprMacroValuBPack) |
| 4657 |
|
| 4658 |
imodM = Module("tailLoopAllocValuMetadataVgpr") |
| 4659 |
vgprBaseM = -1 |
| 4660 |
numValuM = 0 |
| 4661 |
numVgprValuPackMetadata = 0 |
| 4662 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 4663 |
numValuM = self.states.m.numVgprValu |
| 4664 |
if not kernel["UnrollMajorLDSMetadata"]: |
| 4665 |
if self.states.lrvwTileMetadata > 1: |
| 4666 |
miWaveTile = kernel["MIWaveTileB"] if kernel["ProblemType"]["Sparse"] == 2 else kernel["MIWaveTileA"] |
| 4667 |
numVgprValuPackMetadata = roundUp(kernel["VectorWidthMetadata"] * tensorParametersM["bpe"] / self.states.bpr) * miWaveTile // kernel["VectorWidthMetadata"] * kernel["InnerUnroll"] * self.states.numVgprBuffer * kernel["MIInputPerThreadMetadata"] |
| 4668 |
else: |
| 4669 |
numVgprValuPackMetadata = self.states.m.numVgprValuPerBlock * kernel["InnerUnroll"] * self.states.numVgprBufferPackMetadata * (int(4/tensorParametersM["bpe"]) - 1) |
| 4670 |
|
| 4671 |
vgprBaseM = self.vgprPool.checkOutAligned(max(numValuM + numVgprValuPackMetadata, self.states.m.numVgprG2LAllocated), 2) |
| 4672 |
imodM.add(RegSet("v", "vgprValuMetadata_X0_I0_BASE", vgprBaseM)) |
| 4673 |
imodM.add(self.moduleVgprMacroValuM) |
| 4674 |
if numVgprValuPackMetadata > 0: |
| 4675 |
imodM.add(RegSet("v", "vgprValuMetadata_X0_I0_D_PACK", vgprBaseM + numValuM)) |
| 4676 |
imodM.add(self.moduleVgprMacroValuMPack) |
| 4677 |
|
| 4678 |
imodMisc = Module("tailLoopAllocValuMiscVgpr") |
| 4679 |
vgprBaseMisc = -1 |
| 4680 |
numVgprPackTemp = 0 |
| 4681 |
if ((tensorParametersA["bpe"] < 4 and not kernel["UnrollMajorLDSA"]) \ |
| 4682 |
or (tensorParametersB["bpe"] < 4 and not kernel["UnrollMajorLDSB"]) \ |
| 4683 |
or (not kernel["UnrollMajorLDSMetadata"] and kernel["MIInputPerThreadMetadata"] == 4)) \ |
| 4684 |
and (kernel["ProblemType"]["DataType"].isInt8() or kernel["ProblemType"]["DataType"].is8bitFloat()): |
| 4685 |
numVgprPackTemp = 1 |
| 4686 |
numVgprCvtTemp = 0 |
| 4687 |
if self.states.a.startVgprValuCvtTemp != -1 or self.states.b.startVgprValuCvtTemp != -1: |
| 4688 |
numVgprCvtTemp = 2 |
| 4689 |
if numVgprPackTemp + numVgprCvtTemp > 0: |
| 4690 |
vgprBaseMisc = self.vgprPool.checkOut(numVgprPackTemp + numVgprCvtTemp) |
| 4691 |
if numVgprPackTemp > 0: |
| 4692 |
imodMisc.add(RegSet("v", "vgprPackTemp", vgprBaseMisc)) |
| 4693 |
if numVgprCvtTemp > 0: |
| 4694 |
imodMisc.add(RegSet("v", "vgprCvtTemp", vgprBaseMisc + numVgprPackTemp)) |
| 4695 |
|
| 4696 |
return ([vgprBaseA, imodA],[vgprBaseB, imodB],[vgprBaseM, imodM],[vgprBaseMisc, imodMisc]) |
| 4697 |
|
| 4698 |
def tailLoopAllocG2LVgpr(self, kernel): |
| 4699 |
imod = Module("tailLoopAllocG2LVgpr") |
| 4700 |
vgprBase = -1 |
| 4701 |
numG2LA = 0 |
| 4702 |
numG2LB = 0 |
| 4703 |
numG2LMetadata = 0 |
| 4704 |
if not kernel["DirectToVgprA"]: |
| 4705 |
if ("ULSGRODoubleG2L" in kernel) and kernel["ULSGRODoubleG2L"] == 1: |
| 4706 |
numG2LA = self.states.a.numVgprG2LTailloopAllocated*2 |
| 4707 |
else: |
| 4708 |
numG2LA = self.states.a.numVgprG2LTailloopAllocated |
| 4709 |
if not kernel["DirectToVgprB"]: |
| 4710 |
if ("ULSGRODoubleG2L" in kernel) and kernel["ULSGRODoubleG2L"] == 1: |
| 4711 |
numG2LB = self.states.b.numVgprG2LTailloopAllocated*2 |
| 4712 |
else: |
| 4713 |
numG2LB = self.states.b.numVgprG2LTailloopAllocated |
| 4714 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 4715 |
numG2LMetadata = self.states.m.numVgprG2LAllocated |
| 4716 |
|
| 4717 |
if numG2LB > 0: |
| 4718 |
|
| 4719 |
numG2LA = ((numG2LA+1)//2)*2 |
| 4720 |
if numG2LMetadata > 0: |
| 4721 |
|
| 4722 |
numTemp1 = numG2LA + numG2LB |
| 4723 |
numTemp2 = ((numTemp1+1)//2)*2 |
| 4724 |
numG2LB += (numTemp2 - numTemp1) |
| 4725 |
if numG2LA + numG2LB + numG2LMetadata > 0: |
| 4726 |
vgprBase = self.vgprPool.checkOutAligned(numG2LA + numG2LB + numG2LMetadata, 2) |
| 4727 |
imod.addComment0("Check out VGPR (numG2LA,numG2LB,numG2LMetadata) = (%d,%d,%d)"%(numG2LA,numG2LB,numG2LMetadata)) |
| 4728 |
if numG2LA > 0: |
| 4729 |
imod.add(RegSet("v", "vgprG2LA_BASE", vgprBase)) |
| 4730 |
if kernel["DirectToLdsA"] and kernel["NonDTLTailLoopA"]: |
| 4731 |
imod.add(RegSet("v", "vgprG2LA", "vgprG2LA_BASE", 0)) |
| 4732 |
else: |
| 4733 |
imod.add(self.moduleVgprMacroG2LA) |
| 4734 |
if numG2LB > 0: |
| 4735 |
imod.add(RegSet("v", "vgprG2LB_BASE", vgprBase + numG2LA)) |
| 4736 |
if kernel["DirectToLdsB"] and kernel["NonDTLTailLoopB"]: |
| 4737 |
imod.add(RegSet("v", "vgprG2LB", "vgprG2LB_BASE", 0)) |
| 4738 |
else: |
| 4739 |
imod.add(self.moduleVgprMacroG2LB) |
| 4740 |
if numG2LMetadata > 0: |
| 4741 |
imod.add(RegSet("v", "vgprG2LMetadata", vgprBase + numG2LA + numG2LB)) |
| 4742 |
|
| 4743 |
return imod, vgprBase |
| 4744 |
|
| 4745 |
def tailLoopAllocDTLLWVgpr(self, kernel): |
| 4746 |
imod = Module("tailLoopAllocDTLLWVgpr") |
| 4747 |
vgprBase = -1 |
| 4748 |
numLWA = self.states.a.numVgprLocalWriteAddrTailLoop |
| 4749 |
numLWB = self.states.b.numVgprLocalWriteAddrTailLoop |
| 4750 |
|
| 4751 |
if numLWA + numLWB > 0: |
| 4752 |
vgprBase = self.vgprPool.checkOutAligned(numLWA + numLWB, 2) |
| 4753 |
imod.addComment0("Check out VGPR (numLWA,numLWB) = (%d,%d)"%(numLWA,numLWB)) |
| 4754 |
if numLWA > 0 and (kernel["DirectToLdsA"] and kernel["NonDTLTailLoopA"]): |
| 4755 |
imod.add(RegSet("v", "vgprLocalWriteAddrA", vgprBase)) |
| 4756 |
if numLWB > 0 and (kernel["DirectToLdsB"] and kernel["NonDTLTailLoopB"]): |
| 4757 |
imod.add(RegSet("v", "vgprLocalWriteAddrB", vgprBase + numLWA)) |
| 4758 |
|
| 4759 |
return imod, vgprBase |
| 4760 |
|
| 4761 |
|
| 4762 |
def tailLoopAllocDTVVgpr(self, kernel, tensorParametersA, tensorParametersB): |
| 4763 |
imodA = Module("tailLoopAllocDTVVgprA") |
| 4764 |
vgprBaseA = -1 |
| 4765 |
if kernel["DirectToVgprA"]: |
| 4766 |
if self.states.packDTVA or self.states.convDTVA: |
| 4767 |
numValuA = self.states.a.numVgprValu |
| 4768 |
numVgprValuPackA = 0 |
| 4769 |
if tensorParametersA["bpe"] < 4 and not kernel["UnrollMajorLDSA"]: |
| 4770 |
if self.states.lrvwTileA > 1: |
| 4771 |
numVgprValuPackA = ceil(kernel["VectorWidthA"] * tensorParametersA["bpe"] / self.states.bpr) * kernel["MIWaveTileA"] // kernel["VectorWidthA"] * kernel["InnerUnroll"] * self.states.numVgprBuffer * kernel["MIInputPerThreadA"] |
| 4772 |
if self.states.packDTVA: |
| 4773 |
|
| 4774 |
numVgprValuPackA *= 2 |
| 4775 |
else: |
| 4776 |
numVgprValuPackA = self.states.a.numVgprValuPerBlock * kernel["InnerUnroll"] * self.states.numVgprBufferPackA * (int(4/tensorParametersA["bpeDS"]) - 1) |
| 4777 |
|
| 4778 |
vgprBaseA = self.vgprPool.checkOutAligned(numValuA + numVgprValuPackA, 2) |
| 4779 |
imodA.add(RegSet("v", "vgprValuA_X0_I0_BASE", vgprBaseA)) |
| 4780 |
if numVgprValuPackA > 0: |
| 4781 |
imodA.add(RegSet("v", "vgprValuA_X0_I0_D0_PACK", vgprBaseA + numValuA)) |
| 4782 |
if self.states.packDTVA: |
| 4783 |
|
| 4784 |
imodA.add(RegSet("v", "vgprG2LA_BASE", vgprBaseA + numValuA)) |
| 4785 |
elif self.states.convDTVA: |
| 4786 |
|
| 4787 |
imodA.add(RegSet("v", "vgprG2LA_BASE", vgprBaseA)) |
| 4788 |
imodA.add(self.moduleVgprMacroG2LA) |
| 4789 |
imodA.add(self.moduleVgprMacroValuA) |
| 4790 |
imodA.add(self.moduleVgprMacroValuAPack) |
| 4791 |
else: |
| 4792 |
|
| 4793 |
if kernel["ULSGRODoubleG2L"] == 1: |
| 4794 |
numG2LA = self.states.a.numVgprG2LAllocated*2 |
| 4795 |
else: |
| 4796 |
numG2LA = self.states.a.numVgprG2LAllocated |
| 4797 |
vgprBaseA = self.vgprPool.checkOutAligned(numG2LA, 2) |
| 4798 |
imodA.add(RegSet("v", "vgprG2LA_BASE", vgprBaseA)) |
| 4799 |
imodA.add(self.moduleVgprMacroG2LA) |
| 4800 |
|
| 4801 |
imodB = Module("tailLoopAllocDTVVgprA") |
| 4802 |
vgprBaseB = -1 |
| 4803 |
if kernel["DirectToVgprB"]: |
| 4804 |
if self.states.packDTVB or self.states.convDTVB: |
| 4805 |
numValuB = self.states.b.numVgprValu |
| 4806 |
numVgprValuPackB = 0 |
| 4807 |
if tensorParametersB["bpe"] < 4 and not kernel["UnrollMajorLDSB"]: |
| 4808 |
if self.states.lrvwTileB > 1: |
| 4809 |
numVgprValuPackB = ceil(kernel["VectorWidthB"] * tensorParametersB["bpe"] / self.states.bpr) * kernel["MIWaveTileB"] // kernel["VectorWidthB"] * kernel["InnerUnroll"] * self.states.numVgprBuffer * kernel["MIInputPerThreadB"] |
| 4810 |
if self.states.packDTVB: |
| 4811 |
|
| 4812 |
numVgprValuPackB *= 2 |
| 4813 |
else: |
| 4814 |
numVgprValuPackB = self.states.b.numVgprValuPerBlock * kernel["InnerUnroll"] * self.states.numVgprBufferPackB * (int(4/tensorParametersB["bpeDS"]) - 1) |
| 4815 |
|
| 4816 |
vgprBaseB = self.vgprPool.checkOutAligned(numValuB + numVgprValuPackB, 2) |
| 4817 |
imodB.add(RegSet("v", "vgprValuB_X0_I0_BASE", vgprBaseB)) |
| 4818 |
if numVgprValuPackB > 0: |
| 4819 |
imodB.add(RegSet("v", "vgprValuB_X0_I0_D0_PACK", vgprBaseB + numValuB)) |
| 4820 |
if self.states.packDTVB: |
| 4821 |
|
| 4822 |
imodB.add(RegSet("v", "vgprG2LB_BASE", vgprBaseB + numValuB)) |
| 4823 |
elif self.states.convDTVB: |
| 4824 |
|
| 4825 |
imodB.add(RegSet("v", "vgprG2LB_BASE", vgprBaseB)) |
| 4826 |
imodB.add(self.moduleVgprMacroG2LB) |
| 4827 |
imodB.add(self.moduleVgprMacroValuB) |
| 4828 |
imodB.add(self.moduleVgprMacroValuBPack) |
| 4829 |
else: |
| 4830 |
|
| 4831 |
if kernel["ULSGRODoubleG2L"] == 1: |
| 4832 |
numG2LB = self.states.b.numVgprG2LAllocated*2 |
| 4833 |
else: |
| 4834 |
numG2LB = self.states.b.numVgprG2LAllocated |
| 4835 |
vgprBaseB = self.vgprPool.checkOutAligned(numG2LB, 2) |
| 4836 |
imodB.add(RegSet("v", "vgprG2LB_BASE", vgprBaseB)) |
| 4837 |
imodB.add(self.moduleVgprMacroG2LB) |
| 4838 |
|
| 4839 |
imodM = Module("tailLoopAllocDTVVgprA") |
| 4840 |
vgprBaseM = -1 |
| 4841 |
if kernel["ProblemType"]["Sparse"]: |
| 4842 |
if kernel["DirectToVgprSparseMetadata"]: |
| 4843 |
numValuM = self.states.m.numVgprValu |
| 4844 |
vgprBaseM = self.vgprPool.checkOut(numValuM) |
| 4845 |
imodM.add(RegSet("v", "vgprValuMetadata_X0_I0_BASE", vgprBaseM)) |
| 4846 |
imodM.add(self.moduleVgprMacroValuM) |
| 4847 |
|
| 4848 |
return ([vgprBaseA, imodA],[vgprBaseB, imodB],[vgprBaseM, imodM]) |
| 4849 |
|
| 4850 |
|
| 4851 |
|
| 4852 |
|
| 4853 |
|
| 4854 |
|
| 4855 |
def tailLoopGlobalRead(self, kernel, tPA, tPB, doA, doB): |
| 4856 |
imod = Module("tailLoopGlobalRead") |
| 4857 |
|
| 4858 |
glvwWorkaround = 8 * kernel["ProblemType"]["DataType"].numRegisters() |
| 4859 |
dataTypeA = kernel["ProblemType"]["DataType"] if tPA["glvw"] < glvwWorkaround else \ |
| 4860 |
kernel["ProblemType"]["DataTypeA"] |
| 4861 |
dataTypeB = kernel["ProblemType"]["DataType"] if tPB["glvw"] < glvwWorkaround else \ |
| 4862 |
kernel["ProblemType"]["DataTypeB"] |
| 4863 |
numElementsPerLoadA = -1 |
| 4864 |
numElementsPerLoadB = -1 |
| 4865 |
if dataTypeA.isHalf() or dataTypeA.isBFloat16(): |
| 4866 |
if tPA["glvw"] > 1 and kernel["AssertSummationElementMultiple"] % 2 == 0: |
| 4867 |
numElementsPerLoadA = 2 |
| 4868 |
if dataTypeB.isHalf() or dataTypeB.isBFloat16(): |
| 4869 |
if tPB["glvw"] > 1 and kernel["AssertSummationElementMultiple"] % 2 == 0: |
| 4870 |
numElementsPerLoadB = 2 |
| 4871 |
if numElementsPerLoadA == 2: |
| 4872 |
doA = False |
| 4873 |
if numElementsPerLoadB == 2: |
| 4874 |
doB = False |
| 4875 |
|
| 4876 |
tagList = ["AddressA", "AddressB", "WrapUA", "WrapUB", "StaggerU", "WGM", \ |
| 4877 |
"StaggerUIter", "GlobalReadIncsA", "GlobalReadIncsB", \ |
| 4878 |
"sgprStridesA", "sgprStridesB", "sgprShadowLimitA", "sgprShadowLimitB"] |
| 4879 |
lastRegTag = None |
| 4880 |
spool = self.sgprPool.getPool() |
| 4881 |
imod.addComment1("release sgprs that will not be used") |
| 4882 |
for i in range(0, self.sgprPool.size()): |
| 4883 |
regTag = spool[i].tag |
| 4884 |
if regTag != lastRegTag: |
| 4885 |
lastRegTag = regTag |
| 4886 |
if (lastRegTag not in self.states.nonPostLoopSgpr) and \ |
| 4887 |
(spool[i].status == RegisterPool.Status.InUse) and \ |
| 4888 |
(lastRegTag in tagList): |
| 4889 |
imod.add(self.undefineSgpr(regTag)) |
| 4890 |
|
| 4891 |
loadALabel = Label(label="LoadA", comment="") |
| 4892 |
loadBLabel = Label(label="LoadB", comment="") |
| 4893 |
mergeALabel = Label(label="MergeA", comment="") |
| 4894 |
mergeBLabel = Label(label="MergeB", comment="") |
| 4895 |
tailGlobalLoadEndLabel = Label(label="TailGlobalLoadEnd", comment="") |
| 4896 |
checkOtherLoadALabel = Label(label="CheckOtherLoadA", comment="") |
| 4897 |
checkAddrALabel = Label(label="CheckAddrA", comment="") |
| 4898 |
checkOtherLoadBLabel = Label(label="CheckOtherLoadB", comment="") |
| 4899 |
checkAddrBLabel = Label(label="CheckAddrB", comment="") |
| 4900 |
lspA = kernel[tPA["lsp"]] |
| 4901 |
lscA = kernel[tPA["lsc"]] |
| 4902 |
lspB = kernel[tPB["lsp"]] |
| 4903 |
lscB = kernel[tPB["lsc"]] |
| 4904 |
nlcA = kernel["NumLoadsCoalescedA"] |
| 4905 |
nlcB = kernel["NumLoadsCoalescedB"] |
| 4906 |
nlpA = kernel["NumLoadsPerpendicularA"] |
| 4907 |
nlpB = kernel["NumLoadsPerpendicularB"] |
| 4908 |
|
| 4909 |
numElementsPer4BytesA = int(4 / tPA["bpeGR"]) |
| 4910 |
numElementsPer4BytesB = int(4 / tPB["bpeGR"]) |
| 4911 |
maxNumOOBElementsA = numElementsPer4BytesA - 1 |
| 4912 |
maxNumOOBElementsB = numElementsPer4BytesB - 1 |
| 4913 |
|
| 4914 |
numDwordA = (tPA["glvw"] * tPA["bpeGR"]) >> 2 |
| 4915 |
numDwordB = (tPB["glvw"] * tPB["bpeGR"]) >> 2 |
| 4916 |
numDwordA = 1 if numDwordA == 0 else numDwordA |
| 4917 |
numDwordB = 1 if numDwordB == 0 else numDwordB |
| 4918 |
numTmpVgpr = maxNumOOBElementsA * numDwordA + maxNumOOBElementsB * numDwordB |
| 4919 |
|
| 4920 |
numSingleSgpr = 10 |
| 4921 |
numPairSgpr = 2 |
| 4922 |
singSgprList = [] |
| 4923 |
pairSgprList = [] |
| 4924 |
|
| 4925 |
for num in range(numSingleSgpr): |
| 4926 |
tmpSgpr = self.sgprPool.checkOut(1, preventOverflow=False) |
| 4927 |
singSgprList.append(tmpSgpr) |
| 4928 |
for num in range(numPairSgpr): |
| 4929 |
tmpSgpr = self.sgprPool.checkOutAligned(2, 2, preventOverflow=False) |
| 4930 |
pairSgprList.append(tmpSgpr) |
| 4931 |
|
| 4932 |
|
| 4933 |
|
| 4934 |
|
| 4935 |
|
| 4936 |
|
| 4937 |
|
| 4938 |
|
| 4939 |
|
| 4940 |
|
| 4941 |
def generateFindTheLastElementLocation(tc): |
| 4942 |
do = doA if tc == "A" else doB |
| 4943 |
strSize = "SizeI" if tc == "A" else "SizeJ" |
| 4944 |
strMacroTile = "MacroTile0" if tc == "A" else "MacroTile1" |
| 4945 |
tP = tPA if tc == "A" else tPB |
| 4946 |
nlp = nlpA if tc == "A" else nlpB |
| 4947 |
nlc = nlcA if tc == "A" else nlcB |
| 4948 |
lsp = lspA if tc == "A" else lspB |
| 4949 |
lsc = lscA if tc == "A" else lscB |
| 4950 |
strWSGR = "WaveSeparateGlobalReadA" if tc == "A" else "WaveSeparateGlobalReadB" |
| 4951 |
maxNumOOBElements = maxNumOOBElementsA if tc == "A" else maxNumOOBElementsB |
| 4952 |
|
| 4953 |
sReloadFlag = singSgprList[2] if tc == "A" else singSgprList[3] |
| 4954 |
sTmp0 = singSgprList[0] if tc == "A" else singSgprList[1] |
| 4955 |
sTmp1 = singSgprList[4] if tc == "A" else singSgprList[5] |
| 4956 |
sTmp2 = singSgprList[6] if tc == "A" else singSgprList[7] |
| 4957 |
sTmp3 = singSgprList[8] if tc == "A" else singSgprList[9] |
| 4958 |
sx2Tmp0 = pairSgprList[0] |
| 4959 |
sx2Tmp1 = pairSgprList[1] |
| 4960 |
sLoadTileIdx = sTmp0 |
| 4961 |
sLoadNum = sTmp1 |
| 4962 |
sValidBytes = sTmp2 |
| 4963 |
|
| 4964 |
loopIdx = self.states.unrollIdx |
| 4965 |
if do: |
| 4966 |
imod.addComment("Calculate %s %% %s"%(strSize, strMacroTile)) |
| 4967 |
imod.add(scalarStaticDivideAndRemainder(sTmp0, sTmp0, sgpr(strSize), \ |
| 4968 |
kernel[strMacroTile], \ |
| 4969 |
ContinuousRegister(sx2Tmp0, 2), 1)) |
| 4970 |
imod.add(SCmpEQU32(src0=sgpr(sTmp0), src1=0, comment="")) |
| 4971 |
imod.add(SCMovB32(dst=sgpr(sTmp0), src=kernel[strMacroTile])) |
| 4972 |
imod.add(SAndB32(dst=sgpr(sTmp1), src0=sgpr("SizeL"), src1=(kernel["DepthU"] - 1), \ |
| 4973 |
comment="Calculate how many sizes along L direction in tail")) |
| 4974 |
imod.add(SLShiftRightB32(dst=sgpr(sx2Tmp1), shiftHex=hex(log2(lsc)), \ |
| 4975 |
src=sgpr(sTmp1), comment="Divided by lsc(%s)"%(lsc))) |
| 4976 |
imod.add(SMulI32(dst=sgpr(sTmp2), src0=sgpr(sTmp0), src1=sgpr(sTmp1), \ |
| 4977 |
comment="Calculate total valid elements number of last tile")) |
| 4978 |
imod.add(SMulI32(dst=sgpr(sValidBytes), src0=sgpr(sTmp2), src1=tP["bpeGR"], \ |
| 4979 |
comment="Total valid bytes")) |
| 4980 |
|
| 4981 |
if (kernel[strWSGR] == 0): |
| 4982 |
tmpSgpr = sTmp0 |
| 4983 |
else: |
| 4984 |
tmpSgpr = sTmp3 |
| 4985 |
imod.add(SSubU32(dst=sgpr(sTmp1), src0=sgpr(strSize), src1=1, \ |
| 4986 |
comment="sLoadTileIdx starts from 0")) |
| 4987 |
imod.addComment("Calculate %s - 1 %% %s"%(strSize, strMacroTile)) |
| 4988 |
imod.add(scalarStaticDivideAndRemainder(tmpSgpr, tmpSgpr, sTmp1, \ |
| 4989 |
kernel[strMacroTile], \ |
| 4990 |
ContinuousRegister(sx2Tmp0, 2), 1)) |
| 4991 |
if (kernel[strWSGR] > 0): |
| 4992 |
imod.addComment("Calculate %s %% (nlp * lsp)"%(tmpSgpr)) |
| 4993 |
imod.add(scalarStaticDivideAndRemainder(sTmp0, sTmp0, tmpSgpr, \ |
| 4994 |
(nlp * lsp), \ |
| 4995 |
ContinuousRegister(sx2Tmp0, 2), 1)) |
| 4996 |
imod.add(SLShiftRightB32(dst=sgpr(sTmp0), shiftHex=hex(log2(lsp)), \ |
| 4997 |
src=sgpr(sTmp0), comment="Divide lsp to get the load tile index")) |
| 4998 |
imod.add(SMulI32(dst=sgpr(sTmp0), src0=sgpr(sTmp0), src1=nlc, comment="Multiply nlc")) |
| 4999 |
imod.add(SAddI32(dst=sgpr(sLoadTileIdx), src0=sgpr(sTmp0), src1=sgpr(sx2Tmp1), \ |
| 5000 |
comment="")) |
| 5001 |
imod.add(scalarStaticDivideAndRemainder(sx2Tmp0, sTmp1, "SizesSum+%u"%loopIdx, \ |
| 5002 |
kernel["DepthU"], ContinuousRegister(sx2Tmp0, 2), \ |
| 5003 |
2)) |
| 5004 |
imod.add(SAndB32(dst=sgpr(sLoadNum), src0=sgpr(sTmp1), src1=(tP["glvw"] - 1), \ |
| 5005 |
comment="sLoadNum = (SizesSum+%u"%(loopIdx)+" mod DU) & glvw")) |
| 5006 |
imod.add(SAndB32(dst=sgpr(sReloadFlag), src0=sgpr(sLoadNum), src1=hex(maxNumOOBElements), \ |
| 5007 |
comment="")) |
| 5008 |
|
| 5009 |
|
| 5010 |
|
| 5011 |
|
| 5012 |
|
| 5013 |
|
| 5014 |
|
| 5015 |
|
| 5016 |
|
| 5017 |
|
| 5018 |
def generateLoadAndMergeSingleElement(): |
| 5019 |
def generateJumpBranch(idx): |
| 5020 |
def generateDetailsForBehavior(tP, tmpVgpr, behavior, jumpLabel, tileSgpr, kSgpr): |
| 5021 |
self.param = TailOptParams() |
| 5022 |
tc = tP["tensorChar"] |
| 5023 |
bpe = tP["bpeGR"] |
| 5024 |
numThreadsPerp = kernel[tP["lsp"]] |
| 5025 |
numThreadsCoal = int(kernel["NumThreads"] / numThreadsPerp) |
| 5026 |
nlp = kernel["NumLoadsPerpendicular%c"%(tc)] |
| 5027 |
nlc = kernel["NumLoadsCoalesced%c"%(tc)] |
| 5028 |
numTiles = nlp * nlc |
| 5029 |
numElementsPer4Bytes = int(4 / bpe) |
| 5030 |
def generateFunction(idx, bevavior, jumpLabel, tileSgpr, kSgpr): |
| 5031 |
tileLabelsList = [] |
| 5032 |
kLabelsList = [] |
| 5033 |
if (idx >= 0): |
| 5034 |
labelStr = bevavior + "_"+tc+str(idx) |
| 5035 |
labelTmp = Label(label=labelStr, comment="") |
| 5036 |
tileLabelsList.append(labelTmp) |
| 5037 |
if (idx != 0): |
| 5038 |
imod.add(SCmpEQU32(src0=sgpr(tileSgpr), src1=idx, comment="")) |
| 5039 |
imod.add(SCBranchSCC1(labelName=labelTmp.getLabelName(), comment="")) |
| 5040 |
|
| 5041 |
generateFunction(idx-1, bevavior, jumpLabel, tileSgpr, kSgpr) |
| 5042 |
|
| 5043 |
imod.add(labelTmp) |
| 5044 |
for i in range(tP["glvw"], 0, -1): |
| 5045 |
if i % numElementsPer4Bytes != 0: |
| 5046 |
labelStrK = labelStr+"_K"+str(i) |
| 5047 |
labelTmp = Label(label=labelStrK, comment="") |
| 5048 |
kLabelsList.append(labelTmp) |
| 5049 |
if idx == (numTiles - 1): |
| 5050 |
finalLoop = 1 |
| 5051 |
else: |
| 5052 |
finalLoop = 0 |
| 5053 |
|
| 5054 |
self.param.idx = idx |
| 5055 |
self.param.jumpLabel = jumpLabel |
| 5056 |
self.param.tmpVgpr = tmpVgpr |
| 5057 |
self.param.kLabelsList = kLabelsList |
| 5058 |
self.param.behavior = behavior |
| 5059 |
self.param.kSgpr = kSgpr |
| 5060 |
self.param.finalLoop = finalLoop |
| 5061 |
imod.add(self.globalReadGuardK(kernel, tP, 1, self.param)) |
| 5062 |
generateFunction(numTiles - 1, behavior, jumpLabel, tileSgpr, kSgpr) |
| 5063 |
|
| 5064 |
|
| 5065 |
tc = ["A", "B"] |
| 5066 |
do = [doA, doB] |
| 5067 |
tP = [tPA, tPB] |
| 5068 |
loadLabel = [loadALabel, loadBLabel] |
| 5069 |
sReloadFlag = [sReloadFlagA, sReloadFlagB] |
| 5070 |
sLoadTileIdx = [sLoadTileIdxA, sLoadTileIdxB] |
| 5071 |
sLoadNum = [sLoadNumA, sLoadNumB] |
| 5072 |
tmpVgprStartIdx = [0, (maxNumOOBElementsA * numDwordA)] |
| 5073 |
mergeLabel = [mergeALabel, mergeBLabel] |
| 5074 |
numElementsPerLoad = [numElementsPerLoadA, numElementsPerLoadB] |
| 5075 |
if (idx >= 0 and idx < len(tc)): |
| 5076 |
if tc[idx] == "A": |
| 5077 |
jumpLabel = loadBLabel if doA and doB else mergeALabel |
| 5078 |
else: |
| 5079 |
jumpLabel = mergeALabel if doA and doB else mergeBLabel |
| 5080 |
if do[idx]: |
| 5081 |
imod.addComment1("load single element for %s"%(tc[idx])) |
| 5082 |
imod.add(loadLabel[idx]) |
| 5083 |
imod.add(SCmpEQU32(src0=sgpr(sReloadFlag[idx]), src1=0, \ |
| 5084 |
comment="Valid loading size per thread is multiples of 4 bytes")) |
| 5085 |
imod.add(SCBranchSCC1(labelName=jumpLabel.getLabelName(), |
| 5086 |
comment="Skip loading "+tc[idx])) |
| 5087 |
generateDetailsForBehavior(tP[idx], tmpVgpr + tmpVgprStartIdx[idx], "LOAD", jumpLabel, |
| 5088 |
sLoadTileIdx[idx], sLoadNum[idx]) |
| 5089 |
|
| 5090 |
generateJumpBranch(idx+1) |
| 5091 |
|
| 5092 |
idx = idx ^ 1 |
| 5093 |
if do[idx]: |
| 5094 |
if tc[idx] == "A": |
| 5095 |
jumpLabel = mergeBLabel if doA and doB else checkOtherLoadALabel |
| 5096 |
else: |
| 5097 |
jumpLabel = checkOtherLoadALabel if doA and doB else checkOtherLoadBLabel |
| 5098 |
imod.addComment1("merge single element for %s"%(tc[idx])) |
| 5099 |
imod.add(mergeLabel[idx]) |
| 5100 |
imod.add(SCmpEQU32(src0=sgpr(sReloadFlag[idx]), src1=0, \ |
| 5101 |
comment="Valid loading size per thread is multiples of 4 bytes")) |
| 5102 |
imod.add(SCBranchSCC1(labelName=jumpLabel.getLabelName(), |
| 5103 |
comment="Skip mergeing "+tc[idx])) |
| 5104 |
generateDetailsForBehavior(tP[idx], tmpVgpr + tmpVgprStartIdx[idx], "MERGE", \ |
| 5105 |
jumpLabel, sLoadTileIdx[idx], sLoadNum[idx]) |
| 5106 |
generateJumpBranch(0) |
| 5107 |
|
| 5108 |
|
| 5109 |
|
| 5110 |
|
| 5111 |
def jumpLabel(tP, tmpSgpr, jumpLabel): |
| 5112 |
tc = tP["tensorChar"] |
| 5113 |
nlp = kernel["NumLoadsPerpendicular%c"%(tc)] |
| 5114 |
nlc = kernel["NumLoadsCoalesced%c"%(tc)] |
| 5115 |
numTiles = nlp * nlc |
| 5116 |
def func(idx, tmpSgpr): |
| 5117 |
tileLabelsList = [] |
| 5118 |
if (idx >= 0): |
| 5119 |
labelStr = tc+str(idx) |
| 5120 |
labelTmp = Label(label=labelStr, comment="") |
| 5121 |
tileLabelsList.append(labelTmp) |
| 5122 |
if (idx != 0): |
| 5123 |
imod.add(SCmpEQU32(src0=sgpr(tmpSgpr), src1=idx, comment="")) |
| 5124 |
imod.add(SCBranchSCC1(labelName=labelTmp.getLabelName(), comment="")) |
| 5125 |
func(idx-1, tmpSgpr) |
| 5126 |
imod.add(labelTmp) |
| 5127 |
if tc == "A": |
| 5128 |
addrbase = self.globalread_gpr_record.a.addrVgpr[idx] |
| 5129 |
offset = self.globalread_gpr_record.a.offset[idx] |
| 5130 |
else: |
| 5131 |
addrbase = self.globalread_gpr_record.b.addrVgpr[idx] |
| 5132 |
offset = self.globalread_gpr_record.b.offset[idx] |
| 5133 |
if kernel["_UseSgprForGRO"]: |
| 5134 |
imod.add(VAddU32(dst=vgpr(tmpVgpr), src0=vgpr(addrbase), src1=offset)) |
| 5135 |
else: |
| 5136 |
imod.add(VMovB32(dst=vgpr(tmpVgpr), src=vgpr(addrbase))) |
| 5137 |
if idx != (numTiles - 1): |
| 5138 |
imod.add(SBranch(labelName=jumpLabel.getLabelName(), comment="")) |
| 5139 |
func(numTiles - 1, tmpSgpr) |
| 5140 |
|
| 5141 |
|
| 5142 |
|
| 5143 |
|
| 5144 |
|
| 5145 |
|
| 5146 |
def generateReLoadLoop(tc): |
| 5147 |
do = doA if tc == "A" else doB |
| 5148 |
nlp = nlpA if tc == "A" else nlpB |
| 5149 |
nlc = nlcA if tc == "A" else nlcB |
| 5150 |
checkOtherLoadLabel = checkOtherLoadALabel if tc == "A" else checkOtherLoadBLabel |
| 5151 |
sLoadTileIdx = sLoadTileIdxA if tc == "A" else sLoadTileIdxB |
| 5152 |
tP = tPA if tc == "A" else tPB |
| 5153 |
checkAddrLabel = checkAddrALabel if tc == "A" else checkAddrBLabel |
| 5154 |
sValidBytes = sValidBytesA if tc == "A" else sValidBytesB |
| 5155 |
loadLabel = loadALabel if tc == "A" else loadBLabel |
| 5156 |
imod.add(checkOtherLoadLabel) |
| 5157 |
|
| 5158 |
|
| 5159 |
|
| 5160 |
|
| 5161 |
|
| 5162 |
|
| 5163 |
|
| 5164 |
|
| 5165 |
|
| 5166 |
if do: |
| 5167 |
if tc == "A": |
| 5168 |
imod.add(SCmpEQU32(src0=sgpr(sReloadFlagA), src1=0, |
| 5169 |
comment="Noneed to load single element fo A?")) |
| 5170 |
if doB: |
| 5171 |
imod.add(SCBranchSCC1(labelName=checkOtherLoadBLabel.getLabelName(), comment="")) |
| 5172 |
else: |
| 5173 |
imod.add(SCBranchSCC1(labelName=tailGlobalLoadEndLabel.getLabelName(), comment="")) |
| 5174 |
imod.add(SAddU32(dst=sgpr(sLoadCnt), src0=sgpr(sLoadCnt), src1=nlc)) |
| 5175 |
imod.add(SCmpEQU32(src0=sgpr(sLoadCnt), src1=(nlp * nlc), |
| 5176 |
comment="Have reloaded all subtiles?")) |
| 5177 |
if doB: |
| 5178 |
imod.add(SCMovB32(dst=sgpr(sLoadCnt), src=0, comment="Reset loop count")) |
| 5179 |
imod.add(SCBranchSCC1(labelName=checkOtherLoadBLabel.getLabelName(), comment="")) |
| 5180 |
else: |
| 5181 |
imod.add(SCBranchSCC1(labelName=tailGlobalLoadEndLabel.getLabelName(), comment="")) |
| 5182 |
else: |
| 5183 |
if doA: |
| 5184 |
imod.add(SMovB32(dst=sgpr(sReloadFlagA), src=0, comment="Force to skip reload A")) |
| 5185 |
imod.add(SCmpEQU32(src0=sgpr(sLoadCnt), src1=0, comment="Loop start?")) |
| 5186 |
imod.add(SCMovB32(dst=sgpr(sReloadFlagB), src=sgpr(sBackupSkipLoadB), |
| 5187 |
comment="Restore sReloadFlagB for B")) |
| 5188 |
imod.add(SCmpEQU32(src0=sgpr(sReloadFlagB), src1=0, |
| 5189 |
comment="Noneed to load single element for B?")) |
| 5190 |
imod.add(SCBranchSCC1(labelName=tailGlobalLoadEndLabel.getLabelName(), comment="")) |
| 5191 |
imod.add(SAddU32(dst=sgpr(sLoadCnt), src0=sgpr(sLoadCnt), src1=nlc)) |
| 5192 |
imod.add(SCmpEQU32(src0=sgpr(sLoadCnt), src1=(nlp * nlc), |
| 5193 |
comment="Have reloaded all subtiles?")) |
| 5194 |
imod.add(SCBranchSCC1(labelName=tailGlobalLoadEndLabel.getLabelName(), comment="")) |
| 5195 |
|
| 5196 |
imod.add(SSubI32(dst=sgpr(sLoadTileIdx), src0=sgpr(sLoadTileIdx), src1=nlc, \ |
| 5197 |
comment="Check the upper subtile")) |
| 5198 |
imod.add(SCmpLtI32(src0=sgpr(sLoadTileIdx), src1=0, comment="")) |
| 5199 |
imod.add(SCSelectB32(dst=sgpr(sCmpLoadStartAddrStatusx2), src0=(nlp * nlc), src1=0, \ |
| 5200 |
comment="Back to the last subtile")) |
| 5201 |
imod.add(SAddI32(dst=sgpr(sLoadTileIdx), src0= sgpr(sLoadTileIdx), |
| 5202 |
src1=sgpr(sCmpLoadStartAddrStatusx2), \ |
| 5203 |
comment="If currently reload the first subtile, \ |
| 5204 |
check the last subtile next.")) |
| 5205 |
jumpLabel(tP, sLoadTileIdx, checkAddrLabel) |
| 5206 |
imod.add(checkAddrLabel) |
| 5207 |
imod.add(VSubU32(dst=vgpr(tmpVgpr), src0=vgpr(tmpVgpr), |
| 5208 |
src1=self.states.srdShiftLeft[tc] * tP["bpeGR"], comment="sub prepad")) |
| 5209 |
loadRangePerThread = tP["glvw"] * tP["bpeGR"] - 1 |
| 5210 |
imod.add(VAddU32(dst=vgpr(tmpVgpr+1), src0=vgpr(tmpVgpr), src1=loadRangePerThread, \ |
| 5211 |
comment="Calculate load range per thread")) |
| 5212 |
imod.add(VCmpLtI32(dst=sgpr(sCmpLoadStartAddrStatusx2, 2), src0=vgpr(tmpVgpr), \ |
| 5213 |
src1=sgpr(sValidBytes), \ |
| 5214 |
comment="If loading start address < total valid bytes?")) |
| 5215 |
imod.add(VCmpGEI32(dst=sgpr(sCmpLoadEndAddrStatusx2, 2), src0=vgpr(tmpVgpr+1), \ |
| 5216 |
src1=sgpr(sValidBytes), \ |
| 5217 |
comment="If loading end address >= total valid bytes?")) |
| 5218 |
imod.add(SAndB32(dst=sgpr(sCmpLoadStartAddrStatusx2), \ |
| 5219 |
src0=sgpr(sCmpLoadStartAddrStatusx2), \ |
| 5220 |
src1=sgpr(sCmpLoadEndAddrStatusx2), \ |
| 5221 |
comment="Find threads which access the last element")) |
| 5222 |
imod.add(SAndB32(dst=sgpr(sCmpLoadStartAddrStatusx2+1), \ |
| 5223 |
src0=sgpr(sCmpLoadStartAddrStatusx2+1), \ |
| 5224 |
src1=sgpr(sCmpLoadEndAddrStatusx2+1), \ |
| 5225 |
comment="Find thread that access the last element")) |
| 5226 |
imod.add(SAddU32(dst=sgpr(sCmpLoadStartAddrStatusx2), \ |
| 5227 |
src0=sgpr(sCmpLoadStartAddrStatusx2), \ |
| 5228 |
src1=sgpr(sCmpLoadStartAddrStatusx2+1), \ |
| 5229 |
comment="Find thread that access the last element")) |
| 5230 |
imod.add(SCmpLgU32(src0=sgpr(sCmpLoadStartAddrStatusx2), src1=0, \ |
| 5231 |
comment="Have threads access the last element?")) |
| 5232 |
if tc == "A": |
| 5233 |
if doB: |
| 5234 |
imod.add(SCMovB32(dst=sgpr(sReloadFlagB), src=0, comment="Skip reload B temporarily")) |
| 5235 |
imod.add(SCSelectB32(dst=sgpr(sLoadCnt), src0=sgpr(sLoadCnt), src1=0, \ |
| 5236 |
comment="Reset loop count if needed")) |
| 5237 |
imod.add(SCBranchSCC1(labelName=loadLabel.getLabelName(), comment="Reload "+tc)) |
| 5238 |
|
| 5239 |
|
| 5240 |
|
| 5241 |
sLoadTileIdxA = singSgprList[0] |
| 5242 |
sLoadTileIdxB = singSgprList[1] |
| 5243 |
sReloadFlagA = singSgprList[2] |
| 5244 |
sReloadFlagB = singSgprList[3] |
| 5245 |
sLoadNumA = singSgprList[4] |
| 5246 |
sLoadNumB = singSgprList[5] |
| 5247 |
sValidBytesA = singSgprList[6] |
| 5248 |
sValidBytesB = singSgprList[7] |
| 5249 |
sLoadCnt = singSgprList[8] |
| 5250 |
sBackupSkipLoadB = singSgprList[9] |
| 5251 |
sCmpLoadStartAddrStatusx2 = pairSgprList[0] |
| 5252 |
sCmpLoadEndAddrStatusx2 = pairSgprList[1] |
| 5253 |
|
| 5254 |
imod.addComment1("find the last element location for a") |
| 5255 |
generateFindTheLastElementLocation("A") |
| 5256 |
imod.addComment1("find the last element location for b") |
| 5257 |
generateFindTheLastElementLocation("B") |
| 5258 |
|
| 5259 |
if doA or doB: |
| 5260 |
tmpVgpr = self.vgprPool.checkOut(numTmpVgpr) |
| 5261 |
imod.add(SMovB32(dst=sgpr(sLoadCnt), src=0, comment="Set loop count = 0")) |
| 5262 |
if doA and doB: |
| 5263 |
imod.add(SMovB32(dst=sgpr(sBackupSkipLoadB), src=sgpr(sReloadFlagB), \ |
| 5264 |
comment="Backup and will be restored in label_CheckB_OOB")) |
| 5265 |
|
| 5266 |
generateLoadAndMergeSingleElement() |
| 5267 |
|
| 5268 |
imod.addComment1("reload loop for a: check if there's other load range need to be reloaded") |
| 5269 |
generateReLoadLoop("A") |
| 5270 |
imod.addComment1("reload loop for b: check if there's other load range need to be reloaded") |
| 5271 |
generateReLoadLoop("B") |
| 5272 |
|
| 5273 |
imod.addComment1("global read for tail done") |
| 5274 |
imod.add(tailGlobalLoadEndLabel) |
| 5275 |
|
| 5276 |
if (doA and kernel["DirectToLds%s"%tPA["tensorChar"]]) or \ |
| 5277 |
(doB and kernel["DirectToLds%s"%tPB["tensorChar"]]): |
| 5278 |
imod.add(SMovB32(dst=mgpr(0), src=hex(kernel["LdsNumBytes"]), \ |
| 5279 |
comment="Restore LDS clamp at %u bytes HERE"%(kernel["LdsNumBytes"]))) |
| 5280 |
|
| 5281 |
if doA or doB: |
| 5282 |
self.vgprPool.checkIn(tmpVgpr) |
| 5283 |
for s in singSgprList: |
| 5284 |
self.sgprPool.checkIn(s) |
| 5285 |
for s in pairSgprList: |
| 5286 |
self.sgprPool.checkIn(s) |
| 5287 |
|
| 5288 |
return imod |
| 5289 |
|
| 5290 |
|
| 5291 |
|
| 5292 |
|
| 5293 |
|
| 5294 |
|
| 5295 |
|
| 5296 |
|
| 5297 |
|
| 5298 |
|
| 5299 |
|
| 5300 |
|
| 5301 |
|
| 5302 |
|
| 5303 |
def calculateLoopNumIterGsu(self, kernel, destName, tmpSgprRes: ContinuousRegister): |
| 5304 |
module = Module("calculateLoopNumIterGsu") |
| 5305 |
|
| 5306 |
loopCounter = sgpr(destName) |
| 5307 |
quotient = destName |
| 5308 |
remainder = "GSUSumIdx+1" |
| 5309 |
dividend = destName |
| 5310 |
|
| 5311 |
tmpVgpr = self.vgprPool.checkOut(2,"tmp") |
| 5312 |
tmpVgprRes = ContinuousRegister(idx=tmpVgpr, size=2) |
| 5313 |
module.add(SAndB32(dst=sgpr(remainder), src0=sgpr("GSU"), src1=hex(0x3FFF), comment="Restore GSU")) |
| 5314 |
module.add(scalarUInt32DivideAndRemainder(quotient, dividend, remainder, remainder, tmpVgprRes, wavewidth=kernel["WavefrontSize"])) |
| 5315 |
self.vgprPool.checkIn(tmpVgpr) |
| 5316 |
|
| 5317 |
|
| 5318 |
module.add(SAddU32(dst=sgpr(tmpSgprRes.idx), src0=1, src1=loopCounter, comment="tmp<-numIterMyWg+1")) |
| 5319 |
module.add(SCmpLtU32(src0=sgpr("GSUSumIdx"), src1=sgpr("GSUSumIdx+1"), comment="gsuSumIdx < numIterPerWgRemainder")) |
| 5320 |
module.add(SCMovB32(dst=loopCounter, src=sgpr(tmpSgprRes.idx), comment="numIterMyWg++ if needed")) |
| 5321 |
|
| 5322 |
return module |
| 5323 |
|
| 5324 |
def calculateLoopNumIterOffsetGsu(self, kernel, destName, tmpSgprRes: ContinuousRegister): |
| 5325 |
module = Module("calculateLoopNumIterOffsetGsu") |
| 5326 |
|
| 5327 |
loopCounter = sgpr(destName) |
| 5328 |
quotient = destName |
| 5329 |
remainder = "GSUSumIdx+1" |
| 5330 |
dividend = destName |
| 5331 |
|
| 5332 |
tmpVgpr = self.vgprPool.checkOut(2,"tmp") |
| 5333 |
tmpVgprRes = ContinuousRegister(idx=tmpVgpr, size=2) |
| 5334 |
module.add(SAndB32(dst=sgpr(remainder), src0=sgpr("GSU"), src1=hex(0x3FFF), comment="Restore GSU")) |
| 5335 |
module.add(scalarUInt32DivideAndRemainder(quotient, dividend, remainder, remainder, tmpVgprRes, wavewidth=kernel["WavefrontSize"])) |
| 5336 |
self.vgprPool.checkIn(tmpVgpr) |
| 5337 |
|
| 5338 |
|
| 5339 |
stmp = tmpSgprRes.idx |
| 5340 |
module.add(SMulI32(dst=sgpr(stmp+1), src0=loopCounter, src1=sgpr("GSUSumIdx"), comment="quotient*GSUSumIdx")) |
| 5341 |
module.add(SAddU32(dst=sgpr(stmp+0), src0=1, src1=loopCounter, comment="quotient+1")) |
| 5342 |
module.add(SAddU32(dst=sgpr(stmp+1), src0=sgpr(stmp+1), src1=sgpr("GSUSumIdx+1"), comment="quotient*GSUSumIdx+remainder")) |
| 5343 |
module.add(SMulI32(dst=sgpr(stmp+0), src0=sgpr(stmp+0), src1=sgpr("GSUSumIdx"), comment="(quotient+1)*GSUSumIdx")) |
| 5344 |
|
| 5345 |
module.add(SCmpLtU32(src0=sgpr("GSUSumIdx"), src1=sgpr("GSUSumIdx+1"), comment="gsuSumIdx < numIterPerWgRemainder")) |
| 5346 |
module.add(SCSelectB32(dst=sgpr(stmp+0), src0=sgpr(stmp+0), src1=sgpr(stmp+1), comment="(quotient+1)*GSUSumIdx if needed")) |
| 5347 |
|
| 5348 |
return module |
| 5349 |
|
| 5350 |
|
| 5351 |
|
| 5352 |
|
| 5353 |
|
| 5354 |
|
| 5355 |
|
| 5356 |
def calculateLoopNumIter(self, kernel, tPA, tPB, loopIdx): |
| 5357 |
module = Module("calculateLoopNumIter") |
| 5358 |
|
| 5359 |
tailLoop = loopIdx < 0 |
| 5360 |
if tailLoop: |
| 5361 |
loopIdx = self.states.unrollIdx |
| 5362 |
loopDim = kernel["ProblemType"]["IndicesSummation"][loopIdx] |
| 5363 |
loopChar = self.states.indexChars[loopDim] |
| 5364 |
|
| 5365 |
|
| 5366 |
|
| 5367 |
if tailLoop: |
| 5368 |
with self.allocTmpSgpr(4) as tmpSgprInfo: |
| 5369 |
tmpSgpr = tmpSgprInfo.idx |
| 5370 |
loopCounterName = self.loopCounterName(kernel, loopIdx) |
| 5371 |
module.addSpaceLine() |
| 5372 |
if kernel["SuppressNoLoadLoop"]: |
| 5373 |
|
| 5374 |
|
| 5375 |
|
| 5376 |
assert(kernel["PrefetchGlobalRead"] == 1) |
| 5377 |
module.add(SCmpEQU32(src0=sgpr("OrigLoopCounter"), src1=0, comment="completely skipped unroll loop?")) |
| 5378 |
module.add(SCSelectB32(dst=sgpr(tmpSgpr+0), src0=0, src1=sgpr("GlobalReadIncsA"), comment="force to 0?")) |
| 5379 |
module.add(SCSelectB32(dst=sgpr(tmpSgpr+1), src0=0, src1=sgpr("GlobalReadIncsB"), comment="force to 0?")) |
| 5380 |
module.add(self.setTailSrd(tPA, sgpr(tmpSgpr+0))) |
| 5381 |
module.addSpaceLine() |
| 5382 |
module.add(self.setTailSrd(tPB, sgpr(tmpSgpr+1))) |
| 5383 |
module.addSpaceLine() |
| 5384 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 5385 |
module.add(SCSelectB32(dst=sgpr(tmpSgpr+0), src0=0, src1=sgpr("GlobalReadIncsMetadata"), comment="force to 0?")) |
| 5386 |
tP = tPB if kernel["ProblemType"]["Sparse"] == 2 else tPA |
| 5387 |
module.add(self.setTailSrd(tP, sgpr(tmpSgpr+0))) |
| 5388 |
module.addSpaceLine() |
| 5389 |
|
| 5390 |
module.addComment("numIter%s = LOCAL_SPLITU * min(size%s %% LOCAL_DEPTHU, DEPTHU / LOCAL_SPLITU)" \ |
| 5391 |
% (self.states.unrollChar, self.states.unrollChar)) |
| 5392 |
|
| 5393 |
|
| 5394 |
module.add(scalarStaticDivideAndRemainder(tmpSgpr, loopCounterName, \ |
| 5395 |
"SizesSum+%u"%loopIdx, kernel["DepthU"], ContinuousRegister(tmpSgpr+2, 2), 2)) |
| 5396 |
loopCounter = sgpr(loopCounterName) |
| 5397 |
|
| 5398 |
if kernel["LocalSplitU"] > 1: |
| 5399 |
|
| 5400 |
|
| 5401 |
|
| 5402 |
|
| 5403 |
|
| 5404 |
|
| 5405 |
|
| 5406 |
dividend = tmpSgpr+2 |
| 5407 |
tmpVgpr = self.vgprPool.checkOutAligned(2, 2,"tmpVgpr") |
| 5408 |
tmpVgprRes = ContinuousRegister(tmpVgpr, 2) |
| 5409 |
wave_id = self.vgprPool.checkOut(1) |
| 5410 |
numWaves = kernel["MIWaveGroup"][0] * kernel["MIWaveGroup"][1] |
| 5411 |
|
| 5412 |
module.add(SMovB32(dst=sgpr(dividend), src=hex(kernel["DepthU"]//kernel["LocalSplitU"]), comment="DEPTHU / LOCAL_SPLITU" )) |
| 5413 |
module.add(vectorStaticDivide(wave_id, "Serial", kernel["WavefrontSize"], tmpVgprRes)) |
| 5414 |
module.add(vectorStaticDivide(wave_id, wave_id, numWaves, tmpVgprRes, comment="LSU offset: Get LSU wave_id")) |
| 5415 |
|
| 5416 |
module.add(VMulLOU32(vgpr(wave_id), sgpr(dividend), vgpr(wave_id), comment="multiply by (DEPTHU / LOCAL_SPLITU)")) |
| 5417 |
module.add(SNop(waitState=0, comment="Wait to read lane")) |
| 5418 |
module.add(VReadfirstlaneB32(dst=sgpr("LSUTailLoopOffset"), src=vgpr(wave_id), comment="Update Alpha")) |
| 5419 |
module.add(SSubI32( |
| 5420 |
dst=sgpr("LSUTailLoopOffset"), \ |
| 5421 |
src0=loopCounter, \ |
| 5422 |
src1=sgpr("LSUTailLoopOffset"), \ |
| 5423 |
comment="lsu offset" )) |
| 5424 |
module.add(SMinU32(dst=loopCounter, src0=sgpr(dividend), src1=loopCounter, comment="" )) |
| 5425 |
self.vgprPool.checkIn(wave_id) |
| 5426 |
self.vgprPool.checkIn(tmpVgpr) |
| 5427 |
|
| 5428 |
skComponent = Component.StreamK.find(self) |
| 5429 |
module.add(skComponent.tailLoopNumIter(self, kernel, loopCounter)) |
| 5430 |
|
| 5431 |
gsuComponent = Component.GSU.find(self) |
| 5432 |
module.add(gsuComponent.tailLoopNumIter(self, kernel, loopCounter)) |
| 5433 |
|
| 5434 |
|
| 5435 |
skipTailLoopLabel = Label.getFormatting("SkipTailLoop%s"%(loopChar) ) |
| 5436 |
module.add(SCmpEQU32(src0=loopCounter, src1=0, comment="numIter%s == 0"%loopChar )) |
| 5437 |
module.add(SMovB32(dst=sgpr("OrigLoopCounter"), src=0, comment="repurpose to count each localRead increment")) |
| 5438 |
module.add(SCBranchSCC1(labelName=skipTailLoopLabel, \ |
| 5439 |
comment="skip to end of tail loop b/c numIter==0")) |
| 5440 |
|
| 5441 |
|
| 5442 |
|
| 5443 |
elif loopIdx == self.states.unrollIdx: |
| 5444 |
loopCounterName = self.loopCounterName(kernel, loopIdx) |
| 5445 |
loopCounter = sgpr(loopCounterName) |
| 5446 |
if not self.do["PreLoop"]: module.add(ValueEndif()) |
| 5447 |
|
| 5448 |
with self.allocTmpSgpr(3) as tmpSgprInfo: |
| 5449 |
skComponent = Component.StreamK.find(self) |
| 5450 |
module.add(skComponent.calculateLoopNumIter(self, kernel, loopCounterName, loopIdx, tmpSgprInfo)) |
| 5451 |
|
| 5452 |
gsuComponent = Component.GSU.find(self) |
| 5453 |
module.add(gsuComponent.calculateLoopNumIter(self, kernel, loopCounterName, tmpSgprInfo)) |
| 5454 |
|
| 5455 |
module.add(SMovB32(dst=sgpr("OrigLoopCounter"), \ |
| 5456 |
src=loopCounter, \ |
| 5457 |
comment="copy loop counter")) |
| 5458 |
else: |
| 5459 |
|
| 5460 |
|
| 5461 |
module.addComment1("%sother summation, numIter%s = size%s" \ |
| 5462 |
% (self.indent, loopChar, loopChar)) |
| 5463 |
loopCounter = self.loopCounter(kernel, loopIdx) |
| 5464 |
module.add(SMovB32(dst=loopCounter, \ |
| 5465 |
src=sgpr("SizesSum+%u"%loopIdx), \ |
| 5466 |
comment="init loop counter")) |
| 5467 |
|
| 5468 |
return module |
| 5469 |
|
| 5470 |
|
| 5471 |
|
| 5472 |
|
| 5473 |
def calculateIncrementMetadata(self, kernel, sgprOut): |
| 5474 |
module = Module("calculateIncrementMetadata") |
| 5475 |
gsuComponent = Component.GSU.find(self) |
| 5476 |
module.add(gsuComponent.calculateIncrementMetadata(self, kernel, sgprOut)) |
| 5477 |
return module |
| 5478 |
|
| 5479 |
|
| 5480 |
|
| 5481 |
|
| 5482 |
def openLoop(self, kernel, tPA, tPB, loopIdx, noLabelGen=False, beginLabelOnly=False): |
| 5483 |
module = Module("openLoop") |
| 5484 |
|
| 5485 |
tailLoop = loopIdx < 0 |
| 5486 |
if tailLoop: |
| 5487 |
loopIdx = self.states.unrollIdx |
| 5488 |
self.states.inTailLoop = True |
| 5489 |
loopChar = self.states.indexChars[ \ |
| 5490 |
kernel["ProblemType"]["IndicesSummation"][loopIdx]] |
| 5491 |
if not tailLoop and not noLabelGen: |
| 5492 |
module.add(Label("openLoop%s"%loopChar, "")) |
| 5493 |
loopLabelBegin = Label("%sLoopBegin%s"%("Tail" if tailLoop else "", loopChar), "" ) |
| 5494 |
loopLabelEnd = Label("%sLoopEnd%s"%("Tail" if tailLoop else "", loopChar), "" ) |
| 5495 |
|
| 5496 |
if beginLabelOnly: |
| 5497 |
|
| 5498 |
module.add(loopLabelBegin) |
| 5499 |
return module |
| 5500 |
|
| 5501 |
|
| 5502 |
|
| 5503 |
|
| 5504 |
loopCounter = self.loopCounter(kernel, loopIdx) |
| 5505 |
if tailLoop: |
| 5506 |
endCounter = 0 |
| 5507 |
elif kernel["PrefetchGlobalRead"] == 1: |
| 5508 |
if kernel["SuppressNoLoadLoop"]: |
| 5509 |
endCounter = 0 |
| 5510 |
else: |
| 5511 |
endCounter = 1 |
| 5512 |
elif kernel["PrefetchGlobalRead"] == 2: |
| 5513 |
if kernel["SuppressNoLoadLoop"]: |
| 5514 |
endCounter = 1 |
| 5515 |
else: |
| 5516 |
endCounter = 2 |
| 5517 |
else: |
| 5518 |
endCounter = 0 |
| 5519 |
|
| 5520 |
if tailLoop: |
| 5521 |
|
| 5522 |
if not noLabelGen: |
| 5523 |
module.add(loopLabelBegin) |
| 5524 |
|
| 5525 |
else: |
| 5526 |
|
| 5527 |
if loopIdx == self.states.unrollIdx: |
| 5528 |
|
| 5529 |
if kernel["PrefetchGlobalRead"] == 2 and kernel["AssertSummationElementMultiple"] % (kernel["DepthU"] * 2) != 0: |
| 5530 |
module.add(SCmpEQU32( |
| 5531 |
src0=loopCounter, \ |
| 5532 |
src1=hex(endCounter-1), \ |
| 5533 |
comment="LoopCounter%s < EndCounter"%(loopChar) )) |
| 5534 |
toPGR1 = Label.getFormatting(self.labels.getName("toPGR1")) |
| 5535 |
module.add(SCBranchSCC1(labelName=toPGR1, comment="PGR=2 but only 1 loop, toPGR1")) |
| 5536 |
|
| 5537 |
module.add(SCmpLeU32( |
| 5538 |
src0=loopCounter, \ |
| 5539 |
src1=hex(endCounter), \ |
| 5540 |
comment="LoopCounter%s < EndCounter"%(loopChar) )) |
| 5541 |
jumpLabel = loopLabelEnd |
| 5542 |
if kernel["PrefetchGlobalRead"]==2 and (not kernel["SuppressNoLoadLoop"]) and kernel["ExpandPointerSwap"]: |
| 5543 |
|
| 5544 |
jumpLabel = Label("LoopEnd%s_evenexit"%(loopChar), "" ) |
| 5545 |
module.add(SCBranchSCC1(labelName=jumpLabel.getLabelName(), \ |
| 5546 |
comment="do not enter Loop%s"%loopChar )) |
| 5547 |
|
| 5548 |
if kernel["ExpertSchedulingMode"] > 0: |
| 5549 |
expertSchedulingMode = int(kernel["ExpertSchedulingMode"]) |
| 5550 |
module.add(SSetRegIMM32B32(dst=HWRegContainer(reg="26", value=[0,2]), src=expertSchedulingMode, comment="disable conservative hardware dependency checking to allow scheduling by software")) |
| 5551 |
|
| 5552 |
if not noLabelGen: |
| 5553 |
module.add(loopLabelBegin) |
| 5554 |
|
| 5555 |
if loopIdx != self.states.unrollIdx: |
| 5556 |
|
| 5557 |
if kernel["PrefetchGlobalRead"]: |
| 5558 |
module.addComment0("openLoop - reset LRO for possible odd-iter exit") |
| 5559 |
module.add(self.localReadResetOffsets(kernel, tPA)) |
| 5560 |
module.add(self.localReadResetOffsets(kernel, tPB)) |
| 5561 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 5562 |
tPM = tPA["tpsMetadata"] if tPA["is_sparse"] else tPB["tpsMetadata"] |
| 5563 |
module.add(self.localReadResetOffsets(kernel, tPM)) |
| 5564 |
|
| 5565 |
return module |
| 5566 |
|
| 5567 |
|
| 5568 |
|
| 5569 |
|
| 5570 |
|
| 5571 |
def closeLoop(self, kernel, tPA, tPB, loopIdx, finalLoop, emitEndLabelOnly=False, oddLabel=False, skipCondJumpCounter=-1): |
| 5572 |
module = Module("closeLoop") |
| 5573 |
if emitEndLabelOnly: |
| 5574 |
loopIdx = self.states.unrollIdx |
| 5575 |
loopChar = self.states.indexChars[ \ |
| 5576 |
kernel["ProblemType"]["IndicesSummation"][loopIdx]] |
| 5577 |
module.add(Label("SkipTailLoop%s"%(loopChar), "")) |
| 5578 |
return module |
| 5579 |
|
| 5580 |
tPM = tPA["tpsMetadata"] if tPA["is_sparse"] else tPB["tpsMetadata"] |
| 5581 |
|
| 5582 |
finalJump = SCBranchSCC0 |
| 5583 |
nonFinalJumpNeeded = True |
| 5584 |
|
| 5585 |
tailLoop = loopIdx < 0 |
| 5586 |
if tailLoop: |
| 5587 |
loopIdx = self.states.unrollIdx |
| 5588 |
loopChar = self.states.indexChars[kernel["ProblemType"]["IndicesSummation"][loopIdx]] |
| 5589 |
loopLabelBegin = Label("TailLoopBegin%s"%(loopChar), "" ) |
| 5590 |
loopLabelEnd = Label("TailLoopEnd%s"%(loopChar), "" ) |
| 5591 |
loopLabelEndOddExit = Label("TailLoopEnd%s_oddexit"%(loopChar), "unroll loop odditer exit" ) |
| 5592 |
loopCounter = self.loopCounter(kernel, loopIdx) |
| 5593 |
numReadsIterCoalescedA = self.states.numReadsIterCoalescedA |
| 5594 |
numReadsIterCoalescedB = self.states.numReadsIterCoalescedB |
| 5595 |
numReadsIterCoalesced = max(numReadsIterCoalescedA, numReadsIterCoalescedB) |
| 5596 |
|
| 5597 |
unrollInc = 1 |
| 5598 |
KinInnerUnroll = kernel["InnerUnroll"] |
| 5599 |
|
| 5600 |
if kernel["UseDotInstruction"]: |
| 5601 |
unrollInc *= kernel["NumDotElements"] * kernel["NumWaveSplitK"] |
| 5602 |
else: |
| 5603 |
if kernel["EnableMatrixInstruction"]: |
| 5604 |
unrollInc *= kernel["MatrixInstK"] * numReadsIterCoalesced |
| 5605 |
KinInnerUnroll *= kernel["MatrixInstK"] |
| 5606 |
if kernel["AssertSummationElementMultiple"] % KinInnerUnroll == 0: |
| 5607 |
unrollInc *= kernel["InnerUnroll"] |
| 5608 |
|
| 5609 |
skipCondJump = False |
| 5610 |
if skipCondJumpCounter >= 0 and (skipCondJumpCounter%numReadsIterCoalesced < numReadsIterCoalesced - 1): |
| 5611 |
|
| 5612 |
|
| 5613 |
|
| 5614 |
|
| 5615 |
|
| 5616 |
|
| 5617 |
skipCondJump = True |
| 5618 |
nonFinalJumpNeeded = not skipCondJump |
| 5619 |
if not skipCondJump: |
| 5620 |
module.addComment1("closeLoop loop%s finalLoop=%d tailLoop=%d" % (loopChar, finalLoop, tailLoop)) |
| 5621 |
|
| 5622 |
module.add(SSubI32( |
| 5623 |
dst=loopCounter, \ |
| 5624 |
src0=loopCounter, \ |
| 5625 |
src1=hex(unrollInc), \ |
| 5626 |
comment="dec counter%s (tailLoop)"%(loopChar) )) |
| 5627 |
|
| 5628 |
|
| 5629 |
module.add(SAddU32( |
| 5630 |
dst=sgpr("OrigLoopCounter"), \ |
| 5631 |
src0=sgpr("OrigLoopCounter"), \ |
| 5632 |
src1=hex(unrollInc), |
| 5633 |
comment="inc counter%s"%(loopChar) )) |
| 5634 |
|
| 5635 |
endCounter = 0 |
| 5636 |
if kernel["LocalSplitU"] > 1: |
| 5637 |
module.add(SSubI32( |
| 5638 |
dst=sgpr("LSUTailLoopOffset"), \ |
| 5639 |
src0=sgpr("LSUTailLoopOffset"), \ |
| 5640 |
src1=hex(unrollInc), \ |
| 5641 |
comment="LSU offset dec counter%s (tailLoop)"%(loopChar) )) |
| 5642 |
tmp = self.sgprPool.checkOut(1) |
| 5643 |
module.add(SMinI32(dst=sgpr(tmp), src0=loopCounter, src1=sgpr("LSUTailLoopOffset"), comment="check lsu offset too")) |
| 5644 |
module.add(SCmpLeI32( |
| 5645 |
src0=sgpr(tmp), \ |
| 5646 |
src1=hex(endCounter), \ |
| 5647 |
comment="counter%s<=%d"%(loopChar,endCounter) )) |
| 5648 |
self.sgprPool.checkIn(tmp) |
| 5649 |
else: |
| 5650 |
module.add(SCmpLeI32( |
| 5651 |
src0=loopCounter, \ |
| 5652 |
src1=hex(endCounter), \ |
| 5653 |
comment="counter%s<=%d"%(loopChar,endCounter) )) |
| 5654 |
else: |
| 5655 |
loopChar = self.states.indexChars[ \ |
| 5656 |
kernel["ProblemType"]["IndicesSummation"][loopIdx]] |
| 5657 |
loopLabelBegin = Label("LoopBegin%s"%(loopChar), "" ) |
| 5658 |
loopLabelEnd = Label("LoopEnd%s"%(loopChar), "" ) |
| 5659 |
loopLabelEndOddExit = Label("LoopEnd%s_oddexit"%(loopChar), "unroll loop odditer exit" ) |
| 5660 |
loopLabelEndEvenExit = Label("LoopEnd%s_evenexit"%(loopChar), "unroll loop eveniter exit" ) |
| 5661 |
loopCounter = self.loopCounter(kernel, loopIdx) |
| 5662 |
module.addComment1("closeLoop loop%s finalLoop=%d tailLoop=%d" % (loopChar, finalLoop, tailLoop)) |
| 5663 |
|
| 5664 |
|
| 5665 |
|
| 5666 |
|
| 5667 |
|
| 5668 |
|
| 5669 |
|
| 5670 |
|
| 5671 |
|
| 5672 |
if kernel["PrefetchGlobalRead"] == 1 and not kernel["SuppressNoLoadLoop"] and loopIdx == self.states.unrollIdx: |
| 5673 |
endCounter = 1 |
| 5674 |
elif kernel["PrefetchGlobalRead"] == 2 and not kernel["SuppressNoLoadLoop"] and loopIdx == self.states.unrollIdx: |
| 5675 |
endCounter = 2 |
| 5676 |
else: |
| 5677 |
endCounter = 0 |
| 5678 |
|
| 5679 |
if kernel["AssertSummationElementMultiple"] % (kernel["DepthU"] * 2) == 0 and endCounter > 0: |
| 5680 |
|
| 5681 |
|
| 5682 |
|
| 5683 |
|
| 5684 |
|
| 5685 |
|
| 5686 |
if kernel["PrefetchGlobalRead"]==2: |
| 5687 |
with self.allocTmpSgpr(2) as tmpSgprInfo: |
| 5688 |
tmpSgpr = tmpSgprInfo.idx |
| 5689 |
module.add(SCmpEQU32(src0=sgpr("StaggerU"), src1=0)) |
| 5690 |
module.add(SCSelectB32(dst=sgpr(tmpSgpr), src0=hex(2), src1=hex(1))) |
| 5691 |
decCode = SSubU32(dst=loopCounter, src0=loopCounter, \ |
| 5692 |
src1=sgpr(tmpSgpr), \ |
| 5693 |
comment="dec counter%s"%(loopChar) ) |
| 5694 |
else: |
| 5695 |
decCode = SSubU32(dst=loopCounter, src0=loopCounter, \ |
| 5696 |
src1=1, \ |
| 5697 |
comment="dec counter%s"%(loopChar) ) |
| 5698 |
condCode = SCmpEQI32(src0=loopCounter, \ |
| 5699 |
src1=hex(endCounter), \ |
| 5700 |
comment="counter%s==%d"%(loopChar,endCounter) ) |
| 5701 |
|
| 5702 |
noExit = False |
| 5703 |
|
| 5704 |
if endCounter%2 != 0: |
| 5705 |
if not oddLabel: |
| 5706 |
noExit = True |
| 5707 |
else: |
| 5708 |
if oddLabel: |
| 5709 |
noExit = True |
| 5710 |
|
| 5711 |
if noExit: |
| 5712 |
|
| 5713 |
if decValue == 2: |
| 5714 |
decCode = "" |
| 5715 |
condCode = "" |
| 5716 |
nonFinalJumpNeeded = False |
| 5717 |
if finalLoop: |
| 5718 |
|
| 5719 |
finalJump = SBranch |
| 5720 |
|
| 5721 |
if decCode: module.add(decCode) |
| 5722 |
if condCode: module.add(condCode) |
| 5723 |
else: |
| 5724 |
module.add(SSubU32( |
| 5725 |
dst=loopCounter, src0=loopCounter, \ |
| 5726 |
src1=1, \ |
| 5727 |
comment="dec counter%s"%(loopChar) )) |
| 5728 |
|
| 5729 |
module.add(SCmpEQI32( |
| 5730 |
src0=loopCounter, \ |
| 5731 |
src1=hex(endCounter), \ |
| 5732 |
comment="counter%s==%d"%(loopChar,endCounter) )) |
| 5733 |
|
| 5734 |
jumpLabel = loopLabelEnd |
| 5735 |
if not tailLoop and not kernel["SuppressNoLoadLoop"] and kernel["ExpandPointerSwap"]: |
| 5736 |
|
| 5737 |
|
| 5738 |
jumpLabel = loopLabelEndOddExit if oddLabel else loopLabelEndEvenExit |
| 5739 |
if not finalLoop: |
| 5740 |
if nonFinalJumpNeeded: |
| 5741 |
|
| 5742 |
module.add(SCBranchSCC1(labelName=jumpLabel.getLabelName(), comment="exit Loop%s"%loopChar )) |
| 5743 |
else: |
| 5744 |
module.add(finalJump(labelName=loopLabelBegin.getLabelName(), comment="restart Loop%s"%(loopChar))) |
| 5745 |
|
| 5746 |
if not tailLoop and loopIdx == self.states.unrollIdx: |
| 5747 |
oddIterPreCode = Module() |
| 5748 |
oddIterCode = Module() |
| 5749 |
evenIterPreCode = Module() |
| 5750 |
evenIterCode = Module() |
| 5751 |
if not kernel["SuppressNoLoadLoop"] and kernel["ExpandPointerSwap"]: |
| 5752 |
oddIterPreCode.add(loopLabelEndOddExit) |
| 5753 |
|
| 5754 |
|
| 5755 |
|
| 5756 |
oddIterPreCode.addComment1("Select high bank of LDS") |
| 5757 |
|
| 5758 |
if not kernel["DirectToVgprA"] and not kernel["StoreSwapAddr"]: |
| 5759 |
if kernel["ExpertSchedulingMode"] > 0: |
| 5760 |
oddIterCode.add(SWaitAlu(vm_vsrc=0, comment="wait for local read to vgpr complete")) |
| 5761 |
oddIterCode.add(self.localReadSwapOffsets(kernel, False, tPA)) |
| 5762 |
|
| 5763 |
if not kernel["DirectToVgprB"] and not kernel["StoreSwapAddr"]: |
| 5764 |
if kernel["ExpertSchedulingMode"] > 0: |
| 5765 |
oddIterCode.add(SWaitAlu(vm_vsrc=0, comment="wait for local read to vgpr complete")) |
| 5766 |
oddIterCode.add(self.localReadSwapOffsets(kernel, False, tPB)) |
| 5767 |
|
| 5768 |
if kernel["ProblemType"]["Sparse"]: |
| 5769 |
if kernel["DirectToVgprSparseMetadata"]: |
| 5770 |
oddIterCode.add(SWaitCnt(vlcnt=0, comment="wait for global read before moving metadata to target vgpr")) |
| 5771 |
for i in range(0, self.states.m.numVgprValuPerBlock): |
| 5772 |
oddIterCode.add(VMovB32(vgpr("ValuMetadata+%u"%i), vgpr("ValuMetadata+%u+%u"%(self.states.m.numVgprValuPerBlock, i)), \ |
| 5773 |
comment="copy ValuMetadata blk1 to blk0")) |
| 5774 |
else: |
| 5775 |
oddIterCode.add(self.localReadSwapOffsets(kernel, False, tPM)) |
| 5776 |
|
| 5777 |
evenIterPreCode.add(loopLabelEndEvenExit) |
| 5778 |
|
| 5779 |
if kernel["PrefetchGlobalRead"]==2: |
| 5780 |
if not kernel["DirectToVgprA"] or not kernel["DirectToVgprB"]: |
| 5781 |
if kernel["ExpertSchedulingMode"] > 0: |
| 5782 |
evenIterCode.add(SWaitAlu(vm_vsrc=0, comment="wait for local read to vgpr complete")) |
| 5783 |
|
| 5784 |
|
| 5785 |
if not kernel["DirectToLdsA"]: |
| 5786 |
evenIterCode.add(self.localWriteSwapOffsets(kernel, False, tPA)) |
| 5787 |
if not kernel["DirectToLdsB"]: |
| 5788 |
evenIterCode.add(self.localWriteSwapOffsets(kernel, False, tPB)) |
| 5789 |
evenIterCode.add(self.localWriteSwapOffsets(kernel, True, tPA)) |
| 5790 |
evenIterCode.add(self.localWriteSwapOffsets(kernel, True, tPB)) |
| 5791 |
|
| 5792 |
|
| 5793 |
|
| 5794 |
firstPreCode = evenIterPreCode |
| 5795 |
firstCode = evenIterCode |
| 5796 |
secondPreCode = oddIterPreCode |
| 5797 |
secondCode = oddIterCode |
| 5798 |
if oddLabel: |
| 5799 |
|
| 5800 |
firstPreCode, secondPreCode = secondPreCode, firstPreCode |
| 5801 |
firstCode, secondCode = secondCode, firstCode |
| 5802 |
|
| 5803 |
module.add(firstPreCode) |
| 5804 |
module.add(firstCode) |
| 5805 |
|
| 5806 |
|
| 5807 |
if secondCode.count(): |
| 5808 |
module.add(SBranch(labelName=loopLabelEnd.getLabelName(), \ |
| 5809 |
comment="exit unroll loop%s (and skip second exit code)"%(loopChar))) |
| 5810 |
module.add(secondPreCode) |
| 5811 |
module.add(secondCode) |
| 5812 |
|
| 5813 |
module.add(loopLabelEnd) |
| 5814 |
|
| 5815 |
if tailLoop: |
| 5816 |
if len(kernel["ProblemType"]["IndicesSummation"]) > 1 or kernel["StreamK"]: |
| 5817 |
|
| 5818 |
|
| 5819 |
|
| 5820 |
tPList = [] |
| 5821 |
if self.oriLraA != None: |
| 5822 |
module.add(VMovB32(dst=vgpr("LocalReadAddrA"), src=vgpr(self.oriLraA), comment="restore LRA")) |
| 5823 |
self.vgprPool.checkIn(self.oriLraA) |
| 5824 |
self.oriLraA = None |
| 5825 |
else: |
| 5826 |
tPList.append(tPA) |
| 5827 |
if self.oriLraB != None: |
| 5828 |
module.add(VMovB32(dst=vgpr("LocalReadAddrB"), src=vgpr(self.oriLraB), comment="restore LRA")) |
| 5829 |
self.vgprPool.checkIn(self.oriLraB) |
| 5830 |
self.oriLraB = None |
| 5831 |
else: |
| 5832 |
tPList.append(tPB) |
| 5833 |
|
| 5834 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 5835 |
if self.oriLraM != None: |
| 5836 |
module.add(VMovB32(dst=vgpr("LocalReadAddrMetadata"), src=vgpr(self.oriLraM), comment="restore LRA")) |
| 5837 |
self.vgprPool.checkIn(self.oriLraM) |
| 5838 |
self.oriLraM= None |
| 5839 |
else: |
| 5840 |
tPList.append(tPM) |
| 5841 |
|
| 5842 |
for tP in tPList: |
| 5843 |
tc = tP["tensorChar"] |
| 5844 |
LdsPad = kernel["LdsPad%s" % tc] * tP["bpeDS"] if kernel["LdsBlockSizePerPad%s"%tc] == 0 else 0 |
| 5845 |
inc = kernel["LocalSplitU"] * (kernel["MacroTile%s"%tc] + LdsPad) * tP["bpeDS"] |
| 5846 |
|
| 5847 |
|
| 5848 |
if kernel["EnableMatrixInstruction"]: |
| 5849 |
if kernel["UnrollMajorLDS%s" % tP["tensorChar"]]: |
| 5850 |
inc = kernel["LocalSplitU"] * tP["bpeDS"] |
| 5851 |
|
| 5852 |
|
| 5853 |
|
| 5854 |
if not (tP["isA"] and kernel["DirectToVgprA"] or tP["isB"] and kernel["DirectToVgprB"]): |
| 5855 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 5856 |
stmp = tmpSgprInfo.idx |
| 5857 |
module.add(SMovB32(dst=sgpr(stmp), src=inc, comment="tailloop lds offset")) |
| 5858 |
module.add(SMulI32(dst=sgpr(stmp), src0=sgpr("OrigLoopCounter"), src1=sgpr(stmp), comment="scale by mul")) |
| 5859 |
module.add(VSubU32(dst=vgpr("LocalReadAddr%s"%tc), src0=vgpr("LocalReadAddr%s"%tc), src1=sgpr(stmp), comment="remove lro damage")) |
| 5860 |
|
| 5861 |
if self.oriLwaA != None: |
| 5862 |
module.add(VMovB32(dst=vgpr("LocalWriteAddrA"), src=vgpr(self.oriLwaA), comment="restore LWA")) |
| 5863 |
module.add(VMovB32(dst=vgpr("LocalWriteAddrB"), src=vgpr(self.oriLwaB), comment="restore LWA")) |
| 5864 |
self.vgprPool.checkIn(self.oriLwaA) |
| 5865 |
self.vgprPool.checkIn(self.oriLwaB) |
| 5866 |
self.oriLwaA = None |
| 5867 |
self.oriLwaB = None |
| 5868 |
|
| 5869 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 5870 |
module.add(VMovB32(dst=vgpr("LocalWriteAddrMetadata"), src=vgpr(self.oriLwaM), comment="restore LWA")) |
| 5871 |
self.vgprPool.checkIn(self.oriLwaM) |
| 5872 |
self.oriLwaM = None |
| 5873 |
|
| 5874 |
if tailLoop and kernel["LocalSplitU"] > 1: |
| 5875 |
sgprCnt = self.states.laneSGPRCount |
| 5876 |
waveSize = kernel["WavefrontSize"] |
| 5877 |
module.addComment1("restore full exec mask") |
| 5878 |
|
| 5879 |
with self.allocTmpSgpr(sgprCnt) as tmpSgprInfo: |
| 5880 |
fullExec = tmpSgprInfo.idx |
| 5881 |
activeMask = "0xFFFFFFFF" if (waveSize == 32) else "0xFFFFFFFFFFFFFFFF" |
| 5882 |
SMovBX = SMovB32 if (waveSize == 32) else SMovB64 |
| 5883 |
SOrSaveExecBX = SOrSaveExecB32 if (waveSize == 32) else SOrSaveExecB64 |
| 5884 |
module.add(SMovBX(dst=sgpr(fullExec,sgprCnt), src=activeMask, comment="restore all threads active")) |
| 5885 |
module.add(SOrSaveExecBX (dst=sgpr(fullExec,sgprCnt), src=sgpr(fullExec,sgprCnt), comment="full mask -> exec" )) |
| 5886 |
return module |
| 5887 |
|
| 5888 |
|
| 5889 |
|
| 5890 |
|
| 5891 |
def endSummation(self, kernel, tPA, tPB, noSkipLoad = True, label = None, isOptNLL = False): |
| 5892 |
module = Module("endSummation") |
| 5893 |
|
| 5894 |
module.add(Label((self.labels.getUniqueNamePrefix("Summation_End") if label is None else label), "")) |
| 5895 |
|
| 5896 |
if kernel["StorePriorityOpt"]: |
| 5897 |
module.add(SSetPrior(prior=0, comment="optimization store")) |
| 5898 |
|
| 5899 |
if isOptNLL: |
| 5900 |
vbegin = self.states.startVgprMisc |
| 5901 |
vsize = self.states.lastVgprForReads - vbegin |
| 5902 |
if self.states.a.startVgprLocalReadAddr > self.states.lastVgprForReads: |
| 5903 |
vsize = self.states.a.startVgprLocalReadAddr + self.states.a.numVgprLocalReadAddr - vbegin |
| 5904 |
if self.states.b.startVgprLocalReadAddr > self.states.lastVgprForReads: |
| 5905 |
vsize = self.states.b.startVgprLocalReadAddr + self.states.b.numVgprLocalReadAddr - vbegin |
| 5906 |
self.vgprPool.add(vbegin, vsize, "endSummation") |
| 5907 |
module.addComment0("endSummation: add vgpr [%u...%u) to pool" % \ |
| 5908 |
(vbegin, vbegin+vsize)) |
| 5909 |
|
| 5910 |
|
| 5911 |
if kernel["ProblemType"]["Gradient"] and kernel["ProblemType"]["UseBias"] and (kernel["ProblemType"]["BiasSrc"] == "A" or kernel["ProblemType"]["BiasSrc"] == "B"): |
| 5912 |
|
| 5913 |
tP = tPA if kernel["ProblemType"]["BiasSrc"] == "A" else tPB |
| 5914 |
module.add(self.exclasses.biasSumUnroll.storeSumLDS(self, kernel, tP)) |
| 5915 |
|
| 5916 |
vbegin = self.states.bias.startVgprValu |
| 5917 |
vsize = self.states.bias.numVgprValu |
| 5918 |
self.vgprPool.add(vbegin, vsize, "free vgpr of biasSumUnroll") |
| 5919 |
module.addComment0("endSummation: add vgpr [%u...%u) to pool" % \ |
| 5920 |
(vbegin, vbegin+vsize)) |
| 5921 |
|
| 5922 |
lastRegTag=None |
| 5923 |
|
| 5924 |
spool = self.sgprPool.getPool() |
| 5925 |
for i in range(0, self.sgprPool.size()): |
| 5926 |
regTag = spool[i].tag |
| 5927 |
if regTag != lastRegTag: |
| 5928 |
lastRegTag = regTag |
| 5929 |
if (lastRegTag not in self.states.nonPostLoopSgpr) and (spool[i].status == RegisterPool.Status.InUse): |
| 5930 |
if label == "Summation_End_OptNLL": |
| 5931 |
self.undefineSgpr(regTag) |
| 5932 |
else: |
| 5933 |
module.add(self.undefineSgpr(regTag)) |
| 5934 |
|
| 5935 |
if self.db["InitVgpr"] & 0x2: |
| 5936 |
module.add(self.vgprPool.initTmps(self.consts.initVgprValue,start=0, stop=100)) |
| 5937 |
if 0: |
| 5938 |
for i in range(0,16+1): |
| 5939 |
module.add(VMovB32(dst=vgpr(21), src=vgpr(21), comment="hack tmp in pool")) |
| 5940 |
|
| 5941 |
|
| 5942 |
if self.db["InitSgpr"] & 0x2: |
| 5943 |
module.add(self.sgprPool.initTmps(self.consts.initSgprValue)) |
| 5944 |
|
| 5945 |
if self.db["ConservativeWaitCnt"] & 0x10: |
| 5946 |
module.add(SBarrier(comment="debug")) |
| 5947 |
module.add(SWaitCnt(dscnt=0, vlcnt=0, vscnt=0, comment="")) |
| 5948 |
|
| 5949 |
if kernel["SuppressNoLoadLoop"]: |
| 5950 |
module.add(SWaitCnt(dscnt=0, vlcnt=0, vscnt=0, comment="wait for all summation activity")) |
| 5951 |
|
| 5952 |
|
| 5953 |
|
| 5954 |
|
| 5955 |
|
| 5956 |
module.addComment0("load store sgprs") |
| 5957 |
|
| 5958 |
runActivation = True if ((kernel["ProblemType"]["ActivationType"] != 'none') \ |
| 5959 |
and kernel["ActivationFused"]) else False |
| 5960 |
|
| 5961 |
def fixPreloadOffset(offset, sgpxIdxVec, numStoreSgprToLoad): |
| 5962 |
item = None |
| 5963 |
startVgprName = sgpxIdxVec[0] |
| 5964 |
if kernel["ProblemType"]["UseScaleAB"] == "Scalar": |
| 5965 |
if (kernel["ProblemType"]["DataTypeA"].numRegisters() > kernel["ProblemType"]["DataType"].numRegisters()) and (kernel["ProblemType"]["DataTypeB"].numRegisters() > kernel["ProblemType"]["DataType"].numRegisters()): |
| 5966 |
self.argLoader.setOffset(offset + ((self.states.rpga * self.states.bpr) * 2)) |
| 5967 |
elif kernel["ProblemType"]["DataTypeA"].numRegisters() > kernel["ProblemType"]["DataType"].numRegisters(): |
| 5968 |
assert sgpxIdxVec[0] == self.sgprs["AddressScaleB"] |
| 5969 |
self.argLoader.setOffset(offset + (self.states.rpga * self.states.bpr)) |
| 5970 |
elif kernel["ProblemType"]["DataTypeB"].numRegisters() > kernel["ProblemType"]["DataType"].numRegisters(): |
| 5971 |
assert sgpxIdxVec[0] == self.sgprs["AddressScaleA"] |
| 5972 |
item = self.argLoader.loadKernArg(self.sgprs["AddressScaleA"], "KernArgAddress", dword=2) |
| 5973 |
if len(sgpxIdxVec) > 1: |
| 5974 |
startVgprName = sgpxIdxVec[1] |
| 5975 |
numStoreSgprToLoad -= self.states.rpga |
| 5976 |
self.argLoader.setOffset(offset + ((self.states.rpga * self.states.bpr) * 2)) |
| 5977 |
return (item, startVgprName, numStoreSgprToLoad) |
| 5978 |
|
| 5979 |
if self.states.numStoreSgprToLoad: |
| 5980 |
sgpxIdxVec = self.defineMultiSgprIndex(self.states.numStoreSgprNames, self.states.numStoreSgprNameSizes, align=4) |
| 5981 |
for name in self.states.numStoreSgprNames: |
| 5982 |
module.add(RegSet("s", "sgpr"+name, self.sgprs[name])) |
| 5983 |
if noSkipLoad and kernel["GlobalSplitU"] != 0: |
| 5984 |
gsuLabel = Label(label=self.labels.getNameInc("GSU"), comment="") |
| 5985 |
with self.allocTmpSgpr(1) as tmpSgprGSU: |
| 5986 |
module.add(SAndB32(dst=sgpr(tmpSgprGSU.idx), src0=sgpr("GSU"), src1=hex(0x3FFF), comment="Restore GSU")) |
| 5987 |
module.add(SCmpEQU32(src0=sgpr(tmpSgprGSU.idx), src1=1, comment="GSU == 1 ?")) |
| 5988 |
if (kernel["_GlobalAccumulation"] != 'MultipleBufferSingleKernel'): |
| 5989 |
module.add(SCBranchSCC0(labelName=gsuLabel.getLabelName(), comment="branch if GSU != 1")) |
| 5990 |
if kernel["ProblemType"]["SupportUserArgs"]: |
| 5991 |
extReadEpilogueLabel = Label(label=self.labels.getNameInc("LoadExternalEpilogueStruct"), comment="") |
| 5992 |
extReadEpilogueLabelEnd = Label(label=self.labels.getNameInc("LoadExternalEpilogueStructEnd"), comment="") |
| 5993 |
module.addComment0("Check if custom structure pointer is null") |
| 5994 |
module.add(SCmpEQU32(src0=sgpr("ArgType"), src1=2, comment="ArgType == 2 ?")) |
| 5995 |
module.add(SCBranchSCC1(labelName=extReadEpilogueLabel.getLabelName(), comment="branch if ArgType == 2")) |
| 5996 |
argOffset = self.argLoader.getOffset() |
| 5997 |
numStoreSgprToLoad = self.states.numStoreSgprToLoad |
| 5998 |
(item, startVgprName, numStoreSgprToLoad) = fixPreloadOffset(argOffset, sgpxIdxVec, numStoreSgprToLoad) |
| 5999 |
if item: |
| 6000 |
module.add(item) |
| 6001 |
loadModule = module.addModuleAsFlatItems(self.argLoader.loadAllKernArg(startVgprName, "KernArgAddress", numStoreSgprToLoad)) |
| 6002 |
self.states.numStoreSgprInst = countSMemLoad(loadModule) |
| 6003 |
self.argLoader.setOffset(argOffset) |
| 6004 |
module.add(SBranch(extReadEpilogueLabelEnd.getLabelName())) |
| 6005 |
module.add(extReadEpilogueLabel) |
| 6006 |
extArgOffset = self.externalArgLoader.getOffset() |
| 6007 |
backupExtArgOffset = extArgOffset |
| 6008 |
loadList = [[-1, 0, extArgOffset]] |
| 6009 |
extArgOffset += self.states.userArgsInfo.scaleASize |
| 6010 |
if (kernel["ProblemType"]["UseScaleAB"] == "Scalar" and (not self.states.preloadScaleA)) or kernel["ProblemType"]["UseScaleAB"] == "Vector": |
| 6011 |
if loadList[-1][0] == -1: |
| 6012 |
loadList[-1][0] = self.sgprs["AddressScaleA"] |
| 6013 |
loadList[-1][1] += self.states.userArgsInfo.scaleASize |
| 6014 |
else: |
| 6015 |
loadList.append([-1, 0, extArgOffset]) |
| 6016 |
extArgOffset += self.states.userArgsInfo.scaleBSize |
| 6017 |
if (kernel["ProblemType"]["UseScaleAB"] == "Scalar" and (not self.states.preloadScaleB)) or kernel["ProblemType"]["UseScaleAB"] == "Vector": |
| 6018 |
if loadList[-1][0] == -1: |
| 6019 |
loadList[-1][0] = self.sgprs["AddressScaleB"] |
| 6020 |
loadList[-1][1] += self.states.userArgsInfo.scaleBSize |
| 6021 |
else: |
| 6022 |
loadList.append([-1, 0, extArgOffset]) |
| 6023 |
extArgOffset += self.states.userArgsInfo.scaleCSize + self.states.userArgsInfo.scaleDSize |
| 6024 |
if kernel["ProblemType"]["UseScaleCD"]: |
| 6025 |
if loadList[-1][0] == -1: |
| 6026 |
loadList[-1][0] = self.sgprs["AddressScaleC"] |
| 6027 |
loadList[-1][1] += self.states.userArgsInfo.scaleCSize + self.states.userArgsInfo.scaleDSize |
| 6028 |
else: |
| 6029 |
loadList.append([-1, 0, extArgOffset]) |
| 6030 |
extArgOffset += self.states.userArgsInfo.scaleAlphaVecSize |
| 6031 |
if kernel["ProblemType"]["UseScaleAlphaVec"]: |
| 6032 |
if loadList[-1][0] == -1: |
| 6033 |
loadList[-1][0] = self.sgprs["AddressScaleAlphaVec"] |
| 6034 |
loadList[-1][1] += self.states.userArgsInfo.scaleAlphaVecSize |
| 6035 |
else: |
| 6036 |
loadList.append([-1, 0, extArgOffset]) |
| 6037 |
extArgOffset += self.states.userArgsInfo.biasSize |
| 6038 |
if self.states.numSgprAddressBias: |
| 6039 |
biasLoadSize = (self.states.numSgprAddressBias + self.states.BiasType + self.states.BiasStride) * 4 |
| 6040 |
if loadList[-1][0] == -1: |
| 6041 |
loadList[-1][0] = self.sgprs["AddressBias"] |
| 6042 |
loadList[-1][1] += biasLoadSize |
| 6043 |
if biasLoadSize < self.states.userArgsInfo.biasSize: |
| 6044 |
loadList.append([-1, 0, extArgOffset]) |
| 6045 |
else: |
| 6046 |
loadList.append([-1, 0, extArgOffset]) |
| 6047 |
|
| 6048 |
extArgOffset += self.states.userArgsInfo.factorDimSize |
| 6049 |
if self.states.FactorDim == 3: |
| 6050 |
if loadList[-1][0] == -1: |
| 6051 |
loadList[-1][0] = self.sgprs["FactorDim"] |
| 6052 |
loadList[-1][1] += self.states.userArgsInfo.factorDimSize |
| 6053 |
else: |
| 6054 |
loadList.append([-1, 0, extArgOffset]) |
| 6055 |
|
| 6056 |
extArgOffset += self.states.userArgsInfo.eSize |
| 6057 |
if kernel["ProblemType"]["UseE"]: |
| 6058 |
if loadList[-1][0] == -1: |
| 6059 |
loadList[-1][0] = self.sgprs["AddressE"] |
| 6060 |
loadList[-1][1] += self.states.userArgsInfo.eSize |
| 6061 |
else: |
| 6062 |
loadList.append([-1, 0, extArgOffset]) |
| 6063 |
extArgOffset += self.states.userArgsInfo.activationSize |
| 6064 |
if runActivation: |
| 6065 |
needActTypeArg = 1 if self.states.numActivationTypeArgSize else 0 |
| 6066 |
actNames = kernel["ProblemType"]["ActivationType"].getAdditionalArgStringList() |
| 6067 |
actLoadSize = (len(actNames) * self.states.numActivationArgSize + needActTypeArg) * 4 |
| 6068 |
if (actLoadSize == self.states.userArgsInfo.activationSize) or len(actNames) > 0: |
| 6069 |
if loadList[-1][0] == -1: |
| 6070 |
loadList[-1][0] = self.sgprs[actNames[0]] |
| 6071 |
loadList[-1][1] += actLoadSize |
| 6072 |
else: |
| 6073 |
loadList.append(["ActivationType", actLoadSize]) |
| 6074 |
loadList.append([-1, 0, extArgOffset - (needActTypeArg * 4)]) |
| 6075 |
else: |
| 6076 |
loadList.append([-1, 0, extArgOffset]) |
| 6077 |
|
| 6078 |
loadModuleExt = Module("Count Inst") |
| 6079 |
for loadInfo in loadList: |
| 6080 |
if loadInfo[0] == -1: |
| 6081 |
continue |
| 6082 |
dwordLen = loadInfo[1] // 4 |
| 6083 |
self.externalArgLoader.setOffset(loadInfo[2]) |
| 6084 |
loadModuleExt.addModuleAsFlatItems(module.addModuleAsFlatItems(self.externalArgLoader.loadAllKernArg(loadInfo[0], "KernArgAddress", dwordLen))) |
| 6085 |
self.states.numStoreSgprInstExt = countSMemLoad(loadModuleExt) |
| 6086 |
self.externalArgLoader.setOffset(backupExtArgOffset) |
| 6087 |
module.add(extReadEpilogueLabelEnd) |
| 6088 |
else: |
| 6089 |
argOffset = self.argLoader.getOffset() |
| 6090 |
numStoreSgprToLoad = self.states.numStoreSgprToLoad |
| 6091 |
(item, startVgprName, numStoreSgprToLoad) = fixPreloadOffset(argOffset, sgpxIdxVec, numStoreSgprToLoad) |
| 6092 |
if item: |
| 6093 |
module.add(item) |
| 6094 |
loadModule = module.addModuleAsFlatItems(self.argLoader.loadAllKernArg(startVgprName, "KernArgAddress", numStoreSgprToLoad)) |
| 6095 |
self.states.numStoreSgprInst = countSMemLoad(loadModule) |
| 6096 |
self.argLoader.setOffset(argOffset) |
| 6097 |
if noSkipLoad and kernel["GlobalSplitU"] != 0: |
| 6098 |
module.add(gsuLabel) |
| 6099 |
|
| 6100 |
|
| 6101 |
|
| 6102 |
|
| 6103 |
|
| 6104 |
if self.states.numStoreSgprToLoad2: |
| 6105 |
module.addComment0("load store sgprs2") |
| 6106 |
for name in self.states.numStoreSgprNames2: |
| 6107 |
module.add(RegSet("s", "sgpr"+name, self.sgprs[name])) |
| 6108 |
|
| 6109 |
argOffset = self.argLoader.getOffset() |
| 6110 |
if kernel["ProblemType"]["UseScaleAB"] == "Scalar" and (kernel["ProblemType"]["DataTypeA"].numRegisters() > kernel["ProblemType"]["DataType"].numRegisters() or kernel["ProblemType"]["DataTypeB"].numRegisters() > kernel["ProblemType"]["DataType"].numRegisters()): |
| 6111 |
self.argLoader.setOffset(argOffset + (self.states.numStoreSgprToLoad)*4 + (self.states.rpga * self.states.bpr)) |
| 6112 |
else: |
| 6113 |
self.argLoader.setOffset(argOffset + (self.states.numStoreSgprToLoad)*4) |
| 6114 |
numStoreSgprToLoad = self.states.numStoreSgprToLoad2 |
| 6115 |
|
| 6116 |
if kernel["ProblemType"]["UseScaleAB"] == "Scalar" and (kernel["ProblemType"]["DataTypeA"].numRegisters() > kernel["ProblemType"]["DataType"].numRegisters() or kernel["ProblemType"]["DataTypeB"].numRegisters() > kernel["ProblemType"]["DataType"].numRegisters()): |
| 6117 |
argOffsettmp = (argOffset + (self.states.numStoreSgprToLoad)*4 + (self.states.rpga * self.states.bpr)) |
| 6118 |
else: |
| 6119 |
argOffsettmp = (argOffset + (self.states.numStoreSgprToLoad)*4) |
| 6120 |
|
| 6121 |
extReadEpilogueLabeltmp = Label(label=self.labels.getNameInc("LoadExternalEpilogueStruct"), comment="") |
| 6122 |
module.addComment0("Check if custom structure pointer is null") |
| 6123 |
if kernel["ProblemType"]["SupportUserArgs"]: |
| 6124 |
module.add(SCmpEQU32(src0=sgpr("ArgType"), src1=2, comment="ArgType == 2 ?")) |
| 6125 |
module.add(SCBranchSCC1(labelName=extReadEpilogueLabeltmp.getLabelName())) |
| 6126 |
|
| 6127 |
module.add(self.argLoader.loadKernArg("AddressTD", "KernArgAddress", sgprOffset=hex(argOffsettmp), dword=2)) |
| 6128 |
module.add(self.argLoader.loadKernArg("Synchronizer", "KernArgAddress", sgprOffset=hex(argOffsettmp + (2)*4), dword=2)) |
| 6129 |
|
| 6130 |
module.add(extReadEpilogueLabeltmp) |
| 6131 |
|
| 6132 |
self.argLoader.setOffset(argOffset) |
| 6133 |
|
| 6134 |
|
| 6135 |
if (not self.states.doShadowInit) and kernel["BufferStore"]: |
| 6136 |
self.defineSgpr("SrdD", 4, 4) |
| 6137 |
self.defineSgpr("SrdC", 4, 4) |
| 6138 |
module.add(RegSet("s", "sgprSrdC", self.sgprs["SrdC"])) |
| 6139 |
module.add(RegSet("s", "sgprSrdD", self.sgprs["SrdD"])) |
| 6140 |
if (kernel["ProblemType"]["UseScaleAB"] == "Vector"): |
| 6141 |
self.defineSgpr("SrdScaleA", 4, 4) |
| 6142 |
self.defineSgpr("SrdScaleB", 4, 4) |
| 6143 |
module.add(RegSet("s", "sgprSrdScaleA", self.sgprs["SrdScaleA"])) |
| 6144 |
module.add(RegSet("s", "sgprSrdScaleB", self.sgprs["SrdScaleB"])) |
| 6145 |
if kernel["ProblemType"]["UseScaleAlphaVec"]: |
| 6146 |
self.defineSgpr("SrdScaleAlphaVec", 4, 4) |
| 6147 |
module.add(RegSet("s", "sgprSrdScaleAlphaVec", self.sgprs["SrdScaleAlphaVec"])) |
| 6148 |
if self.states.useBias != DataDirection.NONE: |
| 6149 |
self.defineSgpr("SrdBias", 4, 4) |
| 6150 |
module.add(RegSet("s", "sgprSrdBias", self.sgprs["SrdBias"])) |
| 6151 |
|
| 6152 |
if (kernel["GlobalSplitUAlgorithm"] == 'MultipleBufferSingleKernel'): |
| 6153 |
self.defineSgpr("SrdTD", 4, 4) |
| 6154 |
module.add(RegSet("s", "sgprSrdTD", self.sgprs["SrdTD"])) |
| 6155 |
self.defineSgpr("GSUSync", 1) |
| 6156 |
module.add(RegSet("s", "sgprGSUSync", self.sgprs["GSUSync"])) |
| 6157 |
|
| 6158 |
if kernel["ProblemType"]["UseE"]: |
| 6159 |
self.defineSgpr("SrdE", 4, 4) |
| 6160 |
module.add(RegSet("s", "sgprSrdE", self.sgprs["SrdE"])) |
| 6161 |
for idx in range(0, kernel["ProblemType"]["NumIndicesC"]): |
| 6162 |
i = idx |
| 6163 |
idxChar= self.states.indexChars[idx] |
| 6164 |
if i == 0 and not kernel["ProblemType"]["UseInitialStridesCD"]: |
| 6165 |
module.add(ValueSet("constStrideE%s"%idxChar, 1)) |
| 6166 |
else: |
| 6167 |
if not kernel["ProblemType"]["UseInitialStridesCD"]: |
| 6168 |
i = i-1 |
| 6169 |
module.add(RegSet("s", "sgprStrideE%s"%idxChar, \ |
| 6170 |
"sgprStridesE", i)) |
| 6171 |
if kernel["ProblemType"]["UseScaleCD"]: |
| 6172 |
assert kernel["ProblemType"]["ComputeDataType"].isSingle() |
| 6173 |
self.defineSgpr("ScaleD", 2, 2) |
| 6174 |
module.add(RegSet("s", "sgprScaleD", self.sgprs["ScaleD"])) |
| 6175 |
|
| 6176 |
if kernel["_GlobalAccumulation"] == 'MultipleBufferSingleKernel': |
| 6177 |
self.defineSgpr("SrdSync", 4, 4) |
| 6178 |
module.add(RegSet("s", "sgprSrdSync", self.sgprs["SrdSync"])) |
| 6179 |
|
| 6180 |
|
| 6181 |
|
| 6182 |
|
| 6183 |
|
| 6184 |
if kernel["EnableMatrixInstruction"]: |
| 6185 |
|
| 6186 |
|
| 6187 |
|
| 6188 |
module.addComment1("Mapping of Acc register -> C Vgpr register") |
| 6189 |
self.codes.accVgprRead = mapAcctoArchRegs(kernel, self.states.maxLimitAgprs, write=False) |
| 6190 |
if kernel["StreamK"] > 0 and kernel["StreamKAtomic"] == 0: |
| 6191 |
self.codes.accVgprWrite = mapAcctoArchRegs(kernel, self.states.maxLimitAgprs, write=True) |
| 6192 |
if kernel["GlobalSplitUAlgorithm"] == "MultipleBufferSingleKernel": |
| 6193 |
self.codes.accVgprWrite = mapAcctoArchRegs(kernel, self.states.maxLimitAgprs, write=True) |
| 6194 |
if kernel["MIArchVgpr"]: |
| 6195 |
module.addComment1("Multiply MI out register with Alpha -> C Vgpr register") |
| 6196 |
self.codes.mulAlphaMultipleBuffer = moveMIoutToArch(kernel, self.states.startVgprAlphaTmp) |
| 6197 |
self.codes.mulAlphaOther = mulMIoutAlphaToArch(kernel, self.states.startVgprAlphaTmp) |
| 6198 |
|
| 6199 |
return module |
| 6200 |
|
| 6201 |
def mfmaIter_waitCount(self, kernel): |
| 6202 |
if self.states.version in [(9,4,2), (9,5,0)]: |
| 6203 |
dataType = kernel["ProblemType"]["DataType"] |
| 6204 |
miM = kernel["MatrixInstM"] |
| 6205 |
miN = kernel["MatrixInstN"] |
| 6206 |
if dataType.isSingle() or dataType.isHalf() or dataType.isBFloat16(): |
| 6207 |
if miM == 4 and miN == 4: |
| 6208 |
return 2 |
| 6209 |
return 0 |
| 6210 |
|
| 6211 |
|
| 6212 |
|
| 6213 |
|
| 6214 |
def generateSrcStrForMFMA(self, kernel, tP, innerUnroll, vregSetIdx, vgprPerInput, m, u, iui, idxAB, bk=None): |
| 6215 |
tc = tP["tensorChar"] |
| 6216 |
|
| 6217 |
statesAorB = self.states.a if tP["isA"] else self.states.b |
| 6218 |
numVgprValuPerBlock = kernel["MIWaveTile%c"%tc] * kernel["MIInputPerThread%c"%tc] * tP["bpe"] // self.states.bpr |
| 6219 |
numIterPerCoalescedRead = self.states.numIterPerCoalescedReadA if tP["isA"] else self.states.numIterPerCoalescedReadB |
| 6220 |
numReadsIterCoalesced = self.states.numReadsIterCoalescedA if tP["isA"] else self.states.numReadsIterCoalescedB |
| 6221 |
|
| 6222 |
|
| 6223 |
m_or_u = u if kernel["DirectToVgpr%c"%tc] else m |
| 6224 |
vgprBuffer_new = (m_or_u//numIterPerCoalescedRead)*numIterPerCoalescedRead |
| 6225 |
vgprBuffer_new_offset = m_or_u%numIterPerCoalescedRead*innerUnroll*vgprPerInput |
| 6226 |
|
| 6227 |
|
| 6228 |
packDTV = self.states.packDTVA if tP["isA"] else self.states.packDTVB |
| 6229 |
convDTV = self.states.convDTVA if tP["isA"] else self.states.convDTVB |
| 6230 |
if packDTV or convDTV: |
| 6231 |
|
| 6232 |
numBi = kernel["LoopIters"] |
| 6233 |
vgprBuffer_new += vregSetIdx * numBi |
| 6234 |
|
| 6235 |
iui_new = (iui//numReadsIterCoalesced)*numReadsIterCoalesced |
| 6236 |
iui_new_offset = iui%numReadsIterCoalesced*vgprPerInput |
| 6237 |
ab_new = idxAB*vgprPerInput*numReadsIterCoalesced |
| 6238 |
abStr = "Valu%c_X%u_I%u+%u+%u+%u" % (tc, vgprBuffer_new, iui_new, ab_new, vgprBuffer_new_offset, iui_new_offset) |
| 6239 |
if kernel["DirectToVgpr%c"%tc] and not (packDTV or convDTV): |
| 6240 |
|
| 6241 |
numVgprPerBlock = statesAorB.numVgprG2LAllocated |
| 6242 |
numVgprPerBlock //= 2 |
| 6243 |
ab_new += vregSetIdx * numVgprPerBlock + ( vgprBuffer_new * innerUnroll) * numVgprValuPerBlock |
| 6244 |
abStr = "G2L%c+%u+%u" % (tc, ab_new, vgprBuffer_new_offset) |
| 6245 |
|
| 6246 |
if bk != None: |
| 6247 |
abStr += "+%u"%(bk) |
| 6248 |
return abStr |
| 6249 |
|
| 6250 |
|
| 6251 |
|
| 6252 |
|
| 6253 |
def macIter(self, kernel, tPA, tPB, bufferIdx, iuiCount, useMacro, isTail=False): |
| 6254 |
imod = Module("macIter_X%u_I%u"%(bufferIdx, iuiCount)) |
| 6255 |
|
| 6256 |
|
| 6257 |
|
| 6258 |
|
| 6259 |
|
| 6260 |
|
| 6261 |
shiftK = Module("shiftK") |
| 6262 |
inputType = kernel["ProblemType"]["DataType"] |
| 6263 |
numRegistersIn = inputType.numRegisters() |
| 6264 |
loopCounterName = self.loopCounterName(kernel, self.states.unrollIdx) |
| 6265 |
numInput = kernel["NumDotElements"] if kernel["UseDotInstruction"] else 1 |
| 6266 |
kPerIter = numInput * kernel["NumWaveSplitK"] |
| 6267 |
vgprPerInput = 1 |
| 6268 |
shiftPerElement = int(numRegistersIn * 32) |
| 6269 |
s_nop = 0 |
| 6270 |
|
| 6271 |
kReg = None |
| 6272 |
abReg = None |
| 6273 |
dummy = -1 |
| 6274 |
|
| 6275 |
if isTail and (kernel["AssertSummationElementMultiple"] % kPerIter != 0): |
| 6276 |
kReg = self.vgprPool.checkOut(1,"kReg") |
| 6277 |
loopCntSgpr = loopCounterName |
| 6278 |
|
| 6279 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 6280 |
shiftK.add(vectorStaticRemainder(dummy, kReg, "Serial", kernel["NumWaveSplitK"], None, tmpSgprInfo)) |
| 6281 |
|
| 6282 |
numTmpSgpr = 4 if (vgprPerInput > 2) else 3 |
| 6283 |
|
| 6284 |
with self.allocTmpSgpr(numTmpSgpr, alignment=1) as tmpSgprInfo: |
| 6285 |
if tmpSgprInfo.idx % 2 == 0: |
| 6286 |
tmpSgprX2 = tmpSgprInfo.idx |
| 6287 |
tmpSgprX1 = tmpSgprInfo.idx+2 |
| 6288 |
else: |
| 6289 |
tmpSgprX2 = tmpSgprInfo.idx+1 |
| 6290 |
tmpSgprX1 = tmpSgprInfo.idx |
| 6291 |
|
| 6292 |
|
| 6293 |
shiftK.add(vectorStaticMultiply(vgpr(kReg), vgpr(kReg), numInput, tmpSgprInfo)) |
| 6294 |
shiftK.add(VCmpGEI32(dst=sgpr(tmpSgprX2, self.states.laneSGPRCount), src0=vgpr(kReg), src1=sgpr(loopCntSgpr), comment="check K index >= Size L")) |
| 6295 |
for a in range(0, kernel["ThreadTileA"]): |
| 6296 |
for iui in range(0, iuiCount): |
| 6297 |
aStr = vgpr("ValuA_X%u_I%u+%u" % (bufferIdx, iui, a*vgprPerInput), 1) |
| 6298 |
shiftK.add(VCndMaskB32(dst=aStr, src0=aStr, src1=0, src2=sgpr(tmpSgprX2, self.states.laneSGPRCount), comment="set 0 if K_idx >= sizeL")) |
| 6299 |
|
| 6300 |
for b in range(0, kernel["ThreadTileB"]): |
| 6301 |
for iui in range(0, iuiCount): |
| 6302 |
bStr = vgpr("ValuB_X%u_I%u+%u" % (bufferIdx, iui, b*vgprPerInput), 1) |
| 6303 |
shiftK.add(VCndMaskB32(dst=bStr, src0=bStr, src1=0, src2=sgpr(tmpSgprX2, self.states.laneSGPRCount), comment="set 0 if K_idx >= sizeL")) |
| 6304 |
|
| 6305 |
|
| 6306 |
if kernel["AssertSummationElementMultiple"] % numInput != 0: |
| 6307 |
abReg = self.vgprPool.checkOutAligned(vgprPerInput, vgprPerInput, "abReg") |
| 6308 |
shiftK.add(VSubU32(dst=vgpr(kReg), src0=sgpr(loopCntSgpr), src1=vgpr(kReg), comment="get distance between size and k index")) |
| 6309 |
shiftK.add(VCmpLtI32(dst=sgpr(tmpSgprX2, self.states.laneSGPRCount), src0=vgpr(kReg), src1=numInput, comment="set partial 0 if distance less than numInput")) |
| 6310 |
shiftK.add(SAndB32(dst=sgpr(tmpSgprX1), src0=sgpr(loopCntSgpr), src1=numInput-1, comment="get inputs for edge thread")) |
| 6311 |
shiftK.add(SSubU32(dst=sgpr(tmpSgprX1), src0=numInput, src1=sgpr(tmpSgprX1), comment="use shift to fill 0 for outside element")) |
| 6312 |
shiftK.add(SLShiftLeftB32(dst=sgpr(tmpSgprX1), shiftHex=log2(shiftPerElement), src=sgpr(tmpSgprX1), comment="use shift to fill 0 for outside element")) |
| 6313 |
|
| 6314 |
|
| 6315 |
VShiftLeft = VLShiftLeftB32 |
| 6316 |
|
| 6317 |
for a in range(0, kernel["ThreadTileA"]): |
| 6318 |
for iui in range(0, iuiCount): |
| 6319 |
aStr = vgpr("ValuA_X%u_I%u+%u" % (bufferIdx, iui, a*vgprPerInput), vgprPerInput) |
| 6320 |
shiftK.add(VShiftLeft(dst=vgpr(abReg, vgprPerInput), shiftHex=sgpr(tmpSgprX1), src=aStr, comment="")) |
| 6321 |
for bk in range(0, vgprPerInput): |
| 6322 |
aStr = vgpr("ValuA_X%u_I%u+%u+%u" % (bufferIdx, iui, a*vgprPerInput, bk), 1) |
| 6323 |
shiftK.add(VCndMaskB32(dst=aStr, src0=aStr, src1=vgpr(abReg+bk), src2=sgpr(tmpSgprX2, self.states.laneSGPRCount), comment="")) |
| 6324 |
|
| 6325 |
for b in range(0, kernel["ThreadTileB"]): |
| 6326 |
for iui in range(0, iuiCount): |
| 6327 |
bStr = vgpr("ValuB_X%u_I%u+%u" % (bufferIdx, iui, b*vgprPerInput), vgprPerInput) |
| 6328 |
shiftK.add(VShiftLeft(dst=vgpr(abReg, vgprPerInput), shiftHex=sgpr(tmpSgprX1), src=bStr, comment="")) |
| 6329 |
for bk in range(0, vgprPerInput): |
| 6330 |
bStr = vgpr("ValuB_X%u_I%u+%u+%u" % (bufferIdx, iui, b*vgprPerInput, bk), 1) |
| 6331 |
shiftK.add(VCndMaskB32(dst=bStr, src0=bStr, src1=vgpr(abReg+bk), src2=sgpr(tmpSgprX2, self.states.laneSGPRCount), comment="")) |
| 6332 |
|
| 6333 |
if kernel["LocalSplitU"] > 1: |
| 6334 |
self.sgprPool.checkIn(loopCntSgpr) |
| 6335 |
|
| 6336 |
s_nop = 2 |
| 6337 |
|
| 6338 |
if s_nop != 0: |
| 6339 |
imod.add(SNop(waitState=(s_nop - 1), comment="")) |
| 6340 |
|
| 6341 |
if kernel["InnerUnroll"] > 1 and iuiCount==1: |
| 6342 |
|
| 6343 |
instr = "MAC_%ux%u_X%u_OneIUI" % (kernel["ThreadTile0"],kernel["ThreadTile1"], bufferIdx) |
| 6344 |
else: |
| 6345 |
if not useMacro: |
| 6346 |
printExit("MAC doesn't support useMacro=False") |
| 6347 |
instr = "MAC_%ux%u_X%u" % (kernel["ThreadTile0"],kernel["ThreadTile1"], bufferIdx) |
| 6348 |
|
| 6349 |
|
| 6350 |
if kReg is not None: self.vgprPool.checkIn(kReg) |
| 6351 |
if abReg is not None: self.vgprPool.checkIn(abReg) |
| 6352 |
|
| 6353 |
if self.do["MAC"]: |
| 6354 |
imod.add(shiftK) |
| 6355 |
imod.add(MacroInstruction(name=instr, args=[])) |
| 6356 |
imod.addSpaceLine() |
| 6357 |
return imod |
| 6358 |
|
| 6359 |
|
| 6360 |
|
| 6361 |
|
| 6362 |
def accVgprReadWriteFunction(self, kernel, idx, read=True): |
| 6363 |
if not kernel["MIArchVgpr"]: |
| 6364 |
if idx >= self.states.maxLimitAgprs: |
| 6365 |
return VMovB32 |
| 6366 |
else: |
| 6367 |
return VAccvgprReadB32 if read else VAccvgprWriteB32 |
| 6368 |
else: |
| 6369 |
return VMovB32 |
| 6370 |
def accVgprReadWriteIndex(self, kernel, idx, sz=1): |
| 6371 |
if not kernel["MIArchVgpr"]: |
| 6372 |
if idx >= self.states.maxLimitAgprs: |
| 6373 |
return vgpr(idx - self.states.maxLimitAgprs, sz) |
| 6374 |
else: |
| 6375 |
return accvgpr(idx, sz) |
| 6376 |
else: |
| 6377 |
return vgpr(idx, sz) |
| 6378 |
|
| 6379 |
|
| 6380 |
|
| 6381 |
|
| 6382 |
def mfmaIter(self, kernel, tPA, tPB, u, innerUnroll, vregSetIdx, unrollLoopIdx = 0, unrollIdx = 0, tail = False, firstIter = False, postShiftK = Module()): |
| 6383 |
imod = Module("mi") |
| 6384 |
shiftK = Module("shiftK") |
| 6385 |
m = (u) % (self.states.numVgprBuffer) |
| 6386 |
|
| 6387 |
def dataTypeToMfmaInstTypePair(dataType: DataType, sourceSwap: bool) -> Tuple[InstType, InstType]: |
| 6388 |
miInTypeStr = dataType.toNameAbbrev() |
| 6389 |
miInInstType = dataTypeNameAbbrevToInstType(miInTypeStr, sourceSwap) |
| 6390 |
miOutInstType = dataTypeNameAbbrevToInstType(dataType.MIOutputTypeNameAbbrev()) |
| 6391 |
return miInInstType, miOutInstType |
| 6392 |
|
| 6393 |
def dataTypeNameAbbrevToInstType(abbrev: str, sourceSwap: bool = False) -> InstType: |
| 6394 |
if abbrev == 'f64': |
| 6395 |
return InstType.INST_F64 |
| 6396 |
elif abbrev == 'f32': |
| 6397 |
return InstType.INST_F32 |
| 6398 |
elif abbrev == 'f16': |
| 6399 |
return InstType.INST_F16 |
| 6400 |
elif abbrev == 'i32': |
| 6401 |
return InstType.INST_I32 |
| 6402 |
elif abbrev == 'i8': |
| 6403 |
return InstType.INST_I8 |
| 6404 |
elif abbrev == 'bf16': |
| 6405 |
return InstType.INST_BF16 |
| 6406 |
elif abbrev == 'xf32': |
| 6407 |
return InstType.INST_XF32 |
| 6408 |
elif abbrev == 'fp8_fp8': |
| 6409 |
return InstType.INST_F8 |
| 6410 |
elif abbrev == 'bf8_bf8': |
| 6411 |
return InstType.INST_BF8 |
| 6412 |
elif (abbrev == 'fp8_bf8' and sourceSwap == False) or \ |
| 6413 |
(abbrev == 'bf8_fp8' and sourceSwap == True): |
| 6414 |
return InstType.INST_F8_BF8 |
| 6415 |
elif (abbrev == 'bf8_fp8' and sourceSwap == False) or \ |
| 6416 |
(abbrev == 'fp8_bf8' and sourceSwap == True): |
| 6417 |
return InstType.INST_BF8_F8 |
| 6418 |
else: |
| 6419 |
assert("Unsupported data type.") |
| 6420 |
return InstType.INST_NOTYPE |
| 6421 |
|
| 6422 |
miInputType = kernel["ProblemType"]["F32XdlMathOp"] if kernel["EnableF32XdlMathOp"] else kernel["ProblemType"]["DataType"] |
| 6423 |
|
| 6424 |
is_mfma = self.states.asmCaps["HasMFMA"] |
| 6425 |
is_wmma_v1 = self.states.asmCaps["HasWMMA_V1"] |
| 6426 |
is_wmma_v2 = self.states.asmCaps["HasWMMA_V2"] |
| 6427 |
numRegistersIn = miInputType.numRegisters() |
| 6428 |
numRegistersOut = kernel["MIRegPerOut"] |
| 6429 |
loopCounterName = self.loopCounterName(kernel, self.states.unrollIdx) |
| 6430 |
accs_per_wave = kernel["MatrixInstM"] * kernel["MatrixInstN"] * kernel["MatrixInstB"] \ |
| 6431 |
// self.states.kernel["WavefrontSize"] * numRegistersOut |
| 6432 |
dividerFortidInK = kernel["MatrixInstN"] * kernel["MatrixInstB"] |
| 6433 |
numMIInputA = kernel["MIInputPerThreadA"] |
| 6434 |
numMIInputB = kernel["MIInputPerThreadB"] |
| 6435 |
numMIInput = max(numMIInputA,numMIInputB) |
| 6436 |
miInInstType, miOutInstType = dataTypeToMfmaInstTypePair(miInputType, kernel["SourceSwap"]) |
| 6437 |
neg_flag = True if ((not is_mfma) and (miInInstType == InstType.INST_I8)) else False |
| 6438 |
miInInstType = InstType.INST_U8 if ((not is_mfma) and miInInstType == InstType.INST_I8) else miInInstType |
| 6439 |
miOutInstType = miOutInstType if is_mfma else dataTypeNameAbbrevToInstType(kernel["ProblemType"]["ComputeDataType"].toNameAbbrev()) |
| 6440 |
numReadsIterCoalescedA = self.states.numReadsIterCoalescedA |
| 6441 |
numReadsIterCoalescedB = self.states.numReadsIterCoalescedB |
| 6442 |
numReadsIterCoalesced = max(numReadsIterCoalescedA, numReadsIterCoalescedB) |
| 6443 |
|
| 6444 |
vgprPerInputA = int(numMIInputA * numRegistersIn) |
| 6445 |
vgprPerInputB = int(numMIInputB * numRegistersIn) |
| 6446 |
vgprPerInput = max(vgprPerInputA,vgprPerInputB) |
| 6447 |
shiftPerElement = int(numRegistersIn * 32) |
| 6448 |
s_nop = 0 |
| 6449 |
accumRegType = "acc" if not kernel["MIArchVgpr"] else "v" |
| 6450 |
mfma_1k = True if kernel["MFMA_BF16_1K"] else False |
| 6451 |
accStoreCIdx = 0 |
| 6452 |
|
| 6453 |
kReg_first = None |
| 6454 |
kReg = None |
| 6455 |
abReg = None |
| 6456 |
tmpVgpr = None |
| 6457 |
dummy = -1 |
| 6458 |
|
| 6459 |
if (numRegistersIn < 1) and ((kernel["UnrollMajorLDSA"] == False) or (kernel["UnrollMajorLDSB"] == False)): |
| 6460 |
s_nop = 2 |
| 6461 |
|
| 6462 |
if kernel["ConvertAfterDS"] and (numRegistersIn < 1) and ((tPA["bpe"] > tPA["bpeDS"]) or (tPB["bpe"] > tPB["bpeDS"])): |
| 6463 |
s_nop = 2 |
| 6464 |
|
| 6465 |
|
| 6466 |
|
| 6467 |
|
| 6468 |
|
| 6469 |
|
| 6470 |
|
| 6471 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 6472 |
vgprPerInputM = 1 |
| 6473 |
vgprBufferM_new = (m//self.states.numIterPerCoalescedReadMetadata)*self.states.numIterPerCoalescedReadMetadata |
| 6474 |
vgprBufferM_new_offset = m%self.states.numIterPerCoalescedReadMetadata*kernel["InnerUnroll"]*vgprPerInputM |
| 6475 |
|
| 6476 |
|
| 6477 |
|
| 6478 |
if tail and (kernel["MatrixInstK"] > 1 or numReadsIterCoalescedA > 1 or numReadsIterCoalescedB > 1): |
| 6479 |
if not is_wmma_v1: |
| 6480 |
|
| 6481 |
kReg_first = self.vgprPool.checkOut(1,"kReg_first") |
| 6482 |
kReg = self.vgprPool.checkOut(1,"kReg") |
| 6483 |
if kernel["LocalSplitU"] > 1: |
| 6484 |
loopCntSgpr = self.sgprPool.checkOut(1) |
| 6485 |
else: |
| 6486 |
loopCntSgpr = loopCounterName |
| 6487 |
|
| 6488 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 6489 |
shiftK.add(vectorStaticRemainder(dummy, kReg_first, "Serial", self.states.kernel["WavefrontSize"], tmpVgpr, tmpSgprInfo)) |
| 6490 |
shiftK.add(vectorStaticDivide(kReg_first, kReg_first, dividerFortidInK, tmpVgpr)) |
| 6491 |
numTmpSgpr = 4 if (vgprPerInput > 2 and is_wmma_v2) else 3 |
| 6492 |
|
| 6493 |
with self.allocTmpSgpr(numTmpSgpr, alignment=1) as tmpSgprInfo: |
| 6494 |
if tmpSgprInfo.idx % 2 == 0: |
| 6495 |
tmpSgprX2 = tmpSgprInfo.idx |
| 6496 |
tmpSgprX1 = tmpSgprInfo.idx+2 |
| 6497 |
if numTmpSgpr == 4: |
| 6498 |
tmpSgprX3 = tmpSgprInfo.idx+3 |
| 6499 |
else: |
| 6500 |
tmpSgprX2 = tmpSgprInfo.idx+1 |
| 6501 |
tmpSgprX1 = tmpSgprInfo.idx |
| 6502 |
if numTmpSgpr == 4: |
| 6503 |
tmpSgprX3 = tmpSgprInfo.idx+3 |
| 6504 |
|
| 6505 |
def findSparseOffset(isA:bool): |
| 6506 |
blocksPerTGroupSMFMA = 1 |
| 6507 |
elementsPerBlockSMFMA = 1 |
| 6508 |
blockOffsetSMFMA =1 |
| 6509 |
if kernel["ProblemType"]["Sparse"] != 0: |
| 6510 |
if kernel["MIInputPerThread"] * kernel["ProblemType"]["DataTypeB"].numBytes() > 16: |
| 6511 |
isSparseTrack = (kernel["ProblemType"]["Sparse"] == 1 and isA) or (kernel["ProblemType"]["Sparse"] == 2 and not isA) |
| 6512 |
|
| 6513 |
|
| 6514 |
blocksPerTGroupSMFMA = 1 if isSparseTrack else 2 |
| 6515 |
if blocksPerTGroupSMFMA > 1: |
| 6516 |
threadGroups = kernel["MatrixInstK"] // kernel["MIInputPerThread"] |
| 6517 |
elementsPerBlockSMFMA = kernel["MIInputPerThread"] // blocksPerTGroupSMFMA |
| 6518 |
blockStride = elementsPerBlockSMFMA * threadGroups |
| 6519 |
blockOffsetSMFMA = blockStride - elementsPerBlockSMFMA |
| 6520 |
return blocksPerTGroupSMFMA, elementsPerBlockSMFMA, blockOffsetSMFMA |
| 6521 |
|
| 6522 |
blocksPerTGroupSMFMAA, elementsPerBlockSMFMAA, blockOffsetSMFMAA = findSparseOffset(True) |
| 6523 |
blocksPerTGroupSMFMAB, elementsPerBlockSMFMAB, blockOffsetSMFMAB = findSparseOffset(False) |
| 6524 |
|
| 6525 |
if kernel["ProblemType"]["Sparse"] == 1 and numMIInput//8 >= 1: |
| 6526 |
vgprPerSet0Group = 1 |
| 6527 |
elif vgprPerInputA <= 2: |
| 6528 |
shiftK.add(vectorStaticMultiply(vgpr(kReg_first), vgpr(kReg_first), numMIInput * numReadsIterCoalesced, tmpSgprInfo)) |
| 6529 |
kStepForCoalesced = (u%numReadsIterCoalesced) * numMIInput |
| 6530 |
if kStepForCoalesced > 0: |
| 6531 |
shiftK.add(VAddU32(vgpr(kReg_first), hex(kStepForCoalesced), vgpr(kReg_first), "k += (u%%numReadsIterCoalesced) * numMIInput")) |
| 6532 |
if kernel["LocalSplitU"] > 1: |
| 6533 |
shiftK.add(SMinI32(dst=sgpr(loopCntSgpr), src0=sgpr(loopCounterName), src1=sgpr("LSUTailLoopOffset"), comment="check lsu bound")) |
| 6534 |
shiftK.add(VCmpGEI32(dst=sgpr(tmpSgprX2, self.states.laneSGPRCount), src0=vgpr(kReg_first), src1=sgpr(loopCntSgpr), comment="check K index >= Size L")) |
| 6535 |
vgprPerSet0Group = vgprPerInputA |
| 6536 |
elif is_wmma_v2 and vgprPerInputA > 2: |
| 6537 |
vgprPerSet0Group = 4 |
| 6538 |
else: |
| 6539 |
vgprPerSet0Group = 2 |
| 6540 |
numSet0GroupA = vgprPerInputA//vgprPerSet0Group |
| 6541 |
for group in range(0, numSet0GroupA): |
| 6542 |
if numSet0GroupA > 1 or (is_wmma_v2 and vgprPerInputA > 2): |
| 6543 |
if group == 0: |
| 6544 |
if kernel["ProblemType"]["Sparse"]: |
| 6545 |
multiplyBy = numMIInput//blocksPerTGroupSMFMAA |
| 6546 |
else: |
| 6547 |
multiplyBy = numMIInput//2 if vgprPerInputA == 8 else numMIInput |
| 6548 |
shiftK.add(vectorStaticMultiply(vgpr(kReg_first), vgpr(kReg_first), multiplyBy, tmpSgprInfo)) |
| 6549 |
shiftK.add(VAddU32(vgpr(kReg), vgpr(kReg_first), 0, "")) |
| 6550 |
elif blocksPerTGroupSMFMAA == 2 and (group * vgprPerSet0Group) == (elementsPerBlockSMFMAA * numRegistersIn): |
| 6551 |
kIncA = blockOffsetSMFMAA + (numMIInput//numSet0GroupA) * max(group - 1, 0) |
| 6552 |
shiftK.add(VAddU32(vgpr(kReg), vgpr(kReg), kIncA, "add part of K")) |
| 6553 |
else: |
| 6554 |
kIncA = numMIInput//numSet0GroupA |
| 6555 |
if self.states.asmCaps["HasMFMA_f8f6f4"]: |
| 6556 |
if group == 2 and vgprPerInputA == 8: |
| 6557 |
kIncA = 56 if kernel["MatrixInstK"] == 128 else 24 |
| 6558 |
if kernel["UseF32XEmulation"]: |
| 6559 |
kIncA = 14 if kernel["MatrixInstK"] == 32 else 6 |
| 6560 |
shiftK.add(VAddU32(vgpr(kReg), vgpr(kReg), kIncA, "add part of K")) |
| 6561 |
if kernel["LocalSplitU"] > 1: |
| 6562 |
shiftK.add(SMinI32(dst=sgpr(loopCntSgpr), src0=sgpr(loopCounterName), src1=sgpr("LSUTailLoopOffset"), comment="check lsu bound")) |
| 6563 |
shiftK.add(VCmpGEI32(dst=sgpr(tmpSgprX2, self.states.laneSGPRCount), src0=vgpr(kReg), src1=sgpr(loopCntSgpr), comment="check K index >= Size L")) |
| 6564 |
|
| 6565 |
if not tPA["isSwizzled"]: |
| 6566 |
for bk in range(0, vgprPerSet0Group): |
| 6567 |
for a in range(0, kernel["MIWaveTileA"]): |
| 6568 |
for iui in range(0, innerUnroll): |
| 6569 |
aStr = vgpr(self.generateSrcStrForMFMA(kernel, tPA, innerUnroll, vregSetIdx, vgprPerInputA, m, u, iui, a, bk=bk + group * vgprPerSet0Group), 1) |
| 6570 |
shiftK.add(VCndMaskB32(dst=aStr, src0=aStr, src1=0, src2=sgpr(tmpSgprX2, self.states.laneSGPRCount), comment="set 0 if K_idx >= sizeL")) |
| 6571 |
|
| 6572 |
if kernel["ProblemType"]["Sparse"] == 2 and numMIInput//8 >= 1: |
| 6573 |
shiftK.add(vectorStaticRemainder(dummy, kReg_first, "Serial", kernel["WavefrontSize"], tmpVgpr, tmpSgprInfo)) |
| 6574 |
shiftK.add(vectorStaticDivide(kReg_first, kReg_first, dividerFortidInK, tmpVgpr)) |
| 6575 |
vgprPerSet0Group = 1 |
| 6576 |
elif vgprPerInputB <= 2: |
| 6577 |
vgprPerSet0Group = vgprPerInputB |
| 6578 |
elif is_wmma_v2 and vgprPerInputB > 2: |
| 6579 |
shiftK.add(vectorStaticRemainder(dummy, kReg_first, "Serial", kernel["WavefrontSize"], tmpVgpr, tmpSgprInfo)) |
| 6580 |
shiftK.add(vectorStaticDivide(kReg_first, kReg_first, dividerFortidInK, tmpVgpr)) |
| 6581 |
vgprPerSet0Group = 4 |
| 6582 |
else: |
| 6583 |
shiftK.add(vectorStaticRemainder(dummy, kReg_first, "Serial", kernel["WavefrontSize"], tmpVgpr, tmpSgprInfo)) |
| 6584 |
shiftK.add(vectorStaticDivide(kReg_first, kReg_first, dividerFortidInK, tmpVgpr)) |
| 6585 |
vgprPerSet0Group = 2 |
| 6586 |
|
| 6587 |
numSet0GroupB = vgprPerInputB//vgprPerSet0Group |
| 6588 |
for group in range(0, numSet0GroupB): |
| 6589 |
if numSet0GroupB > 1 or (is_wmma_v2 and vgprPerInputB > 2): |
| 6590 |
if group == 0: |
| 6591 |
if kernel["ProblemType"]["Sparse"]: |
| 6592 |
multiplyBy = numMIInput//blocksPerTGroupSMFMAB |
| 6593 |
else: |
| 6594 |
multiplyBy = numMIInput//2 if vgprPerInputB == 8 else numMIInput |
| 6595 |
shiftK.add(vectorStaticMultiply(vgpr(kReg_first), vgpr(kReg_first), multiplyBy, tmpSgprInfo)) |
| 6596 |
shiftK.add(VAddU32(vgpr(kReg), vgpr(kReg_first), 0, "")) |
| 6597 |
elif blocksPerTGroupSMFMAB == 2 and (group * vgprPerSet0Group) == (elementsPerBlockSMFMAB * numRegistersIn): |
| 6598 |
kIncB = blockOffsetSMFMAB + (numMIInput//numSet0GroupB) * max(group - 1, 0) |
| 6599 |
shiftK.add(VAddU32(vgpr(kReg), vgpr(kReg), kIncB, "add part of K")) |
| 6600 |
else: |
| 6601 |
kIncB = numMIInput//numSet0GroupB |
| 6602 |
if group == 2 and vgprPerInputB == 8: |
| 6603 |
kIncB = 56 if kernel["MatrixInstK"] == 128 else 24 |
| 6604 |
if kernel["UseF32XEmulation"]: |
| 6605 |
kIncB = 14 if kernel["MatrixInstK"] == 32 else 6 |
| 6606 |
shiftK.add(VAddU32(vgpr(kReg), vgpr(kReg), kIncB, "add part of K")) |
| 6607 |
|
| 6608 |
if kernel["LocalSplitU"] > 1: |
| 6609 |
shiftK.add(SMinI32(dst=sgpr(loopCntSgpr), src0=sgpr(loopCounterName), src1=sgpr("LSUTailLoopOffset"), comment="check lsu bound")) |
| 6610 |
shiftK.add(VCmpGEI32(dst=sgpr(tmpSgprX2, self.states.laneSGPRCount), src0=vgpr(kReg), src1=sgpr(loopCntSgpr), comment="check K index >= Size L")) |
| 6611 |
for bk in range(0, vgprPerSet0Group): |
| 6612 |
for b in range(0, kernel["MIWaveTileB"]): |
| 6613 |
for iui in range(0, innerUnroll): |
| 6614 |
bStr = vgpr(self.generateSrcStrForMFMA(kernel, tPB, innerUnroll, vregSetIdx, vgprPerInputB, m, u, iui, b, bk=bk + group*vgprPerSet0Group), 1) |
| 6615 |
shiftK.add(VCndMaskB32(dst=bStr, src0=bStr, src1=0, src2=sgpr(tmpSgprX2, self.states.laneSGPRCount), comment="set 0 if K_idx >= sizeL")) |
| 6616 |
|
| 6617 |
|
| 6618 |
if numMIInput > 1 and kernel["AssertSummationElementMultiple"] < 8: |
| 6619 |
alignment = vgprPerInput if is_wmma_v2 else (2 if vgprPerInput > 1 else 1) |
| 6620 |
abReg = self.vgprPool.checkOutAligned(vgprPerInput, alignment, "abReg") |
| 6621 |
if (vgprPerInput < 4 and is_mfma) or is_wmma_v2: |
| 6622 |
shiftK.add(VSubU32(dst=vgpr(kReg), src0=sgpr(loopCntSgpr), src1=vgpr(kReg_first), comment="get distance between size and k index")) |
| 6623 |
shiftK.add(VCmpLtI32(dst=sgpr(tmpSgprX2, self.states.laneSGPRCount), src0=vgpr(kReg), src1=numMIInput, comment="set partial 0 if distance less than input per thread")) |
| 6624 |
TailLoop_SkipZeroOutMask = Label((self.labels.getUniqueNamePrefix("TailLoop_SkipZeroOutMask")), comment="") |
| 6625 |
shiftK.add(SAndB32(dst=sgpr(tmpSgprX1), src0=sgpr("SizeL"), src1=8-1, comment="if summation is multiple of 8, skip masking")) |
| 6626 |
shiftK.add(SCmpEQU32(src0=sgpr(tmpSgprX1), src1=0)) |
| 6627 |
shiftK.add(SCBranchSCC1(labelName=TailLoop_SkipZeroOutMask.getLabelName(), comment="skip mask")) |
| 6628 |
shiftK.add(SAndB32(dst=sgpr(tmpSgprX1), src0=sgpr(loopCntSgpr), src1=numMIInput-1, comment="get inputs for edge thread")) |
| 6629 |
shiftK.add(SSubU32(dst=sgpr(tmpSgprX1), src0=numMIInput, src1=sgpr(tmpSgprX1), comment="use shift to fill 0 for outside element")) |
| 6630 |
shiftK.add(SLShiftLeftB32(dst=sgpr(tmpSgprX1), shiftHex=log2(shiftPerElement), src=sgpr(tmpSgprX1), comment="use shift to fill 0 for outside element")) |
| 6631 |
|
| 6632 |
if vgprPerInput == 1: |
| 6633 |
VShiftLeft = VLShiftLeftB32 |
| 6634 |
elif vgprPerInput >= 2: |
| 6635 |
VShiftLeft = VLShiftLeftB64 |
| 6636 |
if vgprPerInput == 4 and is_wmma_v2: |
| 6637 |
tmpVgpr2 = self.vgprPool.checkOutAligned(2, 2, "tmpVgpr2") |
| 6638 |
|
| 6639 |
for a in range(0, kernel["MIWaveTileA"]): |
| 6640 |
for iui in range(0, innerUnroll): |
| 6641 |
aStr_base = self.generateSrcStrForMFMA(kernel, tPA, innerUnroll, vregSetIdx, vgprPerInput, m, u, iui, a) |
| 6642 |
aStr = vgpr(aStr_base, min(2, vgprPerInput)) |
| 6643 |
if is_wmma_v2: |
| 6644 |
if vgprPerInput == 4: |
| 6645 |
a_64_shift = Label(label=self.labels.getNameInc("a_64_Shift"), comment="") |
| 6646 |
a_32_shift = Label(label=self.labels.getNameInc("a_32_Shift"), comment="") |
| 6647 |
a_common = Label(label=self.labels.getNameInc("a_shift_end"), comment="") |
| 6648 |
aStr1 = vgpr(aStr_base + "+1", min(2, vgprPerInput)) |
| 6649 |
aStr2 = vgpr(aStr_base + "+2", min(2, vgprPerInput)) |
| 6650 |
|
| 6651 |
shiftK.add(SMovB32(dst=sgpr(tmpSgprX3), src=sgpr(tmpSgprX1), comment="sgpr used for minic shift 128 bit")) |
| 6652 |
shiftK.add(SCmpGeI32(src0=sgpr(tmpSgprX3), src1=64, comment="check offset > 63")) |
| 6653 |
shiftK.add(SCBranchSCC1(labelName=a_64_shift.getLabelName(), comment="jump when positive")) |
| 6654 |
shiftK.add(SCmpGeI32(src0=sgpr(tmpSgprX3), src1=32, comment="check offset > 32")) |
| 6655 |
shiftK.add(SCBranchSCC1(labelName=a_32_shift.getLabelName(), comment="jump when positive")) |
| 6656 |
shiftK.add(VShiftLeft(dst=vgpr(tmpVgpr2, 2), shiftHex=sgpr(tmpSgprX3), src=aStr, comment="")) |
| 6657 |
shiftK.add(VMovB32(dst=vgpr(abReg), src=vgpr(tmpVgpr2),comment="")) |
| 6658 |
shiftK.add(VMovB32(dst=vgpr(abReg+1), src=vgpr(tmpVgpr2+1),comment="")) |
| 6659 |
shiftK.add(VShiftLeft(dst=vgpr(tmpVgpr2, 2), shiftHex=sgpr(tmpSgprX3), src=aStr1, comment="")) |
| 6660 |
shiftK.add(VMovB32(dst=vgpr(abReg+2), src=vgpr(tmpVgpr2+1),comment="")) |
| 6661 |
shiftK.add(VShiftLeft(dst=vgpr(tmpVgpr2, 2), shiftHex=sgpr(tmpSgprX3), src=aStr2, comment="")) |
| 6662 |
shiftK.add(VMovB32(dst=vgpr(abReg+3), src=vgpr(tmpVgpr2+1),comment="")) |
| 6663 |
shiftK.add(SBranch(a_common.getLabelName())) |
| 6664 |
shiftK.add(a_32_shift) |
| 6665 |
shiftK.add(SSubU32(dst=sgpr(tmpSgprX3), src0=sgpr(tmpSgprX3), src1=32, comment="")) |
| 6666 |
shiftK.add(VMovB32(dst=vgpr(abReg), src=0, comment="")) |
| 6667 |
shiftK.add(VShiftLeft(dst=vgpr(tmpVgpr2, 2), shiftHex=sgpr(tmpSgprX3), src=aStr, comment="")) |
| 6668 |
shiftK.add(VMovB32(dst=vgpr(abReg+1), src=vgpr(tmpVgpr2),comment="")) |
| 6669 |
shiftK.add(VMovB32(dst=vgpr(abReg+2), src=vgpr(tmpVgpr2+1),comment="")) |
| 6670 |
shiftK.add(VShiftLeft(dst=vgpr(tmpVgpr2, 2), shiftHex=sgpr(tmpSgprX3), src=aStr1, comment="")) |
| 6671 |
shiftK.add(VMovB32(dst=vgpr(abReg+3), src=vgpr(tmpVgpr2+1),comment="")) |
| 6672 |
shiftK.add(SAddU32(dst=sgpr(tmpSgprX3), src0=sgpr(tmpSgprX3), src1=32, comment="")) |
| 6673 |
shiftK.add(SBranch(a_common.getLabelName())) |
| 6674 |
shiftK.add(a_64_shift) |
| 6675 |
shiftK.add(SSubU32(dst=sgpr(tmpSgprX3), src0=sgpr(tmpSgprX3), src1=64, comment="")) |
| 6676 |
shiftK.add(VMovB32(dst=vgpr(abReg), src=0, comment="")) |
| 6677 |
shiftK.add(VMovB32(dst=vgpr(abReg+1), src=0, comment="")) |
| 6678 |
shiftK.add(VShiftLeft(dst=vgpr(abReg+2, 2), shiftHex=sgpr(tmpSgprX3), src=aStr, comment="")) |
| 6679 |
shiftK.add(SAddU32(dst=sgpr(tmpSgprX3), src0=sgpr(tmpSgprX3), src1=64, comment="")) |
| 6680 |
shiftK.add(a_common) |
| 6681 |
else: |
| 6682 |
shiftK.add(VShiftLeft(dst=vgpr(abReg, vgprPerInput), shiftHex=sgpr(tmpSgprX1), src=aStr, comment="")) |
| 6683 |
for bk in range(0, vgprPerInput): |
| 6684 |
aStr = vgpr(self.generateSrcStrForMFMA(kernel, tPA, innerUnroll, vregSetIdx, vgprPerInput, m, u, iui, a, bk=bk), 1) |
| 6685 |
shiftK.add(VCndMaskB32(dst=aStr, src0=aStr, src1=vgpr(abReg+bk), src2=sgpr(tmpSgprX2, self.states.laneSGPRCount), comment="")) |
| 6686 |
else: |
| 6687 |
if kernel["ProblemType"]["Sparse"]: |
| 6688 |
if vgprPerInput == 2: |
| 6689 |
aStr = vgpr(self.generateSrcStrForMFMA(kernel, tPA, innerUnroll, vregSetIdx, vgprPerInput, m, u, iui, a), 2) |
| 6690 |
shiftK.add(VShiftLeft(dst=vgpr(abReg, vgprPerInput), shiftHex=sgpr(tmpSgprX1), src=aStr, comment="")) |
| 6691 |
elif vgprPerInput <= 2: |
| 6692 |
shiftK.add(VShiftLeft(dst=vgpr(abReg, vgprPerInput), shiftHex=sgpr(tmpSgprX1), src=aStr, comment="")) |
| 6693 |
if vgprPerInput >= 4 and vgprPerInput % 2 == 0: |
| 6694 |
for ivgpr in range(0, vgprPerInput, 2): |
| 6695 |
aStr = vgpr(self.generateSrcStrForMFMA(kernel, tPA, innerUnroll, vregSetIdx, vgprPerInput, m, u, iui, a, bk=ivgpr), 2) |
| 6696 |
shiftK.add(VShiftLeft(dst=vgpr(abReg+ivgpr, 2), shiftHex=sgpr(tmpSgprX1), src=aStr, comment="")) |
| 6697 |
for bk in range(0, vgprPerInput): |
| 6698 |
aStr = vgpr(self.generateSrcStrForMFMA(kernel, tPA, innerUnroll, vregSetIdx, vgprPerInput, m, u, iui, a, bk=bk), 1) |
| 6699 |
if vgprPerInput >= 4: |
| 6700 |
if bk == 0: |
| 6701 |
shiftK.add(VAddU32(vgpr(kReg), vgpr(kReg_first), numMIInput//numSet0GroupA, "add part of K")) |
| 6702 |
elif bk % 2 == 0: |
| 6703 |
kIncA = numMIInput//numSet0GroupA |
| 6704 |
if bk == 4 and self.states.asmCaps["HasMFMA_f8f6f4"]: |
| 6705 |
kIncA = 56 if kernel["MatrixInstK"] == 128 else 24 |
| 6706 |
if kernel["UseF32XEmulation"]: |
| 6707 |
kIncA = 14 if kernel["MatrixInstK"] == 32 else 6 |
| 6708 |
shiftK.add(VAddU32(vgpr(kReg), vgpr(kReg), kIncA, "add part of K")) |
| 6709 |
|
| 6710 |
if kernel["LocalSplitU"] > 1: |
| 6711 |
shiftK.add(SMinI32(dst=sgpr(loopCntSgpr), src0=sgpr(loopCounterName), src1=sgpr("LSUTailLoopOffset"), comment="check lsu bound")) |
| 6712 |
shiftK.add(VCmpGEI32(dst=sgpr(tmpSgprX2,2), src0=vgpr(kReg), src1=sgpr(loopCntSgpr), comment="check K index >= Size L")) |
| 6713 |
shiftK.add(VCndMaskB32(dst=aStr, src0=aStr, src1=vgpr(abReg+bk), src2=sgpr(tmpSgprX2,2), comment="")) |
| 6714 |
|
| 6715 |
for b in range(0, kernel["MIWaveTileB"]): |
| 6716 |
for iui in range(0, innerUnroll): |
| 6717 |
bStr_base = self.generateSrcStrForMFMA(kernel, tPB, innerUnroll, vregSetIdx, vgprPerInput, m, u, iui, b) |
| 6718 |
bStr = vgpr(bStr_base, min(2, vgprPerInput)) |
| 6719 |
if is_wmma_v2: |
| 6720 |
if vgprPerInput == 4: |
| 6721 |
b_64_shift = Label(label=self.labels.getNameInc("b_64_Shift"), comment="") |
| 6722 |
b_32_shift = Label(label=self.labels.getNameInc("b_32_Shift"), comment="") |
| 6723 |
b_common = Label(label=self.labels.getNameInc("b_shift_end"), comment="") |
| 6724 |
bStr1 = vgpr(bStr_base + "+1", min(2, vgprPerInput)) |
| 6725 |
bStr2 = vgpr(bStr_base + "+2", min(2, vgprPerInput)) |
| 6726 |
shiftK.add(SMovB32(dst=sgpr(tmpSgprX3), src=sgpr(tmpSgprX1), comment="sgpr used for minic shift 128 bit")) |
| 6727 |
shiftK.add(SCmpGeI32(src0=sgpr(tmpSgprX3), src1=64, comment="check offset >63")) |
| 6728 |
shiftK.add(SCBranchSCC1(labelName=b_64_shift.getLabelName(), comment="jump when positive")) |
| 6729 |
shiftK.add(SCmpGeI32(src0=sgpr(tmpSgprX3), src1=32, comment="check offset >32")) |
| 6730 |
shiftK.add(SCBranchSCC1(labelName=b_32_shift.getLabelName(), comment="jump when positive")) |
| 6731 |
shiftK.add(VShiftLeft(dst=vgpr(tmpVgpr2, 2), shiftHex=sgpr(tmpSgprX3), src=bStr, comment="")) |
| 6732 |
shiftK.add(VMovB32(dst=vgpr(abReg), src=vgpr(tmpVgpr2),comment="")) |
| 6733 |
shiftK.add(VMovB32(dst=vgpr(abReg+1), src=vgpr(tmpVgpr2+1),comment="")) |
| 6734 |
shiftK.add(VShiftLeft(dst=vgpr(tmpVgpr2, 2), shiftHex=sgpr(tmpSgprX3), src=bStr1, comment="")) |
| 6735 |
shiftK.add(VMovB32(dst=vgpr(abReg+2), src=vgpr(tmpVgpr2+1),comment="")) |
| 6736 |
shiftK.add(VShiftLeft(dst=vgpr(tmpVgpr2, 2), shiftHex=sgpr(tmpSgprX3), src=bStr2, comment="")) |
| 6737 |
shiftK.add(VMovB32(dst=vgpr(abReg+3), src=vgpr(tmpVgpr2+1),comment="")) |
| 6738 |
shiftK.add(SBranch(b_common.getLabelName())) |
| 6739 |
shiftK.add(b_32_shift) |
| 6740 |
shiftK.add(SSubU32(dst=sgpr(tmpSgprX3), src0=sgpr(tmpSgprX3), src1=32, comment="")) |
| 6741 |
shiftK.add(VMovB32(dst=vgpr(abReg), src=0, comment="")) |
| 6742 |
shiftK.add(VShiftLeft(dst=vgpr(tmpVgpr2, 2), shiftHex=sgpr(tmpSgprX3), src=bStr, comment="")) |
| 6743 |
shiftK.add(VMovB32(dst=vgpr(abReg+1), src=vgpr(tmpVgpr2),comment="")) |
| 6744 |
shiftK.add(VMovB32(dst=vgpr(abReg+2), src=vgpr(tmpVgpr2+1),comment="")) |
| 6745 |
shiftK.add(VShiftLeft(dst=vgpr(tmpVgpr2, 2), shiftHex=sgpr(tmpSgprX3), src=bStr1, comment="")) |
| 6746 |
shiftK.add(VMovB32(dst=vgpr(abReg+3), src=vgpr(tmpVgpr2+1),comment="")) |
| 6747 |
shiftK.add(SBranch(b_common.getLabelName())) |
| 6748 |
shiftK.add(b_64_shift) |
| 6749 |
shiftK.add(SSubU32(dst=sgpr(tmpSgprX3), src0=sgpr(tmpSgprX3), src1=64, comment="")) |
| 6750 |
shiftK.add(VMovB32(dst=vgpr(abReg), src=0, comment="")) |
| 6751 |
shiftK.add(VMovB32(dst=vgpr(abReg+1), src=0, comment="")) |
| 6752 |
shiftK.add(VShiftLeft(dst=vgpr(abReg+2,2), shiftHex=sgpr(tmpSgprX3), src=bStr, comment="")) |
| 6753 |
shiftK.add(b_common) |
| 6754 |
else: |
| 6755 |
shiftK.add(VShiftLeft(dst=vgpr(abReg, vgprPerInput), shiftHex=sgpr(tmpSgprX1), src=bStr, comment="")) |
| 6756 |
for bk in range(0, vgprPerInput): |
| 6757 |
bStr = vgpr(self.generateSrcStrForMFMA(kernel, tPB, innerUnroll, vregSetIdx, vgprPerInput, m, u, iui, b, bk=bk), 1) |
| 6758 |
shiftK.add(VCndMaskB32(dst=bStr, src0=bStr, src1=vgpr(abReg+bk), src2=sgpr(tmpSgprX2, self.states.laneSGPRCount), comment="")) |
| 6759 |
else: |
| 6760 |
if kernel["ProblemType"]["Sparse"]: |
| 6761 |
if vgprPerInput == 2: |
| 6762 |
aStr = vgpr(self.generateSrcStrForMFMA(kernel, tPA, innerUnroll, vregSetIdx, vgprPerInput, m, u, iui, a), 2) |
| 6763 |
shiftK.add(VShiftLeft(dst=vgpr(abReg, vgprPerInput), shiftHex=sgpr(tmpSgprX1), src=aStr, comment="")) |
| 6764 |
elif vgprPerInput <= 2: |
| 6765 |
shiftK.add(VShiftLeft(dst=vgpr(abReg, vgprPerInput), shiftHex=sgpr(tmpSgprX1), src=bStr, comment="")) |
| 6766 |
if vgprPerInput >= 4 and vgprPerInput % 2 == 0: |
| 6767 |
for ivgpr in range(0, vgprPerInput, 2): |
| 6768 |
bStr = vgpr(self.generateSrcStrForMFMA(kernel, tPB, innerUnroll, vregSetIdx, vgprPerInput, m, u, iui, b, bk=ivgpr), 2) |
| 6769 |
shiftK.add(VShiftLeft(dst=vgpr(abReg+ivgpr, 2), shiftHex=sgpr(tmpSgprX1), src=bStr, comment="")) |
| 6770 |
for bk in range(0, vgprPerInput): |
| 6771 |
bStr = vgpr(self.generateSrcStrForMFMA(kernel, tPB, innerUnroll, vregSetIdx, vgprPerInput, m, u, iui, b, bk=bk), 1) |
| 6772 |
if vgprPerInput >= 4: |
| 6773 |
if bk == 0: |
| 6774 |
shiftK.add(VAddU32(vgpr(kReg), vgpr(kReg_first), numMIInput//numSet0GroupB, "add part of K")) |
| 6775 |
elif bk % 2 == 0: |
| 6776 |
kIncB = numMIInput//numSet0GroupB |
| 6777 |
if bk == 4: |
| 6778 |
kIncB = 56 if kernel["MatrixInstK"] == 128 else 24 |
| 6779 |
if kernel["UseF32XEmulation"]: |
| 6780 |
kIncB = 14 if kernel["MatrixInstK"] == 32 else 6 |
| 6781 |
shiftK.add(VAddU32(vgpr(kReg), vgpr(kReg), kIncB, "add part of K")) |
| 6782 |
|
| 6783 |
if kernel["LocalSplitU"] > 1: |
| 6784 |
shiftK.add(SMinI32(dst=sgpr(loopCntSgpr), src0=sgpr(loopCounterName), src1=sgpr("LSUTailLoopOffset"), comment="check lsu bound")) |
| 6785 |
shiftK.add(VCmpGEI32(dst=sgpr(tmpSgprX2,2), src0=vgpr(kReg), src1=sgpr(loopCntSgpr), comment="check K index >= Size L")) |
| 6786 |
shiftK.add(VCndMaskB32(dst=bStr, src0=bStr, src1=vgpr(abReg+bk), src2=sgpr(tmpSgprX2,2), comment="")) |
| 6787 |
shiftK.add(TailLoop_SkipZeroOutMask) |
| 6788 |
if vgprPerInput == 4 and is_wmma_v2: |
| 6789 |
if tmpVgpr2 is not None: self.vgprPool.checkIn(tmpVgpr2) |
| 6790 |
if kernel["LocalSplitU"] > 1: |
| 6791 |
self.sgprPool.checkIn(loopCntSgpr) |
| 6792 |
else: |
| 6793 |
iui = 0 |
| 6794 |
abReg = self.vgprPool.checkOutAligned(2, 2, "abReg") |
| 6795 |
with self.allocTmpSgpr(3) as tmpSgprInfo: |
| 6796 |
sgprShift = tmpSgprInfo.idx |
| 6797 |
sgpr64bIdx = sgprShift + 1 |
| 6798 |
sgprMask = sgprShift + 2 |
| 6799 |
|
| 6800 |
shiftK.add(SSubI32(dst=sgpr(sgprShift), src0=sgpr(loopCounterName), src1=1, comment="calculate 64bit groups index")) |
| 6801 |
shiftK.add(SLShiftRightB32(dst=sgpr(sgpr64bIdx), src=sgpr(sgprShift), shiftHex=log2(64 // (numRegistersIn * 32)), comment="calculate 64bit groups index")) |
| 6802 |
shiftK.add(SAndB32(dst=sgpr(sgprShift), src0=sgpr(sgprShift), src1=int((64 // (numRegistersIn * 32))-1), comment="calculate shift value")) |
| 6803 |
shiftK.add(SSubI32(dst=sgpr(sgprShift), src0=int((64 // (numRegistersIn * 32))-1), src1=sgpr(sgprShift), comment="calculate shift value")) |
| 6804 |
shiftK.add(SLShiftLeftB32(dst=sgpr(sgprShift), shiftHex=log2(numRegistersIn * 32), src=sgpr(sgprShift), comment="calculate shift value")) |
| 6805 |
|
| 6806 |
for it in range(int((kernel["MatrixInstK"] * numRegistersIn) // 2)): |
| 6807 |
shiftK.add(VCmpEQI32(dst=sgpr(sgprMask), src0=sgpr(sgpr64bIdx), src1=it, comment='handle this 64bit group: part 1')) |
| 6808 |
for a in range(0, kernel["MIWaveTileA"]): |
| 6809 |
aStr = vgpr("ValuA_X%u_I%u+%u+%u" % (m, iui, a*vgprPerInput, it*2), 2) |
| 6810 |
shiftK.add(VLShiftLeftB64(dst=vgpr(abReg,2), shiftHex=sgpr(sgprShift), src=aStr, comment=f"shfit for ValuA[{it*2}:{it*2+1}]")) |
| 6811 |
for bk in range(2): |
| 6812 |
aStr = vgpr("ValuA_X%u_I%u+%u+%u+%u" % (m, iui, a*vgprPerInput, it*2, bk)) |
| 6813 |
shiftK.add(VCndMaskB32(dst=aStr, src0=aStr, src1=vgpr(abReg+bk), src2=sgpr(sgprMask), comment="shift if in this 64b group")) |
| 6814 |
for b in range(0, kernel["MIWaveTileB"]): |
| 6815 |
bStr = vgpr("ValuB_X%u_I%u+%u+%u" % (m, iui, b*vgprPerInput, it*2), 2) |
| 6816 |
shiftK.add(VLShiftLeftB64(dst=vgpr(abReg,2), shiftHex=sgpr(sgprShift), src=bStr, comment=f"shfit for ValuB[{it*2}:{it*2+1}]")) |
| 6817 |
for bk in range(2): |
| 6818 |
bStr = vgpr("ValuB_X%u_I%u+%u+%u+%u" % (m, iui, b*vgprPerInput, it*2, bk)) |
| 6819 |
shiftK.add(VCndMaskB32(dst=bStr, src0=bStr, src1=vgpr(abReg+bk), src2=sgpr(sgprMask), comment="shift if in this 64b group")) |
| 6820 |
if it > 0: |
| 6821 |
shiftK.add(VCmpLtI32(dst=sgpr(sgprMask), src0=sgpr(sgpr64bIdx), src1=it, comment='handle this 64bit group: part 2')) |
| 6822 |
for a in range(0, kernel["MIWaveTileA"]): |
| 6823 |
for bk in range(2): |
| 6824 |
aStr = vgpr("ValuA_X%u_I%u+%u+%u+%u" % (m, iui, a*vgprPerInput, it*2, bk)) |
| 6825 |
shiftK.add(VCndMaskB32(dst=aStr, src0=aStr, src1=0, src2=sgpr(sgprMask), comment="shift if in this 64b group")) |
| 6826 |
for b in range(0, kernel["MIWaveTileB"]): |
| 6827 |
for bk in range(2): |
| 6828 |
bStr = vgpr("ValuB_X%u_I%u+%u+%u+%u" % (m, iui, b*vgprPerInput, it*2, bk)) |
| 6829 |
shiftK.add(VCndMaskB32(dst=bStr, src0=bStr, src1=0, src2=sgpr(sgprMask), comment="shift if in this 64b group")) |
| 6830 |
|
| 6831 |
s_nop = 2 |
| 6832 |
|
| 6833 |
if s_nop != 0: |
| 6834 |
imod.add(SNop(waitState=(s_nop - 1), comment="")) |
| 6835 |
|
| 6836 |
prevAccIdx = -1 |
| 6837 |
for iui in range(0, innerUnroll): |
| 6838 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 6839 |
iuiM_new = (iui//self.states.numReadsIterCoalescedMetadata)*self.states.numReadsIterCoalescedMetadata |
| 6840 |
iuiM_new_offset = iui%self.states.numReadsIterCoalescedMetadata*vgprPerInputM |
| 6841 |
|
| 6842 |
zgemmVaddSrcCheck = [[], [], []] |
| 6843 |
outer = 1 |
| 6844 |
loopSwap = False |
| 6845 |
|
| 6846 |
|
| 6847 |
if kernel["ProblemType"]["DataType"].isComplex() and tPB["tile01Idx"]: |
| 6848 |
outer = 0 |
| 6849 |
loopSwap = True |
| 6850 |
inner = 1 - outer |
| 6851 |
for idxOuter in range(0, kernel["MIWaveTile"][outer]): |
| 6852 |
for idxInner in range(0, kernel["MIWaveTile"][inner]): |
| 6853 |
idx0 = idxInner |
| 6854 |
idx1 = idxOuter |
| 6855 |
if loopSwap: |
| 6856 |
idx0, idx1 = idx1, idx0 |
| 6857 |
accIdx = idx1 * kernel["MIWaveTile"][0] + idx0 |
| 6858 |
accStart = accIdx * accs_per_wave |
| 6859 |
accEnd = accStart + accs_per_wave - 1 |
| 6860 |
|
| 6861 |
idxA = idx0 if tPB["tile01Idx"] else idx1 |
| 6862 |
idxB = idx1 if tPB["tile01Idx"] else idx0 |
| 6863 |
aStr_base = self.generateSrcStrForMFMA(kernel, tPA, innerUnroll, vregSetIdx, vgprPerInputA, m, u, iui, idxA) |
| 6864 |
bStr_base = self.generateSrcStrForMFMA(kernel, tPB, innerUnroll, vregSetIdx, vgprPerInputB, m, u, iui, idxB) |
| 6865 |
aStr = vgpr(aStr_base, vgprPerInputA) |
| 6866 |
bStr = vgpr(bStr_base, vgprPerInputB) |
| 6867 |
Str0 = aStr if tPB["tile01Idx"] else bStr |
| 6868 |
Str1 = bStr if tPB["tile01Idx"] else aStr |
| 6869 |
|
| 6870 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 6871 |
idxM = idxB if kernel["ProblemType"]["Sparse"] == 2 else idxA |
| 6872 |
m_new = idxM*self.states.numReadsIterCoalescedMetadata |
| 6873 |
mStr = "ValuMetadata_X%u_I%u+%u+%u+%u" % (vgprBufferM_new, iuiM_new, m_new, vgprBufferM_new_offset, iuiM_new_offset) |
| 6874 |
mStr = vgpr(mStr, vgprPerInputM) |
| 6875 |
|
| 6876 |
if kernel["ProblemType"]["DataType"].isComplex(): |
| 6877 |
|
| 6878 |
|
| 6879 |
miInInstType = miOutInstType |
| 6880 |
ccA = kernel["ProblemType"]["ComplexConjugateA"] |
| 6881 |
ccB = kernel["ProblemType"]["ComplexConjugateB"] |
| 6882 |
ccVgprs = [None]*3 |
| 6883 |
ccInsts = [None]*3 |
| 6884 |
accImOffset = accVgprImagNumOffset(kernel) |
| 6885 |
accStartSrcImg = accStartSrc+accImOffset |
| 6886 |
accEndSrcImg = accStartSrcImg + accs_per_wave - 1 |
| 6887 |
|
| 6888 |
|
| 6889 |
ar_base = aStr_base |
| 6890 |
ai_base = ar_base + "+%u"%numRegistersOut |
| 6891 |
ar = vgpr(ar_base, numRegistersOut) |
| 6892 |
ai = vgpr(ai_base, numRegistersOut) |
| 6893 |
br_base = bStr_base |
| 6894 |
bi_base = br_base + "+%u"%numRegistersOut |
| 6895 |
br = vgpr(br_base, numRegistersOut) |
| 6896 |
bi = vgpr(bi_base, numRegistersOut) |
| 6897 |
minus_ar = ar.getMinus() |
| 6898 |
minus_ai = ai.getMinus() |
| 6899 |
if miOutInstType == InstType.INST_F32: |
| 6900 |
VAddX = VAddF32 |
| 6901 |
elif miOutInstType == InstType.INST_F64: |
| 6902 |
VAddX = VAddF64 |
| 6903 |
else: |
| 6904 |
printExit("Unsupported v_add type %s"%miOutInstType) |
| 6905 |
offsetVgpr = [0,0,0] |
| 6906 |
forceGenerate = ccA and ccB |
| 6907 |
if ccA == ccB: |
| 6908 |
arrayIndex = 0 |
| 6909 |
ccVgprs[arrayIndex] = self.vgprPool.checkOutAligned(numRegistersOut, numRegistersOut, "negate r1") |
| 6910 |
|
| 6911 |
if forceGenerate or (ai not in zgemmVaddSrcCheck[arrayIndex]): |
| 6912 |
ccInsts[arrayIndex] = VAddX(dst=vgpr(ccVgprs[arrayIndex] + offsetVgpr[arrayIndex], numRegistersOut), src0=minus_ai, src1=0, comment="Ai=-Ai") |
| 6913 |
zgemmVaddSrcCheck[arrayIndex].append(ai) |
| 6914 |
if ccA: |
| 6915 |
arrayIndex = 1 |
| 6916 |
ccVgprs[arrayIndex] = self.vgprPool.checkOutAligned(numRegistersOut, numRegistersOut, "negate i0") |
| 6917 |
|
| 6918 |
if forceGenerate or (ai not in zgemmVaddSrcCheck[arrayIndex]): |
| 6919 |
ccInsts[arrayIndex] = VAddX(dst=vgpr(ccVgprs[arrayIndex] + offsetVgpr[arrayIndex], numRegistersOut), src0=minus_ai, src1=0, comment="Ai=-Ai") |
| 6920 |
zgemmVaddSrcCheck[arrayIndex].append(ai) |
| 6921 |
if ccB: |
| 6922 |
arrayIndex = 2 |
| 6923 |
ccVgprs[arrayIndex] = self.vgprPool.checkOutAligned(numRegistersOut, numRegistersOut, "negate i1") |
| 6924 |
|
| 6925 |
if forceGenerate or (ar not in zgemmVaddSrcCheck[arrayIndex]): |
| 6926 |
ccInsts[arrayIndex] = VAddX(dst=vgpr(ccVgprs[arrayIndex] + offsetVgpr[arrayIndex], numRegistersOut), src0=minus_ar, src1=0, comment="Ar=-Ar") |
| 6927 |
zgemmVaddSrcCheck[arrayIndex].append(ar) |
| 6928 |
(src0, src1) = (br, ar) if kernel["SourceSwap"] else (ar, br) |
| 6929 |
for inst in ccInsts: |
| 6930 |
if inst is not None: |
| 6931 |
imod.add(inst) |
| 6932 |
variant = [kernel["MatrixInstM"], kernel["MatrixInstN"], kernel["MatrixInstK"], kernel["MatrixInstB"]] |
| 6933 |
imod.add(MFMAInstruction(instType=miInInstType, accType=miOutInstType, variant=variant, mfma1k=False, \ |
| 6934 |
acc=self.accVgprReadWriteIndex(kernel, accStart, (accEnd-accStart+1)), a=src0, b=src1, acc2=self.accVgprReadWriteIndex(kernel, accStart, (accEnd-accStart+1)), \ |
| 6935 |
comment="Cr += Ar*Br")) |
| 6936 |
(src0, src1) = (bi, (vgpr(ccVgprs[0] + offsetVgpr[0], numRegistersOut) if ccVgprs[0] else ai)) if kernel["SourceSwap"] else ((vgpr(ccVgprs[0] + offsetVgpr[0], numRegistersOut) if ccVgprs[0] else ai), bi) |
| 6937 |
imod.add(MFMAInstruction(instType=miInInstType, accType=miOutInstType, variant=variant, mfma1k=False, \ |
| 6938 |
acc=self.accVgprReadWriteIndex(kernel, (accStart+accStoreCIdx), (accEnd-accStart+1)), a=src0, b=src1, acc2=self.accVgprReadWriteIndex(kernel, accStart, (accEnd-accStart+1)), \ |
| 6939 |
comment="Cr += %sAi*Bi"%("-" if ccVgprs[0] else ""))) |
| 6940 |
(src0, src1) = (br, (vgpr(ccVgprs[1] + offsetVgpr[1], numRegistersOut) if ccVgprs[1] else ai)) if kernel["SourceSwap"] else ((vgpr(ccVgprs[1] + offsetVgpr[1], numRegistersOut) if ccVgprs[1] else ai), br) |
| 6941 |
imod.add(MFMAInstruction(instType=miInInstType, accType=miOutInstType, variant=variant, mfma1k=False, \ |
| 6942 |
acc=self.accVgprReadWriteIndex(kernel, (accStart+accImOffset), (accEnd-accStart+1)), a=src0, b=src1, acc2=self.accVgprReadWriteIndex(kernel, accStartSrcImg, (accEndSrcImg-accStartSrcImg+1)), \ |
| 6943 |
comment="Ci += %sAi*Br"%("-" if ccVgprs[1] else ""))) |
| 6944 |
(src0, src1) = (bi, (vgpr(ccVgprs[2] + offsetVgpr[2], numRegistersOut) if ccVgprs[2] else ar)) if kernel["SourceSwap"] else ((vgpr(ccVgprs[2] + offsetVgpr[2], numRegistersOut) if ccVgprs[2] else ar), bi) |
| 6945 |
imod.add(MFMAInstruction(instType=miInInstType, accType=miOutInstType, variant=variant, mfma1k=False, \ |
| 6946 |
acc=self.accVgprReadWriteIndex(kernel, (accStart+accImOffset+accStoreCIdx), (accEnd-accStart+1)), a=src0, b=src1, acc2=self.accVgprReadWriteIndex(kernel, accStartSrcImg, (accEndSrcImg-accStartSrcImg+1)), \ |
| 6947 |
comment="Ci += %sAr*Bi"%("-" if ccVgprs[2] else ""))) |
| 6948 |
for v in ccVgprs: |
| 6949 |
if v is not None: self.vgprPool.checkIn(v) |
| 6950 |
else: |
| 6951 |
|
| 6952 |
if kernel["SourceSwap"]: |
| 6953 |
src0 = Str1 |
| 6954 |
src1 = Str0 |
| 6955 |
else: |
| 6956 |
src0 = Str0 |
| 6957 |
src1 = Str1 |
| 6958 |
|
| 6959 |
variant = [kernel["MatrixInstM"], kernel["MatrixInstN"], kernel["MatrixInstK"], kernel["MatrixInstB"]] |
| 6960 |
|
| 6961 |
waits = self.mfmaIter_waitCount(kernel) |
| 6962 |
if waits > 0 and prevAccIdx == accIdx: |
| 6963 |
imod.add(SNop(waits - 1, "Wait for C")) |
| 6964 |
if(kernel["ProblemType"]["Sparse"]): |
| 6965 |
if kernel["DirectToVgprSparseMetadata"]: |
| 6966 |
miWaveTile = kernel["MIWaveTileB"] if kernel["ProblemType"]["Sparse"] == 2 else kernel["MIWaveTileA"] |
| 6967 |
idx = idx1 if kernel["ProblemType"]["Sparse"] == 2 else idx0 |
| 6968 |
accInStart = miWaveTile * kernel["LoopIters"] * unrollLoopIdx + idx * kernel["LoopIters"] + unrollIdx |
| 6969 |
imod.add(SMFMAInstruction(instType=miInInstType, accType=miOutInstType, variant=variant, mfma1k=mfma_1k, \ |
| 6970 |
acc=self.accVgprReadWriteIndex(kernel, (accStart+accStoreCIdx), (accEnd-accStart+1)), \ |
| 6971 |
a=src0, b=src1, metadata=vgpr("ValuMetadata+%u"%(accInStart)), \ |
| 6972 |
comment="left value = %s[%u+%u:%u+%u]" % (accumRegType, accStart, accStoreCIdx, accEnd, accStoreCIdx))) |
| 6973 |
else: |
| 6974 |
imod.add(SMFMAInstruction(instType=miInInstType, accType=miOutInstType, variant=variant, mfma1k=mfma_1k, \ |
| 6975 |
acc=self.accVgprReadWriteIndex(kernel, (accStart+accStoreCIdx), (accEnd-accStart+1)), \ |
| 6976 |
a=src0, b=src1, metadata=mStr, \ |
| 6977 |
comment="left value = %s[%u+%u:%u+%u]" % (accumRegType, accStart, accStoreCIdx, accEnd, accStoreCIdx))) |
| 6978 |
else: |
| 6979 |
if kernel["UseF32XEmulation"]: |
| 6980 |
abOffsetStr = "+2" if kernel["MatrixInstM"] == 16 and kernel["MatrixInstK"] == 16 else "+4" |
| 6981 |
if kernel["SourceSwap"]: |
| 6982 |
src1_0 = vgpr(aStr_base[:-4], vgprPerInputA / 2) |
| 6983 |
src1_1 = vgpr(aStr_base[:-4] + abOffsetStr, vgprPerInputA / 2) |
| 6984 |
src0_0 = vgpr(bStr_base[:-4], vgprPerInputB / 2) |
| 6985 |
src0_1 = vgpr(bStr_base[:-4] + abOffsetStr, vgprPerInputB / 2) |
| 6986 |
else: |
| 6987 |
src1_0 = vgpr(bStr_base[:-4], vgprPerInputB / 2) |
| 6988 |
src1_1 = vgpr(bStr_base[:-4] + abOffsetStr, vgprPerInputB / 2) |
| 6989 |
src0_0 = vgpr(aStr_base[:-4], vgprPerInputA / 2) |
| 6990 |
src0_1 = vgpr(aStr_base[:-4] + abOffsetStr, vgprPerInputA / 2) |
| 6991 |
|
| 6992 |
|
| 6993 |
imod.add(MFMAInstruction(instType=InstType.INST_BF16, accType=miOutInstType, variant=variant, mfma1k=mfma_1k, \ |
| 6994 |
acc=self.accVgprReadWriteIndex(kernel, (accStart+accStoreCIdx), (accEnd-accStart+1)), \ |
| 6995 |
a=src0_0, b=src1_1, acc2=self.accVgprReadWriteIndex(kernel, accStart, (accEnd-accStart+1)), neg=neg_flag,\ |
| 6996 |
comment="left value = %s[%u+%u:%u+%u]" % (accumRegType, accStart, accStoreCIdx, accEnd, accStoreCIdx))) |
| 6997 |
imod.add(MFMAInstruction(instType=InstType.INST_BF16, accType=miOutInstType, variant=variant, mfma1k=mfma_1k, \ |
| 6998 |
acc=self.accVgprReadWriteIndex(kernel, (accStart+accStoreCIdx), (accEnd-accStart+1)), \ |
| 6999 |
a=src0_1, b=src1_0, acc2=self.accVgprReadWriteIndex(kernel, accStart, (accEnd-accStart+1)), neg=neg_flag,\ |
| 7000 |
comment="left value = %s[%u+%u:%u+%u]" % (accumRegType, accStart, accStoreCIdx, accEnd, accStoreCIdx))) |
| 7001 |
imod.add(MFMAInstruction(instType=InstType.INST_BF16, accType=miOutInstType, variant=variant, mfma1k=mfma_1k, \ |
| 7002 |
acc=self.accVgprReadWriteIndex(kernel, (accStart+accStoreCIdx), (accEnd-accStart+1)), \ |
| 7003 |
a=src0_0, b=src1_0, acc2=self.accVgprReadWriteIndex(kernel, accStart, (accEnd-accStart+1)), neg=neg_flag,\ |
| 7004 |
comment="left value = %s[%u+%u:%u+%u]" % (accumRegType, accStart, accStoreCIdx, accEnd, accStoreCIdx))) |
| 7005 |
else: |
| 7006 |
imod.add(MFMAInstruction(instType=miInInstType, accType=miOutInstType, variant=variant, mfma1k=mfma_1k, \ |
| 7007 |
acc=self.accVgprReadWriteIndex(kernel, (accStart+accStoreCIdx), (accEnd-accStart+1)), \ |
| 7008 |
a=src0, b=src1, acc2=self.accVgprReadWriteIndex(kernel, accStart, (accEnd-accStart+1)), neg=neg_flag,\ |
| 7009 |
comment="left value = %s[%u+%u:%u+%u]" % (accumRegType, accStart, accStoreCIdx, accEnd, accStoreCIdx))) |
| 7010 |
prevAccIdx = accIdx |
| 7011 |
|
| 7012 |
if kernel["ExpertSchedulingMode"] > 0: |
| 7013 |
imod.add(SWaitAlu(va_vdst=0, comment="wait for the current iter's writes to complete")) |
| 7014 |
|
| 7015 |
|
| 7016 |
if kReg_first is not None: self.vgprPool.checkIn(kReg_first) |
| 7017 |
if kReg is not None: self.vgprPool.checkIn(kReg) |
| 7018 |
if abReg is not None: self.vgprPool.checkIn(abReg) |
| 7019 |
if tmpVgpr is not None: self.vgprPool.checkIn(tmpVgpr) |
| 7020 |
|
| 7021 |
mfmaMod = Module("mfmaCode") |
| 7022 |
if self.do["MAC"]: |
| 7023 |
shiftK.add(postShiftK) |
| 7024 |
mfmaMod.add(shiftK) |
| 7025 |
mfmaMod.add(imod) |
| 7026 |
|
| 7027 |
return mfmaMod |
| 7028 |
|
| 7029 |
|
| 7030 |
|
| 7031 |
|
| 7032 |
|
| 7033 |
|
| 7034 |
|
| 7035 |
def openSumAtLeastUnroll(self, kernel, prefetch, isOptNLL, isNGLL=False, NLLindex=0, NLLnum=1): |
| 7036 |
isLongBranch = False |
| 7037 |
if kernel["EnableMatrixInstruction"] and kernel["ProblemType"]["ActivationType"] in ['all', 'hipblaslt_all']: |
| 7038 |
acclen = getAccToArchLen(kernel) |
| 7039 |
|
| 7040 |
if acclen > 100 or (kernel["GlobalSplitUAlgorithm"] == 'MultipleBufferSingleKernel'): |
| 7041 |
isLongBranch = True |
| 7042 |
|
| 7043 |
module = Module("openSumAtLeastUnroll") |
| 7044 |
if prefetch: |
| 7045 |
if not isOptNLL: |
| 7046 |
module.add(self.checkLastIter(kernel)) |
| 7047 |
if kernel["StorePriorityOpt"]: |
| 7048 |
module.add(SSetPrior(prior=0, comment="optimization store")) |
| 7049 |
if self.states.doShadowInit: |
| 7050 |
shadowName = Label.getFormatting("ShadowInitStart") |
| 7051 |
module.add(SCBranchSCC1(labelName=shadowName, \ |
| 7052 |
comment="skip to ShadowInitStart iter b/c numIter==0")) |
| 7053 |
else: |
| 7054 |
|
| 7055 |
module.addComment1("after InitC, skip to end of prefetch last iter if numIter==0") |
| 7056 |
|
| 7057 |
if kernel["SuppressNoLoadLoop"]: |
| 7058 |
loopChar = self.states.indexChars[ \ |
| 7059 |
kernel["ProblemType"]["IndicesSummation"][self.states.unrollIdx]] |
| 7060 |
lastIterEnd = Label("LoopEnd%s"%loopChar, "") |
| 7061 |
module.add(SCBranchSCC1(labelName=lastIterEnd, \ |
| 7062 |
comment="skip to unrollLoop end loop%s iter b/c numIter==0" % loopChar)) |
| 7063 |
else: |
| 7064 |
lastIterEnd = Label("PrefetchGlobalLastIterEnd", "") |
| 7065 |
|
| 7066 |
with self.allocTmpSgpr(3) as tmpSgprInfo: |
| 7067 |
module.add(self.longBranchScc1(lastIterEnd, posNeg=1, tmpSgprInfo=tmpSgprInfo)) |
| 7068 |
|
| 7069 |
else: |
| 7070 |
if isOptNLL and NLLindex==0: |
| 7071 |
skipOptNLL = Label("OptNLL_End", "") |
| 7072 |
with self.allocTmpSgpr(4) as tmpSgprInfo: |
| 7073 |
tmpSgpr = tmpSgprInfo.idx |
| 7074 |
placeHolder="skipOptNLL_placeholder" if self.states.FactorDim == 3 else None |
| 7075 |
module.add(self.checkIsBetaZero(kernel, tmpSgprInfo, skipOptNLL, isLongBranch=isLongBranch, placeHolder=placeHolder, posNeg=1)) |
| 7076 |
|
| 7077 |
|
| 7078 |
if self.do["ApplyAlpha"]: |
| 7079 |
|
| 7080 |
if kernel["ProblemType"]["ComputeDataType"].isHalf(): |
| 7081 |
if kernel["ProblemType"]["HighPrecisionAccumulate"] and kernel["StreamK"]: |
| 7082 |
module.add(SCmpEQU32(src0=sgpr("Alpha"), src1=1.0, comment="Alpha == 1.0 ?")) |
| 7083 |
else: |
| 7084 |
|
| 7085 |
module.add(SMovB32(dst=sgpr(tmpSgpr), src=hex(0x3c003c00), comment="Packed alpha==1.0")) |
| 7086 |
module.add(SCmpEQU32(src0=sgpr("Alpha"), src1=sgpr(tmpSgpr), comment="alpha == 1.0?")) |
| 7087 |
|
| 7088 |
|
| 7089 |
|
| 7090 |
elif kernel["ProblemType"]["ComputeDataType"].isBFloat16(): |
| 7091 |
module.add(SMovB32(dst=sgpr(tmpSgpr), src=hex(0x3f803f80), comment="Packed alpha==1.0")) |
| 7092 |
module.add(SCmpEQU32(src0=sgpr("Alpha"), src1=sgpr(tmpSgpr), comment="alpha == 1.0?")) |
| 7093 |
|
| 7094 |
elif kernel["ProblemType"]["ComputeDataType"].isInt32(): |
| 7095 |
module.add(SCmpEQU32(src0=sgpr("Alpha"), src1=1, comment="Alpha == 1.0 ?")) |
| 7096 |
|
| 7097 |
|
| 7098 |
elif kernel["ProblemType"]["ComputeDataType"].isSingle(): |
| 7099 |
module.add(SCmpEQU32(src0=sgpr("Alpha"), src1=1.0, comment="Alpha == 1.0 ?")) |
| 7100 |
|
| 7101 |
elif kernel["ProblemType"]["ComputeDataType"].isDouble(): |
| 7102 |
module.add(SMovB32(dst=sgpr(tmpSgpr+0), src=hex(0x00000000), comment="Low part of double 1.0")) |
| 7103 |
module.add(SMovB32(dst=sgpr(tmpSgpr+1), src=hex(0x3ff00000), comment="High part of double 1.0")) |
| 7104 |
module.add(SCmpEQU64(src0=sgpr("Alpha",2), src1=sgpr(tmpSgpr,2), comment="Alpha == 1.0 ?")) |
| 7105 |
|
| 7106 |
elif kernel["ProblemType"]["ComputeDataType"].isSingleComplex(): |
| 7107 |
module.add(SMovB32(dst=sgpr(tmpSgpr+0), src=hex(1.0), comment="Real part of 1.0")) |
| 7108 |
module.add(SMovB32(dst=sgpr(tmpSgpr+1), src=hex(0.0), comment="Imaginary part of 1.0")) |
| 7109 |
module.add(SCmpEQU64(src0=sgpr("Alpha",2), src1=sgpr(tmpSgpr,2), comment="Alpha == 1.0 ?")) |
| 7110 |
|
| 7111 |
elif kernel["ProblemType"]["ComputeDataType"].isDoubleComplex(): |
| 7112 |
module.add(SMovB32(dst=sgpr(tmpSgpr+0), src=hex(0x00000000), comment="lsb of real part of 1.0")) |
| 7113 |
module.add(SMovB32(dst=sgpr(tmpSgpr+1), src=hex(0x3ff00000), comment="msb of real part of 1.0")) |
| 7114 |
module.add(SCmpEQU64(src0=sgpr("Alpha",2), src1=sgpr(tmpSgpr,2), comment="Alpha.real == 1.0 ?")) |
| 7115 |
if placeHolder == None: |
| 7116 |
module.add(SCBranchSCC0(labelName=skipOptNLL.getLabelName(), comment="branch if alpha.real != 1")) |
| 7117 |
else: |
| 7118 |
skipOptNLLModule = Module("skipOptNLL_placeholder") |
| 7119 |
skipOptNLLModule.addComment1("branch if alpha.real != 1") |
| 7120 |
module.add(skipOptNLLModule) |
| 7121 |
module.add(SMovB32(dst=sgpr(tmpSgpr+0), src=hex(0x00000000), comment="lsb of imag part of 0.0")) |
| 7122 |
module.add(SMovB32(dst=sgpr(tmpSgpr+1), src=hex(0x00000000), comment="msb of imag part of 0.0")) |
| 7123 |
module.add(SCmpEQU64(src0=sgpr("Alpha+2",2), src1=sgpr(tmpSgpr,2), comment="Alpha.imag == 0.0 ?")) |
| 7124 |
|
| 7125 |
if placeHolder == None: |
| 7126 |
if isLongBranch: |
| 7127 |
module.add(self.longBranchScc0(skipOptNLL, posNeg=1, tmpSgprInfo=tmpSgprInfo, comment="branch if alpha != 1")) |
| 7128 |
else: |
| 7129 |
module.add(SCBranchSCC0(labelName=skipOptNLL.getLabelName(), comment="branch if alpha != 1")) |
| 7130 |
else: |
| 7131 |
skipOptNLLModule = Module("skipOptNLL_placeholder") |
| 7132 |
skipOptNLLModule.addComment1("branch if alpha != 1") |
| 7133 |
module.add(skipOptNLLModule) |
| 7134 |
module.addSpaceLine() |
| 7135 |
|
| 7136 |
placeHolder = "skipOptNLL_scc1_placeholder" if self.states.FactorDim == 3 else None |
| 7137 |
module.add(self.checkIsEdge(kernel, tmpSgprInfo, skipOptNLL, skipOptNLL, isLongBranch=isLongBranch, placeHolder=placeHolder)) |
| 7138 |
module.addSpaceLine() |
| 7139 |
|
| 7140 |
|
| 7141 |
|
| 7142 |
if not kernel["NoTailLoop"]: |
| 7143 |
loopChar = self.states.indexChars[ \ |
| 7144 |
kernel["ProblemType"]["IndicesSummation"][self.states.unrollIdx]] |
| 7145 |
module.add(scalarStaticDivideAndRemainder(tmpSgpr, tmpSgpr+1, "SizesSum+%u"%self.states.unrollIdx, \ |
| 7146 |
kernel["DepthU"], ContinuousRegister(tmpSgpr+2, 2), 2)) |
| 7147 |
module.add(SCmpEQU32(src0=sgpr(tmpSgpr+1), src1=0, comment="numIter%s == 0"%loopChar )) |
| 7148 |
if placeHolder == None: |
| 7149 |
if isLongBranch: |
| 7150 |
module.add(self.longBranchScc0(skipOptNLL, posNeg=1, tmpSgprInfo=tmpSgprInfo, comment="skip if tail loop required")) |
| 7151 |
else: |
| 7152 |
module.add(SCBranchSCC0(labelName=skipOptNLL.getLabelName(), comment="skip if tail loop required")) |
| 7153 |
else: |
| 7154 |
skipOptNLLModule = Module("skipOptNLL_placeholder") |
| 7155 |
skipOptNLLModule.addComment1("skip if tail loop required") |
| 7156 |
module.add(skipOptNLLModule) |
| 7157 |
|
| 7158 |
|
| 7159 |
|
| 7160 |
self.savedVgprPool = deepcopy(self.vgprPool) |
| 7161 |
self.savedSgprPool = deepcopy(self.sgprPool) |
| 7162 |
|
| 7163 |
|
| 7164 |
|
| 7165 |
|
| 7166 |
|
| 7167 |
""" |
| 7168 |
added = [] # track registers added to pool |
| 7169 |
if kernel["PrefetchGlobalRead"]: |
| 7170 |
if not kernel["DirectToLdsA"]: |
| 7171 |
added.append(self.vgprPool.addRange(self.states.a.startVgprG2L, \ |
| 7172 |
self.states.a.startVgprG2L+self.states.a.numVgprG2L-1, "startOptNLL")) |
| 7173 |
added.append(self.vgprPool.addRange(self.states.a.startVgprLocalWriteAddr, \ |
| 7174 |
self.states.a.startVgprLocalWriteAddr, "startOptNLL")) |
| 7175 |
if not kernel["DirectToLdsB"]: |
| 7176 |
added.append(self.vgprPool.addRange(self.states.b.startVgprG2L, \ |
| 7177 |
self.states.b.startVgprG2L+self.states.b.numVgprG2L-1, "startOptNLL")) |
| 7178 |
added.append(self.vgprPool.addRange(self.states.b.startVgprLocalWriteAddr, \ |
| 7179 |
self.states.b.startVgprLocalWriteAddr, "startOptNLL")) |
| 7180 |
|
| 7181 |
if kernel["BufferLoad"]: |
| 7182 |
added.append(self.vgprPool.addRange(self.startVgprGlobalReadOffsetA, \ |
| 7183 |
self.startVgprGlobalReadOffsetB, "startOptNLL")) |
| 7184 |
else: |
| 7185 |
added.append(self.vgprPool.addRange(self.startVgprGlobalReadAddressesA, \ |
| 7186 |
self.startVgprGlobalReadAddressesB, "startOptNLL")) |
| 7187 |
module.addComment1("reclaim VGPRS: " + ", ".join(added)) |
| 7188 |
""" |
| 7189 |
|
| 7190 |
if (not isNGLL) and NLLnum == 2: |
| 7191 |
OptOrOrd = "Opt" if isOptNLL else "Ord" |
| 7192 |
loopLabel2ndNLL = Label("%sNLL_second"%(OptOrOrd), "second %s NoLoadLoop entry"%OptOrOrd ) |
| 7193 |
|
| 7194 |
if NLLindex == 0: |
| 7195 |
|
| 7196 |
module.add(SBitcmp1B32(src0=sgpr("OrigLoopCounter"), src1=0, comment="test if OrigLoopCounter is Odd ?")) |
| 7197 |
module.add(SCBranchSCC1(labelName=loopLabel2ndNLL.getLabelName(), comment="jump to second NoLoadLoop" )) |
| 7198 |
else: |
| 7199 |
module.add(loopLabel2ndNLL) |
| 7200 |
|
| 7201 |
return module |
| 7202 |
|
| 7203 |
|
| 7204 |
def closeSumAtLeastUnroll(self, kernel, tPA, tPB, prefetch, isOptNLL, isNGLL, isNotLast=False): |
| 7205 |
module = Module("closeSumAtLeastUnroll") |
| 7206 |
if not prefetch: |
| 7207 |
if isNGLL: |
| 7208 |
if kernel["ProblemType"]["Sparse"] and kernel["PrefetchGlobalRead"] == 2 and kernel["DirectToVgprSparseMetadata"]: |
| 7209 |
for i in range(0, self.states.m.numVgprValuPerBlock): |
| 7210 |
module.add(VMovB32(vgpr("ValuMetadata+%u"%i), vgpr("ValuMetadata+%u+%u"%(self.states.m.numVgprValuPerBlock, i)), \ |
| 7211 |
comment="copy ValuMetadata blk1 to blk0")) |
| 7212 |
if kernel["DirectToVgprA"] or kernel["DirectToVgprB"]: |
| 7213 |
|
| 7214 |
|
| 7215 |
loopIdx = self.states.unrollIdx |
| 7216 |
loopChar = self.states.indexChars[kernel["ProblemType"]["IndicesSummation"][loopIdx]] |
| 7217 |
loopCounter = self.loopCounter(kernel, loopIdx) |
| 7218 |
module.add(SSubU32(dst=loopCounter, src0=loopCounter, src1=1, comment="dec counter%s"%(loopChar))) |
| 7219 |
toPGR1 = Label(self.labels.getName("toPGR1"), "") |
| 7220 |
if isNotLast: |
| 7221 |
module.add(SBranch(labelName=toPGR1.getLabelName(), comment="Branch to toPGR1")) |
| 7222 |
else: |
| 7223 |
module.add(toPGR1) |
| 7224 |
else: |
| 7225 |
suffix = "OptNLL" if isOptNLL else "OrdNLL" |
| 7226 |
toPGR1end = Label(self.labels.getName("toPGR1end_%s"%suffix), "") |
| 7227 |
if isNotLast: |
| 7228 |
module.add(SBranch(labelName=toPGR1end.getLabelName(), comment="Branch to toPGR1end")) |
| 7229 |
else: |
| 7230 |
module.add(toPGR1end) |
| 7231 |
if isOptNLL: |
| 7232 |
endSumLabel = "Summation_End_OptNLL" |
| 7233 |
|
| 7234 |
module.addComment0("Stores for OptNLL") |
| 7235 |
module.add(self.endSummation(kernel, tPA, tPB, False, endSumLabel, isOptNLL)) |
| 7236 |
|
| 7237 |
|
| 7238 |
if kernel["NumWaveSplitK"] > 1: |
| 7239 |
module.add(self.waveSplitKReduction(kernel)) |
| 7240 |
|
| 7241 |
|
| 7242 |
assert (kernel["LocalSplitU"] == 1) |
| 7243 |
module.add(self.notLocalSplitUGlobalWriteIndices(kernel)) |
| 7244 |
|
| 7245 |
|
| 7246 |
(fullVw, elements, fullVw_1, elements_1) = self.notLocalFullTileElements(kernel, False) |
| 7247 |
alpha = False |
| 7248 |
beta = False |
| 7249 |
module.add(self.globalWriteElements(kernel, tPA, tPB, [fullVw], [fullVw_1], [elements], [elements_1], True, applyAlpha=alpha, betas=[beta], edges=[False])) |
| 7250 |
|
| 7251 |
self.cleanupGlobalWrite(kernel) |
| 7252 |
module.addSpaceLine() |
| 7253 |
module.add(self.functionEnd(kernel, addLabel=False)) |
| 7254 |
module.add(Label("OptNLL_End", "")) |
| 7255 |
|
| 7256 |
else: |
| 7257 |
module.add(Label("PrefetchGlobalLastIterEnd", "")) |
| 7258 |
|
| 7259 |
|
| 7260 |
def swapBackGprPool(gprPool, savedGprPool, isNotLast): |
| 7261 |
if savedGprPool != None and (not isNotLast): |
| 7262 |
|
| 7263 |
|
| 7264 |
newSize = gprPool.size() |
| 7265 |
savedGprPool.appendPool(newSize) |
| 7266 |
return savedGprPool, None |
| 7267 |
return gprPool, savedGprPool |
| 7268 |
|
| 7269 |
self.vgprPool, self.savedVgprPool = swapBackGprPool(self.vgprPool, self.savedVgprPool, isNotLast) |
| 7270 |
self.sgprPool, self.savedSgprPool = swapBackGprPool(self.sgprPool, self.savedSgprPool, isNotLast) |
| 7271 |
return module |
| 7272 |
|
| 7273 |
|
| 7274 |
|
| 7275 |
def incrementSrd(self, tP, incLower, incUpper): |
| 7276 |
imod = Module("incrementSrd") |
| 7277 |
tc = tP["tensorChar"] |
| 7278 |
|
| 7279 |
imod.add(SAddU32(dst=sgpr("Srd%s+0"%(tc)), \ |
| 7280 |
src0=sgpr("Srd%s+0"%(tc)), \ |
| 7281 |
src1=incLower, \ |
| 7282 |
comment="gra SRD += inc(lower)" )) |
| 7283 |
imod.add(SAddCU32(dst=sgpr("Srd%s+1"%(tc)), \ |
| 7284 |
src0=sgpr("Srd%s+1"%(tc)), \ |
| 7285 |
src1=incUpper, \ |
| 7286 |
comment="gra SRD += inc(upper)" )) |
| 7287 |
|
| 7288 |
|
| 7289 |
|
| 7290 |
if self.states.use64bShadowLimit: |
| 7291 |
imod.add(SSubU32(dst=sgpr("ShadowLimit%s+0"%tc), \ |
| 7292 |
src0=sgpr("ShadowLimit%s+0"%tc), \ |
| 7293 |
src1=incLower, \ |
| 7294 |
comment="limit -= inc)")) |
| 7295 |
imod.add(SSubBU32(dst=sgpr("ShadowLimit%s+1"%tc), \ |
| 7296 |
src0=sgpr("ShadowLimit%s+1"%tc), \ |
| 7297 |
src1=incUpper, \ |
| 7298 |
comment="limit -= inc)" )) |
| 7299 |
imod.add(SCmpEQU32(src0=sgpr("ShadowLimit%s+1"%tc), src1=0, comment="are we within 2^32?")) |
| 7300 |
if 1: |
| 7301 |
|
| 7302 |
imod.add(SCSelectB32(dst=sgpr("Srd%s+2"%tc), src0=sgpr("ShadowLimit%s+0"%tc), src1="BufferLimit", comment="Move shadow to real if we are within 2^32")) |
| 7303 |
else: |
| 7304 |
imod.add(SCMovB32(dst=sgpr("Srd%s+2"%tc), src=sgpr("ShadowLimit%s+0"%tc), comment="Move shadow to real if we are within 2^32")) |
| 7305 |
else: |
| 7306 |
imod.add(SSubU32(dst=sgpr("Srd%s+2"%(tc)), \ |
| 7307 |
src0=sgpr("Srd%s+2"%(tc)), \ |
| 7308 |
src1=incLower, \ |
| 7309 |
comment="limit -= inc)" )) |
| 7310 |
return imod |
| 7311 |
|
| 7312 |
def incrementMetadataSrd(self, incSparseLower, incSparseUpper): |
| 7313 |
imod = Module("incrementMetadataSrd") |
| 7314 |
|
| 7315 |
imod.add(SAddU32(sgpr("SrdMetadata+0"), \ |
| 7316 |
sgpr("SrdMetadata+0"), \ |
| 7317 |
incSparseLower, \ |
| 7318 |
"gra SRD += incSparse(lower)")) |
| 7319 |
imod.add(SAddCU32(sgpr("SrdMetadata+1"), \ |
| 7320 |
sgpr("SrdMetadata+1"), \ |
| 7321 |
incSparseUpper, \ |
| 7322 |
"gra SRD += incSparse(uppper)" )) |
| 7323 |
|
| 7324 |
|
| 7325 |
|
| 7326 |
if self.states.use64bShadowLimit: |
| 7327 |
imod.add(SSubU32(sgpr("ShadowLimitMetadata+0"), \ |
| 7328 |
sgpr("ShadowLimitMetadata+0"), \ |
| 7329 |
incSparseLower, \ |
| 7330 |
"limit -= incSparse(lower)")) |
| 7331 |
imod.add(SSubBU32(sgpr("ShadowLimitMetadata+1"), \ |
| 7332 |
sgpr("ShadowLimitMetadata+1"), \ |
| 7333 |
incSparseUpper, \ |
| 7334 |
"limit -= incSparse(uppper)" )) |
| 7335 |
imod.add(SCmpEQU32(sgpr("ShadowLimitMetadata+1"), 0, "are we within 2^32?")) |
| 7336 |
if 1: |
| 7337 |
|
| 7338 |
imod.add(SCSelectB32(dst=sgpr("SrdMetadata+2"), src0=sgpr("ShadowLimitMetadata+0"), src1="BufferLimit", comment="Move shadow to real if we are within 2^32")) |
| 7339 |
else: |
| 7340 |
imod.add(SCMovB32(sgpr("SrdMetadata+2"), sgpr("ShadowLimitMetadata+0"), "Move shadow to real if we are within 2^32")) |
| 7341 |
else: |
| 7342 |
imod.addInst(SSubU32(sgpr("SrdMetadata+2"), \ |
| 7343 |
sgpr("SrdMetadata+2"), \ |
| 7344 |
incSparseLower, \ |
| 7345 |
"limit -= inc)" )) |
| 7346 |
return imod |
| 7347 |
|
| 7348 |
|
| 7349 |
|
| 7350 |
def setTailSrd(self, tP, incLower): |
| 7351 |
|
| 7352 |
|
| 7353 |
|
| 7354 |
|
| 7355 |
|
| 7356 |
|
| 7357 |
|
| 7358 |
|
| 7359 |
|
| 7360 |
tc = tP["tensorChar"] |
| 7361 |
module = Module("setTailSrd") |
| 7362 |
incUpper = 0 |
| 7363 |
|
| 7364 |
module.add(SSubU32(dst=sgpr("Srd%s+0"%(tc)), \ |
| 7365 |
src0=sgpr("Srd%s+0"%(tc)), src1=incLower, \ |
| 7366 |
comment="gra SRD -= inc(lower)" )) |
| 7367 |
module.add(SSubBU32(dst=sgpr("Srd%s+1"%(tc)), \ |
| 7368 |
src0=sgpr("Srd%s+1"%(tc)), src1=incUpper, \ |
| 7369 |
comment="gra SRD -= inc(upper)" )) |
| 7370 |
|
| 7371 |
|
| 7372 |
assert(self.states.use64bShadowLimit) |
| 7373 |
|
| 7374 |
module.add(SAddU32(dst=sgpr("ShadowLimit%s+0"%tc), \ |
| 7375 |
src0=sgpr("ShadowLimit%s+0"%tc), src1=incLower, \ |
| 7376 |
comment="limit -= inc)")) |
| 7377 |
module.add(SAddCU32(dst=sgpr("ShadowLimit%s+1"%tc), \ |
| 7378 |
src0=sgpr("ShadowLimit%s+1"%tc), src1=incUpper, \ |
| 7379 |
comment="limit -= inc)" )) |
| 7380 |
module.add(SCmpEQU32(src0=sgpr("ShadowLimit%s+1"%tc), src1=0, comment="are we within 2^32?")) |
| 7381 |
module.add(SCMovB32(dst=sgpr("Srd%s+2"%tc), src=sgpr("ShadowLimit%s+0"%tc), comment="Move shadow to real if we are within 2^32")) |
| 7382 |
|
| 7383 |
return module |
| 7384 |
|
| 7385 |
|
| 7386 |
|
| 7387 |
|
| 7388 |
|
| 7389 |
|
| 7390 |
|
| 7391 |
|
| 7392 |
def globalReadIncrement(self, kernel, imod, loopIdx, tP, prefetchIndex): |
| 7393 |
if not self.do["GlobalInc"]: return "" |
| 7394 |
tc = tP["tensorChar"] |
| 7395 |
loopChar = self.states.indexChars[ \ |
| 7396 |
kernel["ProblemType"]["IndicesSummation"][loopIdx]] |
| 7397 |
|
| 7398 |
imod.addComment1("global read inc %s loop%s"%(tc,loopChar)) |
| 7399 |
|
| 7400 |
if kernel["BufferLoad"]: |
| 7401 |
|
| 7402 |
|
| 7403 |
|
| 7404 |
if loopIdx == self.states.unrollIdx: |
| 7405 |
|
| 7406 |
with self.allocTmpSgpr(4) as tmpSgprInfo: |
| 7407 |
incLower = tmpSgprInfo.idx |
| 7408 |
incUpper = incLower + 1 |
| 7409 |
tmpS = incLower + 2 |
| 7410 |
tmpIncSparse = incLower + 3 |
| 7411 |
suStr = "StaggerUIter" |
| 7412 |
tcOther = "B" if tP["isA"] else "A" |
| 7413 |
if kernel["PrefetchGlobalRead"] == 2 and (tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc] and (not kernel["DirectToVgpr%s"%tcOther]): |
| 7414 |
suStr += "DTV" |
| 7415 |
if prefetchIndex: |
| 7416 |
imod.add(SAddU32(dst=sgpr(tmpS), src0=self.loopCounter(kernel, self.states.unrollIdx), src1=prefetchIndex, comment="remove pf(%u)"%prefetchIndex)) |
| 7417 |
imod.add(SCmpEQU32(src0=sgpr(suStr), src1=sgpr(tmpS), comment="Is this wrapIter? (pf)")) |
| 7418 |
else: |
| 7419 |
imod.add(SCmpEQU32(src0=self.loopCounter(kernel, self.states.unrollIdx), \ |
| 7420 |
src1=sgpr(suStr), comment="Is this the wrapIter?")) |
| 7421 |
imod.add(SCSelectB32(dst=sgpr(incLower), src0=sgpr("WrapU%s+0"%tc), src1=sgpr("GlobalReadIncs%s+%u"%(tc,self.states.unrollIdx)), \ |
| 7422 |
comment="incLower <- ?")) |
| 7423 |
imod.add(SCSelectB32(dst=sgpr(incUpper), src0=sgpr("WrapU%s+1"%tc), src1=0, |
| 7424 |
comment="incUpper <- ?")) |
| 7425 |
imod.add(self.incrementSrd(tP, sgpr(incLower), sgpr(incUpper))) |
| 7426 |
|
| 7427 |
if kernel["ProblemType"]["Sparse"]: |
| 7428 |
if (kernel["ProblemType"]["Sparse"] == 2 and tP["isB"]) or (kernel["ProblemType"]["Sparse"] == 1 and tP["isA"]) : |
| 7429 |
tc = "Metadata" |
| 7430 |
if kernel["DirectToVgprSparseMetadata"]: |
| 7431 |
incSparse = tmpIncSparse |
| 7432 |
imod.add(self.calculateIncrementMetadata(kernel, incSparse)) |
| 7433 |
else: |
| 7434 |
incSparse = "GlobalReadIncs%s+%u"%(tc,self.states.unrollIdx) |
| 7435 |
if prefetchIndex: |
| 7436 |
imod.add(SCmpEQU32(src0=sgpr("StaggerUIter"), src1=sgpr(tmpS), comment="Is this wrapIter? (pf)")) |
| 7437 |
else: |
| 7438 |
imod.add(SCmpEQU32(src0=self.loopCounter(kernel, self.states.unrollIdx), \ |
| 7439 |
src1=sgpr("StaggerUIter"), comment="Is this the wrapIter?")) |
| 7440 |
imod.add(SCSelectB32(dst=sgpr(incLower), src0=sgpr("WrapU%s+0"%tc), src1=sgpr(incSparse), \ |
| 7441 |
comment="incLower <- ?")) |
| 7442 |
imod.add(SCSelectB32(dst=sgpr(incUpper), src0=sgpr("WrapU%s+1"%tc), src1=0, |
| 7443 |
comment="incUpper <- ?")) |
| 7444 |
if kernel["DirectToVgprSparseMetadata"]: |
| 7445 |
imod.add(self.incrementMetadataSrd(sgpr(incLower), sgpr(incUpper))) |
| 7446 |
else: |
| 7447 |
imod.add(self.incrementSrd(tP["tpsMetadata"], sgpr(incLower), sgpr(incUpper))) |
| 7448 |
|
| 7449 |
else: |
| 7450 |
if loopIdx != self.states.unrollIdx or (tc in ('A', 'B') and kernel["ProblemType"]["IndicesSummation"][self.states.unrollIdx] in kernel["ProblemType"]["MirrorDims%s"%tc]): |
| 7451 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 7452 |
incUpper = tmpSgprInfo.idx |
| 7453 |
|
| 7454 |
imod.add(SAShiftRightI32(dst=sgpr(incUpper), shiftHex=31, src=sgpr("GlobalReadIncs%s+%u"%(tc,loopIdx)), comment="sign-extend")) |
| 7455 |
imod.add(self.incrementSrd(tP, sgpr("GlobalReadIncs%s+%u"%(tc,loopIdx)), sgpr(incUpper))) |
| 7456 |
else: |
| 7457 |
incUpper = 0 |
| 7458 |
imod.add(self.incrementSrd(tP, sgpr("GlobalReadIncs%s+%u"%(tc,loopIdx)), hex(incUpper))) |
| 7459 |
|
| 7460 |
if kernel["ProblemType"]["Sparse"]: |
| 7461 |
if (kernel["ProblemType"]["Sparse"] == 2 and tP["isB"]) or (kernel["ProblemType"]["Sparse"] == 1 and tP["isA"]) : |
| 7462 |
tc = "Metadata" |
| 7463 |
imod.addComment1("global read inc metadata loop%s"%(loopChar)) |
| 7464 |
if kernel["DirectToVgprSparseMetadata"]: |
| 7465 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 7466 |
incSparse = tmpSgprInfo.idx |
| 7467 |
imod.add(self.calculateIncrementMetadata(kernel, incSparse)) |
| 7468 |
imod.add(self.incrementMetadataSrd(sgpr(incSparse), 0)) |
| 7469 |
else: |
| 7470 |
incSparse = "GlobalReadIncs%s+%u"%(tc,loopIdx) |
| 7471 |
if loopIdx != self.states.unrollIdx or (kernel["ProblemType"]["IndicesSummation"][self.states.unrollIdx] in kernel["ProblemType"]["MirrorDims%s"%tc]): |
| 7472 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 7473 |
incUpper = tmpSgprInfo.idx |
| 7474 |
|
| 7475 |
imod.add(SAShiftRightI32(dst=sgpr(incUpper), shiftHex=31, src=sgpr(incSparse), comment="sign-extend")) |
| 7476 |
imod.add(self.incrementMetadataSrd(sgpr(incSparse), sgpr(incUpper))) |
| 7477 |
else: |
| 7478 |
incUpper = 0 |
| 7479 |
imod.add(self.incrementSrd(tP["tpsMetadata"], sgpr(incSparse), hex(incUpper))) |
| 7480 |
|
| 7481 |
else: |
| 7482 |
graIdx = 0 |
| 7483 |
for _ in range(0, tP["nrp"]): |
| 7484 |
for _ in range(0, tP["nrpv"]): |
| 7485 |
for _ in range(0, tP["nrc"]): |
| 7486 |
for _ in range(0, tP["nrcv"]//tP["nrcvpi"]): |
| 7487 |
if self.states.globalReadIncsUseVgpr: |
| 7488 |
imod.add(VAddCOU32( \ |
| 7489 |
dst=vgpr("GlobalReadAddr%s+%u+0"%(tP["tensorChar"], graIdx)), \ |
| 7490 |
dst1=VCC(), \ |
| 7491 |
src0=vgpr("GlobalReadAddr%s+%u+0"%(tP["tensorChar"], graIdx)), \ |
| 7492 |
src1=vgpr("GlobalReadIncs%s+%u+0"%(tP["tensorChar"], 2*loopIdx)), \ |
| 7493 |
comment="gra += inc%s%s (lower)"%(tP["tensorChar"], loopChar))) |
| 7494 |
imod.add(VAddCCOU32( \ |
| 7495 |
dst=vgpr("GlobalReadAddr%s+%u+1"%(tP["tensorChar"], graIdx)), \ |
| 7496 |
dst1=VCC(), \ |
| 7497 |
src0=vgpr("GlobalReadAddr%s+%u+1"%(tP["tensorChar"], graIdx)), \ |
| 7498 |
src1=vgpr("GlobalReadIncs%s+%u+1"%(tP["tensorChar"], 2*loopIdx)), \ |
| 7499 |
src2=VCC(), \ |
| 7500 |
comment="gra += inc%s%s (upper)"%(tP["tensorChar"], loopChar))) |
| 7501 |
else: |
| 7502 |
imod.add(VAddCOU32( \ |
| 7503 |
dst=vgpr("GlobalReadAddr%s+%u+0"%(tP["tensorChar"], graIdx)), \ |
| 7504 |
dst1=VCC(), \ |
| 7505 |
src0=vgpr("GlobalReadAddr%s+%u+0"%(tP["tensorChar"], graIdx)), \ |
| 7506 |
src1=sgpr("GlobalReadIncs%s+%u"%(tP["tensorChar"], loopIdx)), \ |
| 7507 |
comment="gra += inc%s%s (lower)"%(tP["tensorChar"], loopChar))) |
| 7508 |
imod.add(VAddCCOU32( \ |
| 7509 |
dst=vgpr("GlobalReadAddr%s+%u+1"%(tP["tensorChar"], graIdx)), \ |
| 7510 |
dst1=VCC(), \ |
| 7511 |
src0=vgpr("GlobalReadAddr%s+%u+1"%(tP["tensorChar"], graIdx)), \ |
| 7512 |
src1=0, |
| 7513 |
src2=VCC(), \ |
| 7514 |
comment="gra += inc%s%s (upper)"%(tP["tensorChar"], loopChar))) |
| 7515 |
graIdx += self.states.rpga |
| 7516 |
|
| 7517 |
def globalReadIncrementAB(self, kernel, tPA, tPB, loopIdx, prefetchIndex): |
| 7518 |
imod = Module("globalReadIncrementAB") |
| 7519 |
|
| 7520 |
incCodeA = imod.add(Module("globalReadIncrementA")) |
| 7521 |
if tPA != None: |
| 7522 |
self.globalReadIncrement(kernel, incCodeA, loopIdx, tPA, prefetchIndex) |
| 7523 |
incCodeB = imod.add(Module("globalReadIncrementB")) |
| 7524 |
if tPB != None: |
| 7525 |
self.globalReadIncrement(kernel, incCodeB, loopIdx, tPB, prefetchIndex) |
| 7526 |
return imod |
| 7527 |
|
| 7528 |
|
| 7529 |
|
| 7530 |
|
| 7531 |
|
| 7532 |
|
| 7533 |
|
| 7534 |
def globalReadGuardK(self, kernel, tP, doTailOpt = 0, optParams = None): |
| 7535 |
module = Module("globalReadGuardK") |
| 7536 |
tc = tP["tensorChar"] |
| 7537 |
problemType = self.states.kernel["ProblemType"] |
| 7538 |
numElementsPer4Bytes = int(4 / tP["bpeGR"]) |
| 7539 |
isTr = (tc == "A" or tc == "B") and kernel["enableGLTr%s"%tc] |
| 7540 |
|
| 7541 |
|
| 7542 |
|
| 7543 |
|
| 7544 |
|
| 7545 |
if isTr: |
| 7546 |
|
| 7547 |
module.addComment1("Max read address offset for GLTr%s"%tc) |
| 7548 |
|
| 7549 |
maxGroVgpr = self.vgprPool.checkOut(1) |
| 7550 |
|
| 7551 |
tmpVgpr = self.vgprPool.checkOutAligned(2, 2, 'tmpVgpr') |
| 7552 |
tmpVgprRes = ContinuousRegister(tmpVgpr, 2) |
| 7553 |
|
| 7554 |
tmp = self.vgprPool.checkOut(1) |
| 7555 |
tmp2 = self.vgprPool.checkOut(1) |
| 7556 |
|
| 7557 |
WvG_M = kernel["MIWaveGroup"][0] |
| 7558 |
numKr = kernel["MatrixInstK"] // tP["glvw"] |
| 7559 |
|
| 7560 |
module.addComment0("calc last tile offset") |
| 7561 |
module.add(vectorStaticDivide(maxGroVgpr, "Serial", kernel["WavefrontSize"], tmpVgprRes)) |
| 7562 |
if tP["isA"]: |
| 7563 |
module.add(VAndB32(dst=vgpr(maxGroVgpr), src0=hex(WvG_M-1), src1=vgpr(maxGroVgpr), comment="GLTr%s: wave_id (along_M) mod MIWG[0]"%tc)) |
| 7564 |
module.add(VMulU32U24(dst=vgpr(maxGroVgpr), src0=numKr, src1=vgpr(maxGroVgpr), comment="GLTr%s: wave_id (along_M) *= numKr"%tc)) |
| 7565 |
elif tP["isB"]: |
| 7566 |
|
| 7567 |
|
| 7568 |
module.add(VLShiftRightB32(dst=vgpr(maxGroVgpr), shiftHex=log2(WvG_M), src=vgpr(maxGroVgpr), comment="GLTr%s: wave_id (along_N) /= MIWG[0]"%tc)) |
| 7569 |
module.add(VMulU32U24(dst=vgpr(maxGroVgpr), src0=numKr, src1=vgpr(maxGroVgpr), comment="GLTr%s: wave_id (along_N) *= numKr"%tc)) |
| 7570 |
|
| 7571 |
module.add(VBfeU32(dst=vgpr(tmp2), src0=vgpr("Serial"), src1=tP["bpeGR"]+1, src2=1, comment="GLTr%s: offset for the right half of the tile"%(tc))) |
| 7572 |
module.add(VAddU32(dst=vgpr(maxGroVgpr), src0=vgpr(tmp2), src1=vgpr(maxGroVgpr), comment="GLTr%s: wave_id += offset for the right half of the tile"%(tc))) |
| 7573 |
|
| 7574 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 7575 |
if tP["glvw"] > 1: |
| 7576 |
if tP["tlu"]: |
| 7577 |
module.add(vectorStaticMultiply(vgpr(maxGroVgpr), vgpr(maxGroVgpr), tP["glvw"], tmpSgprInfo, comment="GLTr%s: tile * glvw(%u)"%(tc, tP["glvw"]))) |
| 7578 |
else: |
| 7579 |
module.add(vectorStaticMultiply(vgpr(maxGroVgpr), vgpr(maxGroVgpr), tP["glvw"], tmpSgprInfo, comment="GLTr%s: unroll * glvw(%u)"%(tc, tP["glvw"]))) |
| 7580 |
|
| 7581 |
strideIdx = tP["lsc"] if tP["tlu"] else tP["lsp"] |
| 7582 |
stride = kernel[strideIdx] |
| 7583 |
module.add(VAddU32(dst=vgpr(maxGroVgpr), src0=vgpr(maxGroVgpr), src1=stride*(tP["nrt"]-1))) |
| 7584 |
|
| 7585 |
module.addComment0("calc last unroll offset") |
| 7586 |
module.add(VMovB32(dst=vgpr(tmp), src=sgpr("SizesSum+%u"%self.states.unrollIdx))) |
| 7587 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 7588 |
module.add(vectorStaticRemainder(tmp, tmp2, tmp, kernel["DepthU"], tmpVgprRes, tmpSgprInfo)) |
| 7589 |
|
| 7590 |
module.addComment0("final offset") |
| 7591 |
module.add(VSubU32(dst=vgpr(tmp2), src0=vgpr(tmp2), src1=1, comment="GLTr%s: unroll idx - 1"%(tc))) |
| 7592 |
bfArgs = (maxGroVgpr, maxGroVgpr, tmp2, tmp) |
| 7593 |
module.add(MacroInstruction(name="GLOBAL_OFFSET_%s"%tc, args=bfArgs)) |
| 7594 |
|
| 7595 |
module.addSpaceLine() |
| 7596 |
|
| 7597 |
self.vgprPool.checkIn(tmp) |
| 7598 |
self.vgprPool.checkIn(tmp2) |
| 7599 |
self.vgprPool.checkIn(tmpVgpr) |
| 7600 |
elif not kernel["BufferLoad"]: |
| 7601 |
with self.allocTmpSgpr(2) as tmpSgprInfo: |
| 7602 |
tmpSgpr = tmpSgprInfo.idx |
| 7603 |
maxAddrSgpr = tmpSgpr |
| 7604 |
module.addComment0("flat addressing - max read address = size[n] * stride[n-1]") |
| 7605 |
dim = len(tP["ia"])-1 |
| 7606 |
sizeIdx = tP["ia"][dim] |
| 7607 |
sizeIdxIsSum = sizeIdx in kernel["ProblemType"]["IndicesSummation"] |
| 7608 |
if sizeIdxIsSum: |
| 7609 |
sizeIdx -= kernel["ProblemType"]["NumIndicesC"] |
| 7610 |
|
| 7611 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(maxAddrSgpr+0), sgpr(maxAddrSgpr+1), \ |
| 7612 |
sgpr("Sizes%s+%u"%("Sum" if sizeIdxIsSum else "Free", sizeIdx)), \ |
| 7613 |
sgpr("Stride%s%s"%(tc, self.states.indexChars[tP['ia'][-1]])), \ |
| 7614 |
comment="64b tensor%s size in elements"%tc)) |
| 7615 |
if log2(tP["bpeGR"]) > 0: |
| 7616 |
module.add(SLShiftLeftB64(dst=sgpr(maxAddrSgpr,2), src=sgpr(maxAddrSgpr,2), \ |
| 7617 |
shiftHex=hex(log2(tP["bpeGR"])), comment="<- tensor%s size in bytes"%tc)) |
| 7618 |
else: |
| 7619 |
module.addCommentAlign("<- tensor%s size in bytes (byte is 1, do nothing)") |
| 7620 |
module.add(SAddU32( |
| 7621 |
dst=sgpr(maxAddrSgpr+0), \ |
| 7622 |
src0=sgpr(self.sgprs["AddressA"] if tP["isA"] else self.sgprs["AddressB"]), \ |
| 7623 |
src1=sgpr(maxAddrSgpr+0), \ |
| 7624 |
comment="prepend address lower")) |
| 7625 |
module.add(SAddCU32( |
| 7626 |
dst=sgpr(maxAddrSgpr+1), \ |
| 7627 |
src0=sgpr((self.sgprs["AddressA"] if tP["isA"] else self.sgprs["AddressB"])+1), \ |
| 7628 |
src1=sgpr(maxAddrSgpr+1), \ |
| 7629 |
comment="prepend address upper")) |
| 7630 |
|
| 7631 |
maxAddrVgpr = self.vgprPool.checkOutAligned(2, 2, "maxAddrVgpr") |
| 7632 |
module.add(VMovB32(dst=vgpr(maxAddrVgpr+0), src=sgpr(maxAddrSgpr+0), comment="sgpr->vgpr")) |
| 7633 |
module.add(VMovB32(dst=vgpr(maxAddrVgpr+1), src=sgpr(maxAddrSgpr+1), comment="sgpr->vgpr")) |
| 7634 |
|
| 7635 |
|
| 7636 |
fullExec = tmpSgpr |
| 7637 |
sgprCnt = self.states.laneSGPRCount |
| 7638 |
waveSize = kernel["WavefrontSize"] |
| 7639 |
activeMask = "0xFFFFFFFF" if (waveSize == 32) else "0xFFFFFFFFFFFFFFFF" |
| 7640 |
SMovBX = SMovB32 if (waveSize == 32) else SMovB64 |
| 7641 |
module.add(SMovBX(dst=sgpr(fullExec,sgprCnt), src=activeMask, comment="to restore all threads active")) |
| 7642 |
bpeVgpr = self.vgprPool.checkOut(1, "bpeVgpr") |
| 7643 |
module.add(VMovB32(dst=vgpr(bpeVgpr), src=hex(tP["bpeGR"]), comment="bpeGR")) |
| 7644 |
|
| 7645 |
|
| 7646 |
zeroVgpr = self.vgprPool.checkOut(1,"zeroVgpr") |
| 7647 |
module.add(VMovB32(dst=vgpr(zeroVgpr), src=0, comment="zero")) |
| 7648 |
|
| 7649 |
def globalReadGuardKBody(tP, optParams = None): |
| 7650 |
if optParams != None: |
| 7651 |
jumpLabel = optParams.jumpLabel |
| 7652 |
idx = optParams.idx |
| 7653 |
tmpVgpr = optParams.tmpVgpr |
| 7654 |
kLabelsList = optParams.kLabelsList |
| 7655 |
behavior = optParams.behavior |
| 7656 |
kSgpr = optParams.kSgpr |
| 7657 |
vgprSlot = optParams.vgprSlot |
| 7658 |
periodParam = optParams.periodParam |
| 7659 |
loadNum = optParams.loadNum |
| 7660 |
preDirectToLdsLoads = optParams.preDirectToLdsLoads |
| 7661 |
finalLoop = optParams.finalLoop |
| 7662 |
else: |
| 7663 |
jumpLabel = None |
| 7664 |
idx = 0 |
| 7665 |
tmpVgpr = None |
| 7666 |
kLabelsList = [] |
| 7667 |
behavior = "" |
| 7668 |
kSgpr = None |
| 7669 |
vgprSlot = [] |
| 7670 |
periodParam = [] |
| 7671 |
loadNum = 0 |
| 7672 |
preDirectToLdsLoads = 0 |
| 7673 |
finalLoop = 0 |
| 7674 |
|
| 7675 |
tc = tP["tensorChar"] |
| 7676 |
self.vgprs.globalReadRegisters[tc] = [] |
| 7677 |
|
| 7678 |
tcDataType = "" if tc == "Metadata" else tc |
| 7679 |
graIdx = 0 |
| 7680 |
g2lIdx = 0 |
| 7681 |
loadWidth = tP["globalReadInstruction"].totalWidth |
| 7682 |
|
| 7683 |
glvwWorkaround = 8 * kernel["ProblemType"]["DataType"].numRegisters() |
| 7684 |
dataType = kernel["ProblemType"]["DataType"] if tP["glvw"] < glvwWorkaround else kernel["ProblemType"]["DataType%s"%tcDataType] |
| 7685 |
|
| 7686 |
isGlc = bool(tP["NonTemporal"] & 0x1) |
| 7687 |
isSlc = bool(tP["NonTemporal"] & 0x2) |
| 7688 |
isNT = bool(tP["NonTemporal"] & 0x4) |
| 7689 |
isLds = True if (kernel["DirectToLds%s"%tc] and not kernel["NonDTLTailLoop%s"%tc]) else False |
| 7690 |
isTr = (tc == "A" or tc == "B") and kernel["enableGLTr%s"%tc] |
| 7691 |
is16b = dataType.isHalf() or dataType.isBFloat16() |
| 7692 |
|
| 7693 |
directToLdsLoads = 0 |
| 7694 |
if doTailOpt == 2 and behavior == "LOAD": |
| 7695 |
directToLdsLoads += preDirectToLdsLoads |
| 7696 |
prevLdsOffset = 0 |
| 7697 |
|
| 7698 |
instOffset = 0 |
| 7699 |
loopCnt = -1 |
| 7700 |
vgprIdx = 0 |
| 7701 |
loadCnt = 0 |
| 7702 |
perpStart = periodParam[0] if doTailOpt == 2 else 0 |
| 7703 |
perpEnd = periodParam[1] if doTailOpt == 2 else tP["nrp"] |
| 7704 |
sPerpStart = periodParam[2] if doTailOpt == 2 else 0 |
| 7705 |
sPerpEnd = periodParam[3] if doTailOpt == 2 else tP["nrpv"] |
| 7706 |
paraStart = periodParam[4] if doTailOpt == 2 else 0 |
| 7707 |
paraEnd = periodParam[5] if doTailOpt == 2 else tP["nrc"] |
| 7708 |
sParaStart = periodParam[6] if doTailOpt == 2 else 0 |
| 7709 |
sParaEnd = periodParam[7] if doTailOpt == 2 else (tP["nrcv"]//tP["nrcvpi"]) |
| 7710 |
rStart = periodParam[8] if doTailOpt == 2 else 0 |
| 7711 |
rEnd = periodParam[9] if doTailOpt == 2 else 0 |
| 7712 |
|
| 7713 |
requiredSgprs = min(self.sgprPool.size() + 5, self.states.regCaps["MaxSgpr"]) |
| 7714 |
maxVgprs, occupancy = self.getMaxRegsForOccupancy(kernel["NumThreads"], self.vgprPool.size(), requiredSgprs, \ |
| 7715 |
self.getLdsSize(kernel), self.agprPool.size(), self.states.doubleVgpr) |
| 7716 |
|
| 7717 |
tmpVgprPool = [] |
| 7718 |
packInst = Module() |
| 7719 |
maxNumTempToUse = max(maxVgprs - self.vgprPool.size(), 1) |
| 7720 |
|
| 7721 |
for perp in range(perpStart, perpEnd): |
| 7722 |
for sPerp in range(sPerpStart, sPerpEnd): |
| 7723 |
for para in range(paraStart, paraEnd): |
| 7724 |
for sPara in range(sParaStart, sParaEnd): |
| 7725 |
i = sPara + (tP["nrcv"] // tP["nrcvpi"]) * (para + tP["nrc"] * (sPerp + tP["nrpv"] * perp)) |
| 7726 |
loopCnt = sPara + para * sParaEnd + sPerp * paraEnd * sParaEnd + perp * sPerpEnd * paraEnd * sParaEnd |
| 7727 |
|
| 7728 |
graIdx = i * self.states.rpgo if kernel["BufferLoad"] else i * self.states.rpga |
| 7729 |
g2lIdx = i * loadWidth * tP["bpeRatio"] |
| 7730 |
if (tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc] and kernel["ConvertAfterDS"] and not isTr: |
| 7731 |
|
| 7732 |
if tP["bpe"] > tP["bpeGR"]: |
| 7733 |
g2lIdx *= tP["bpe"] // tP["bpeGR"] |
| 7734 |
|
| 7735 |
destVgprHi = None |
| 7736 |
tmpVgprIdx = tmpVgpr |
| 7737 |
dataIsByte = False |
| 7738 |
packInt8Code = None |
| 7739 |
eccOffset = 0 |
| 7740 |
|
| 7741 |
instOffsetInc = 0 |
| 7742 |
|
| 7743 |
r = 0 |
| 7744 |
numLoadVectorComp = int(loadWidth*self.states.bpr//tP["bpeGR"]) |
| 7745 |
|
| 7746 |
if (kernel["ProblemType"]["DataType%s"%tcDataType].isDouble() or kernel["ProblemType"]["DataType%s"%tcDataType].isSingle())\ |
| 7747 |
and kernel["BufferLoad"]: |
| 7748 |
|
| 7749 |
|
| 7750 |
|
| 7751 |
numLoadVectorComp = numLoadVectorComp // kernel["GlobalReadVectorWidth%c"%tc] |
| 7752 |
|
| 7753 |
if isLds or isTr: |
| 7754 |
numLoadVectorComp = 1 |
| 7755 |
|
| 7756 |
int8TempVgpr = numLoadVectorComp - 1 |
| 7757 |
|
| 7758 |
|
| 7759 |
while r < numLoadVectorComp: |
| 7760 |
if doTailOpt == 1 and i == idx and ((r+1) % numElementsPer4Bytes != 0): |
| 7761 |
if kLabelsList != None: |
| 7762 |
module.add(kLabelsList.pop()) |
| 7763 |
module.add(SCmpGeU32(src0=sgpr(kSgpr), src1=(r + 1), comment="")) |
| 7764 |
module.add(SCBranchSCC0(labelName=jumpLabel.getLabelName(), comment="")) |
| 7765 |
|
| 7766 |
numElementsPerLoad = 1 |
| 7767 |
|
| 7768 |
if kernel["ConvertAfterDS"]: |
| 7769 |
dataType = kernel["ProblemType"]["DataType%s"%tcDataType] |
| 7770 |
if dataType.isInt8() or dataType.is8bitFloat() or tP["isM"]: |
| 7771 |
|
| 7772 |
|
| 7773 |
|
| 7774 |
|
| 7775 |
|
| 7776 |
|
| 7777 |
if isTr: |
| 7778 |
numElementsPerLoad = kernel["GlobalReadVectorWidth%c"%tc] |
| 7779 |
if (not tP["isM"]) and isLds: |
| 7780 |
if not kernel["NonDTLTailLoop%c"%tc]: |
| 7781 |
numElementsPerLoad = kernel["GlobalReadVectorWidth%c"%tc] |
| 7782 |
dataIsByte = False |
| 7783 |
else: |
| 7784 |
dataIsByte = True |
| 7785 |
|
| 7786 |
if doTailOpt == 1: |
| 7787 |
if r == 1: |
| 7788 |
packInt8Code = Module() |
| 7789 |
destVgprHi = tmpVgprIdx |
| 7790 |
else: |
| 7791 |
if doTailOpt == 2: |
| 7792 |
if r == 1: |
| 7793 |
packInt8Code = Module() |
| 7794 |
if r != 0: |
| 7795 |
destVgprHi = vgprSlot[vgprIdx] |
| 7796 |
if r >= rStart and r < rEnd and (r % 4 != 0): |
| 7797 |
vgprIdx = vgprIdx + 1 |
| 7798 |
elif r == 1: |
| 7799 |
packInt8Code = Module() |
| 7800 |
destVgprHi = self.vgprPool.checkOut( int8TempVgpr , 'destVgprHi') |
| 7801 |
regIdx = r // 4 |
| 7802 |
if (tP["localWriteInstruction"].blockWidth <= 0.5) and (r%2 == 0) and not tP["isM"]: |
| 7803 |
numVgprG2L = self.states.a.numVgprG2L if tc == 'A' else self.states.b.numVgprG2L |
| 7804 |
eccBpe = tP["bpeDS"] if kernel["ConvertAfterDS"] else max(tP["bpeGR"], tP["bpe"]) |
| 7805 |
eccOffset = _getEccOffset(tP["globalReadInstruction"].totalWidth, bpr=self.states.bpr, bpe=eccBpe, \ |
| 7806 |
glvw=tP["glvw"], idx=loopCnt, numVgprG2L=numVgprG2L) |
| 7807 |
elif dataType.isHalf() or dataType.isBFloat16(): |
| 7808 |
if isTr: |
| 7809 |
numElementsPerLoad = kernel["GlobalReadVectorWidth%c"%tc] |
| 7810 |
elif tP["glvw"]>1 and kernel["AssertSummationElementMultiple"] % 2 == 0 and not isLds: |
| 7811 |
|
| 7812 |
numElementsPerLoad = 2 |
| 7813 |
elif isLds and not kernel["NonDTLTailLoop%c"%tc]: |
| 7814 |
numElementsPerLoad = kernel["GlobalReadVectorWidth%c"%tc] |
| 7815 |
elif self.states.archCaps["HasEccHalf"]: |
| 7816 |
|
| 7817 |
|
| 7818 |
|
| 7819 |
if doTailOpt == 1: |
| 7820 |
destVgprHi = tmpVgprIdx |
| 7821 |
else: |
| 7822 |
if (tP["localWriteInstruction"].blockWidth == 0.5) and (r%2 == 0): |
| 7823 |
numVgprG2L = self.states.a.numVgprG2L if tc == 'A' else self.states.b.numVgprG2L |
| 7824 |
eccBpe = tP["bpeDS"] if kernel["ConvertAfterDS"] else max(tP["bpeGR"], tP["bpe"]) |
| 7825 |
eccOffset = _getEccOffset(tP["globalReadInstruction"].totalWidth, bpr=self.states.bpr, bpe=eccBpe, \ |
| 7826 |
glvw=tP["glvw"], idx=loopCnt, numVgprG2L=numVgprG2L) |
| 7827 |
else: |
| 7828 |
if doTailOpt == 2: |
| 7829 |
destVgprHi = vgprSlot[vgprIdx] |
| 7830 |
if r >= rStart and r < rEnd and (r % 2 != 0): |
| 7831 |
vgprIdx = vgprIdx + 1 |
| 7832 |
else: |
| 7833 |
if r % 2 == 1: |
| 7834 |
tmpVgprPool.append(self.vgprPool.checkOut(1, 'destVgprHi')) |
| 7835 |
destVgprHi = tmpVgprPool[-1] |
| 7836 |
regIdx = r // 2 |
| 7837 |
elif dataType.isInt8x4() or dataType.isSingle(): |
| 7838 |
|
| 7839 |
if kernel["BufferLoad"]: |
| 7840 |
numElementsPerLoad = kernel["GlobalReadVectorWidth%c"%tc] |
| 7841 |
regIdx = r |
| 7842 |
elif dataType.isDouble(): |
| 7843 |
|
| 7844 |
if kernel["BufferLoad"]: |
| 7845 |
numElementsPerLoad = kernel["GlobalReadVectorWidth%c"%tc] |
| 7846 |
regIdx = r*2 |
| 7847 |
elif dataType.isSingleComplex(): |
| 7848 |
regIdx = r*2 |
| 7849 |
elif dataType.isDoubleComplex() : |
| 7850 |
regIdx = r*4 |
| 7851 |
else: |
| 7852 |
printWarning("DataType unsupported") |
| 7853 |
if doTailOpt == 0: |
| 7854 |
module.addComment0("g2l=%u, load component %u"%(g2lIdx, r)) |
| 7855 |
|
| 7856 |
offset = 0 |
| 7857 |
hi8 = 0 |
| 7858 |
hi16 = 0 |
| 7859 |
|
| 7860 |
if kernel["BufferLoad"] or isTr: |
| 7861 |
|
| 7862 |
|
| 7863 |
|
| 7864 |
|
| 7865 |
if kernel["_UseSgprForGRO"]: |
| 7866 |
offsetVgpr = "GlobalReadOffset%s+0"%(tc) |
| 7867 |
else: |
| 7868 |
offsetVgpr = "GlobalReadOffset%s+%u"%(tc, graIdx) |
| 7869 |
|
| 7870 |
|
| 7871 |
if not kernel["_UseSgprForGRO"]: |
| 7872 |
soffset = "0" |
| 7873 |
|
| 7874 |
elif isLds and kernel["UseInstOffsetForGRO"]: |
| 7875 |
soffset = sgpr("ScalarGlobalReadOffset%s+%u"%(tc, graIdx)) |
| 7876 |
|
| 7877 |
else: |
| 7878 |
soffset = "0" if graIdx == 0 else sgpr("ScalarGlobalReadOffset%s+%u"%(tc, graIdx-1)) |
| 7879 |
|
| 7880 |
unrollMirrorWithSoffset = kernel["ProblemType"]["IndicesSummation"][self.states.unrollIdx] in problemType["MirrorDims%s"%tc] and soffset != "0" |
| 7881 |
|
| 7882 |
|
| 7883 |
if unrollMirrorWithSoffset: |
| 7884 |
codeMod = Module("mirrorIdx%u"%loopCnt) |
| 7885 |
codeMod.add(VSubU32(dst=vgpr(offsetVgpr), src0=vgpr(offsetVgpr), src1=soffset, comment="mirror unroll: GRO=GRO-SGRO, soffset=0")) |
| 7886 |
module.add(codeMod) |
| 7887 |
soffset_prev = soffset |
| 7888 |
soffset = "0" |
| 7889 |
|
| 7890 |
if isLds: |
| 7891 |
|
| 7892 |
|
| 7893 |
if r == 0: |
| 7894 |
ldsInc = (self.states.kernel["WavefrontSize"] if kernel["WaveSeparateGlobalRead%c"%tc] else kernel["NumThreads"]) * kernel["GlobalReadVectorWidth%c"%tc] * tP["bpeGR"] |
| 7895 |
else: |
| 7896 |
ldsInc = 0 |
| 7897 |
if kernel["LdsBlockSizePerPad%s"%tc] != 0: |
| 7898 |
ldsInc += (ldsInc // kernel["LdsBlockSizePerPad%s"%tc]) * kernel["LdsPad%s"%tc] * tP["bpeGR"] |
| 7899 |
else: |
| 7900 |
padInterval = (self.states.kernel["WavefrontSize"] if kernel["WaveSeparateGlobalRead%c"%tc] else kernel["NumThreads"]) * self.states.bpr |
| 7901 |
ldsInc += (ldsInc // padInterval) * kernel["LdsPad%s"%tc] * tP["bpeGR"] |
| 7902 |
if kernel["UseInstOffsetForGRO"]: |
| 7903 |
|
| 7904 |
|
| 7905 |
if instOffset >= self.buff_load_inst_offset_max: |
| 7906 |
inc = (instOffset // self.buff_load_inst_offset_max) * self.buff_load_inst_offset_max |
| 7907 |
module.add(SAddU32(dst=mgpr(0), src0=mgpr(0), src1=inc, comment="Move LDS write address to next base" )) |
| 7908 |
instOffset -= inc |
| 7909 |
elif directToLdsLoads != 0 and ldsInc > 0: |
| 7910 |
if tP["nrc"] > 1: |
| 7911 |
|
| 7912 |
divisorName = tP["lvc"] |
| 7913 |
divisor = kernel[divisorName] |
| 7914 |
|
| 7915 |
wSize = max(self.states.kernel["WavefrontSize"], divisor) |
| 7916 |
lscaOffset = para * wSize * tP["bpeGR"] * tP["glvw"] |
| 7917 |
ldsOffset = ldsInc * tP["nrc"] * (sPerp + tP["nrpv"] * perp) + lscaOffset |
| 7918 |
ldsInc = ldsOffset - prevLdsOffset |
| 7919 |
prevLdsOffset = ldsOffset |
| 7920 |
|
| 7921 |
if (doTailOpt == 0) or (doTailOpt == 2 and behavior == "LOAD") or\ |
| 7922 |
(doTailOpt == 1 and i == idx and ((r+1) % numElementsPer4Bytes != 0) and r != 0): |
| 7923 |
module.add(SAddU32(dst=mgpr(0), src0=mgpr(0), src1=ldsInc, comment="Move LDS write address to next line" )) |
| 7924 |
destVgpr=0 |
| 7925 |
self.vgprs.globalReadRegisters[tc].append(0) |
| 7926 |
else: |
| 7927 |
g2lIdxM = i * max(loadWidth * tP["bpeRatio"], 1) |
| 7928 |
destVgpr="G2L%s+%u+%u"%(tc, g2lIdx + tP["shiftGR"] if not tP["isM"] else g2lIdxM, regIdx+eccOffset) |
| 7929 |
self.vgprs.globalReadRegisters[tc].append( (g2lIdx + tP["shiftGR"] if not tP["isM"] else g2lIdxM) + regIdx+eccOffset) |
| 7930 |
|
| 7931 |
offset = r * tP["bpeGR"] + instOffset |
| 7932 |
comment = "load one buffer value" |
| 7933 |
|
| 7934 |
if (dataType.isHalf() or dataType.isBFloat16()) and not tP["isM"]: |
| 7935 |
if numElementsPerLoad==2: |
| 7936 |
|
| 7937 |
r += 1 |
| 7938 |
comment = "load packed 2X half buffer value" |
| 7939 |
elif not isLds: |
| 7940 |
hi16=loopCnt%2 if tP["glvw"]==1 else r%2 |
| 7941 |
comment="load one buffer value" |
| 7942 |
|
| 7943 |
if ((dataType.isInt8() or dataType.is8bitFloat()) \ |
| 7944 |
and not tP["isM"]) or (tP["isM"] and destVgprHi != None): |
| 7945 |
|
| 7946 |
|
| 7947 |
|
| 7948 |
|
| 7949 |
|
| 7950 |
if not isLds: |
| 7951 |
hi8 = (loopCnt%4) %2 if tP["glvw"]==1 else (r%4) %2 |
| 7952 |
hi16 = False if tP["glvw"]==1 else (r%4)//2 |
| 7953 |
comment="load one buffer value" |
| 7954 |
|
| 7955 |
useBuffer = not isTr |
| 7956 |
bpl = numElementsPerLoad*(tP["bpeGR"] if not tP["isM"] else tP["bpe"]) |
| 7957 |
|
| 7958 |
|
| 7959 |
|
| 7960 |
if doTailOpt == 1: |
| 7961 |
loadVgpr = destVgprHi |
| 7962 |
else: |
| 7963 |
loadVgpr = destVgprHi if ((hi16 or hi8) and destVgprHi != None) else destVgpr |
| 7964 |
self.vgprs.globalReadRegisters[tc][-1] = destVgprHi if ((hi16 or hi8) and destVgprHi != None) else self.vgprs.globalReadRegisters[tc][-1] |
| 7965 |
if (kernel["ProblemType"]["DataType%s"%tcDataType].isInt8() or kernel["ProblemType"]["DataType%s"%tcDataType].is8bitFloat() or tP["isM"]) \ |
| 7966 |
and (not self.states.archCaps["HasEccHalf"]) and useBuffer: |
| 7967 |
module.add(VMovB32(dst=vgpr(loadVgpr), src=0, comment="set to zero to avoid unexpected value")) |
| 7968 |
|
| 7969 |
if isTr: |
| 7970 |
module.add(VMinI32(dst=vgpr(offsetVgpr), src0=vgpr(maxGroVgpr), src1=vgpr(offsetVgpr), comment="truncated load: clamp GRO to legal range")) |
| 7971 |
|
| 7972 |
if doTailOpt == 1: |
| 7973 |
if behavior == "LOAD" and i == idx: |
| 7974 |
if (numElementsPerLoad == 2 and r % numElementsPer4Bytes != 0) or \ |
| 7975 |
(numElementsPerLoad != 2 and ((r + 1) % numElementsPer4Bytes != 0)): |
| 7976 |
module.addComment0("g2l=%u, load component %u"%(g2lIdx, r)) |
| 7977 |
module.add(self.chooseGlobalRead(useBuffer, \ |
| 7978 |
bpl, destVgpr=loadVgpr, \ |
| 7979 |
addr0=vgpr(offsetVgpr), addr1=sgpr("Srd%s"%tc, 2 if isTr else 4), \ |
| 7980 |
soffset=soffset, offset=offset, \ |
| 7981 |
glc=isGlc, slc=isSlc, nt=isNT, lds=isLds, \ |
| 7982 |
tr=isTr, hi16=hi16, \ |
| 7983 |
comment=comment)) |
| 7984 |
tmpVgprIdx += 1 |
| 7985 |
|
| 7986 |
if (numElementsPerLoad == 2 and r == (numLoadVectorComp - 1)) or \ |
| 7987 |
(numElementsPerLoad != 2 and (r + 1) == (numLoadVectorComp - 1)): |
| 7988 |
module.add(SBranch(labelName=jumpLabel.getLabelName(), comment="")) |
| 7989 |
else: |
| 7990 |
if (doTailOpt == 0) or (doTailOpt == 2 and behavior == "LOAD" and r >= rStart and r < rEnd): |
| 7991 |
|
| 7992 |
module.add(self.chooseGlobalRead(useBuffer, \ |
| 7993 |
bpl, destVgpr=loadVgpr, \ |
| 7994 |
addr0=vgpr(offsetVgpr), addr1=sgpr("Srd%s"%tc, 2 if isTr else 4), \ |
| 7995 |
soffset=soffset, offset=offset, \ |
| 7996 |
glc=isGlc, slc=isSlc, nt=isNT, lds=isLds, \ |
| 7997 |
tr=isTr, hi16=hi16, \ |
| 7998 |
comment=comment)) |
| 7999 |
loadCnt = loadCnt + 1 |
| 8000 |
if unrollMirrorWithSoffset: |
| 8001 |
codeMod = Module("mirrorIdx%u"%loopCnt) |
| 8002 |
codeMod.add(VAddU32(dst=vgpr(offsetVgpr), src0=vgpr(offsetVgpr), src1=soffset_prev, comment="mirror unroll: restore GRO=GRO+SGRO")) |
| 8003 |
module.add(codeMod) |
| 8004 |
|
| 8005 |
if isLds and kernel["UseInstOffsetForGRO"]: |
| 8006 |
instOffsetInc += ldsInc |
| 8007 |
|
| 8008 |
else: |
| 8009 |
|
| 8010 |
module.add(VCmpXLtU64(dst=VCC(), \ |
| 8011 |
src0=vgpr("GlobalReadAddr%s+%u"%(tP["tensorChar"], graIdx),2), \ |
| 8012 |
src1=vgpr(maxAddrVgpr,2), \ |
| 8013 |
comment="addr < maxAddr")) |
| 8014 |
hi16=(kernel["ProblemType"]["DataType%s"%tcDataType].isHalf() or kernel["ProblemType"]["DataType%s"%tcDataType].isBFloat16()) and r%2==1 |
| 8015 |
destVgpr="G2L%s+%u+%u"%(tc, g2lIdx, regIdx) |
| 8016 |
|
| 8017 |
module.add(self.chooseGlobalRead(False, \ |
| 8018 |
tP["bpeGR"], destVgpr=destVgprHi if (hi16 and destVgprHi != None) else destVgpr, \ |
| 8019 |
addr0=vgpr("GlobalReadAddr%s+%u"%(tc,graIdx),2), addr1="", \ |
| 8020 |
soffset=0, offset=0, \ |
| 8021 |
glc=isGlc, slc=isSlc, nt=isNT, lds=isLds, \ |
| 8022 |
hi16=hi16, \ |
| 8023 |
comment="load one flat value")) |
| 8024 |
|
| 8025 |
|
| 8026 |
SOrSaveExecBX = SOrSaveExecB32 if self.states.kernel["WavefrontSize"] == 32 else SOrSaveExecB64 |
| 8027 |
module.add(SOrSaveExecBX(dst=VCC(), src=sgpr(fullExec,self.states.laneSGPRCount), comment="all threads active")) |
| 8028 |
|
| 8029 |
|
| 8030 |
module.add(VAddCOU32( |
| 8031 |
dst=vgpr("GlobalReadAddr%s+%u+0"%(tP["tensorChar"], graIdx)), \ |
| 8032 |
dst1=VCC(), \ |
| 8033 |
src0=vgpr("GlobalReadAddr%s+%u+0"%(tP["tensorChar"], graIdx)), \ |
| 8034 |
src1=vgpr(bpeVgpr), comment="gra += 1 (lower)")) |
| 8035 |
module.add(VAddCCOU32( |
| 8036 |
dst=vgpr("GlobalReadAddr%s+%u+1"%(tP["tensorChar"], graIdx)), \ |
| 8037 |
dst1=VCC(), \ |
| 8038 |
src0=vgpr("GlobalReadAddr%s+%u+1"%(tP["tensorChar"], graIdx)), \ |
| 8039 |
src1=vgpr(zeroVgpr), \ |
| 8040 |
src2=VCC(), comment="gra += 1 (upper)")) |
| 8041 |
|
| 8042 |
|
| 8043 |
|
| 8044 |
|
| 8045 |
|
| 8046 |
|
| 8047 |
|
| 8048 |
|
| 8049 |
|
| 8050 |
if doTailOpt == 2 and r >= rStart and r < rEnd: |
| 8051 |
loadNum = loadNum - 1 |
| 8052 |
if dataIsByte and (destVgprHi != None): |
| 8053 |
if doTailOpt == 1: |
| 8054 |
if behavior == "MERGE" and i == idx and ((r + 1) % numElementsPer4Bytes != 0): |
| 8055 |
|
| 8056 |
|
| 8057 |
module.add(SWaitCnt(vlcnt=0, comment="")) |
| 8058 |
if hi8: |
| 8059 |
module.add(VLShiftLeftB32(dst=vgpr(destVgprHi), shiftHex=hex(0x8), src=vgpr(destVgprHi), comment="shift left to higher 8 bits")) |
| 8060 |
module.add(VOrB32(dst=vgpr(destVgpr), src0=vgpr(destVgpr), src1=vgpr(destVgprHi), comment="pack a sub 8-bit with dest")) |
| 8061 |
tmpVgprIdx += 1 |
| 8062 |
if (r + 1) == (numLoadVectorComp - 1): |
| 8063 |
module.add(SBranch(labelName=jumpLabel.getLabelName(), comment="")) |
| 8064 |
else: |
| 8065 |
if (doTailOpt == 0) or (doTailOpt == 2 and behavior == "MERGE" and r >= rStart and r < rEnd): |
| 8066 |
|
| 8067 |
|
| 8068 |
if hi8 or hi16: |
| 8069 |
|
| 8070 |
if doTailOpt == 0: |
| 8071 |
packInt8Code.add(SWaitCnt(vlcnt=(int8TempVgpr-r), comment="")) |
| 8072 |
else: |
| 8073 |
packInt8Code.add(SWaitCnt(vlcnt=(loadNum), comment="")) |
| 8074 |
if hi8: |
| 8075 |
|
| 8076 |
packInt8Code.add(VLShiftLeftB32(dst=vgpr(destVgprHi), shiftHex=hex(0x8), src=vgpr(destVgprHi), comment="shift left to higher 8 bits")) |
| 8077 |
if hi8 or hi16: |
| 8078 |
|
| 8079 |
packInt8Code.add(VOrB32(dst=vgpr(destVgpr), src0=vgpr(destVgpr), src1=vgpr(destVgprHi), comment="pack a sub 8-bit with dest")) |
| 8080 |
destVgprHi += 1 |
| 8081 |
|
| 8082 |
|
| 8083 |
elif destVgprHi != None: |
| 8084 |
if doTailOpt == 1: |
| 8085 |
if behavior == "MERGE" and i == idx: |
| 8086 |
if (numElementsPerLoad == 2 and r % numElementsPer4Bytes != 0) or \ |
| 8087 |
(numElementsPerLoad != 2 and ((r + 1) % numElementsPer4Bytes != 0)): |
| 8088 |
module.add(SWaitCnt(vlcnt=0, comment="")) |
| 8089 |
|
| 8090 |
if kernel["ProblemType"]["DataType%s"%tcDataType].is8bitFloat(): |
| 8091 |
module.add(VLShiftRightB32(dst=vgpr(destVgprHi), shiftHex=hex(8), src=vgpr(destVgprHi), comment="shift right to lower 8 bits")) |
| 8092 |
module.add(VOrB32(dst=vgpr(destVgpr), src0=vgpr(destVgpr), src1=vgpr(destVgprHi), comment="HasEccHalf: pack")) |
| 8093 |
if kernel["ProblemType"]["DataType%s"%tcDataType].is8bitFloat() and (g2lIdx % 2 == 1): |
| 8094 |
module.add(VLShiftLeftB32(dst=vgpr(destVgpr), shiftHex=hex(16), src=vgpr(destVgpr), comment="shift left to higher 16 bits")) |
| 8095 |
tmpVgprIdx += 1 |
| 8096 |
if (numElementsPerLoad == 2 and r == (numLoadVectorComp - 1)) or\ |
| 8097 |
(numElementsPerLoad != 2 and (r + 1) == (numLoadVectorComp - 1)): |
| 8098 |
module.add(SBranch(labelName=jumpLabel.getLabelName(), comment="")) |
| 8099 |
else: |
| 8100 |
if r % 2 == 1: |
| 8101 |
|
| 8102 |
if kernel["ProblemType"]["DataType%s"%tcDataType].is8bitFloat(): |
| 8103 |
packInst.add(VLShiftRightB32(dst=vgpr(destVgprHi), shiftHex=hex(8), src=vgpr(destVgprHi), comment="shift right to lower 8 bits")) |
| 8104 |
packInst.add(VOrB32(dst=vgpr(destVgpr), src0=vgpr(destVgpr), src1=vgpr(destVgprHi), comment="HasEccHalf: pack")) |
| 8105 |
if kernel["ProblemType"]["DataType%s"%tcDataType].is8bitFloat() and (g2lIdx % 2 == 1): |
| 8106 |
packInst.add(VLShiftLeftB32(dst=vgpr(destVgpr), shiftHex=hex(16), src=vgpr(destVgpr), comment="shift left to higher 16 bits")) |
| 8107 |
vlcntVal = 0 if doTailOpt == 0 else loadNum |
| 8108 |
if (doTailOpt == 0 and len(tmpVgprPool) == maxNumTempToUse) or (doTailOpt == 2 and behavior == "MERGE" and r >= rStart and r < rEnd): |
| 8109 |
module.add(SWaitCnt(vlcnt=(vlcntVal), comment="")) |
| 8110 |
module.add(packInst) |
| 8111 |
packInst = Module() |
| 8112 |
else: |
| 8113 |
if doTailOpt == 1 and i == idx and behavior == "MERGE" and\ |
| 8114 |
((numElementsPerLoad == 2 and r == (numLoadVectorComp - 1)) or\ |
| 8115 |
(numElementsPerLoad != 2 and (r + 1) == (numLoadVectorComp - 1))): |
| 8116 |
module.add(SBranch(labelName=jumpLabel.getLabelName(), comment="")) |
| 8117 |
|
| 8118 |
if (destVgprHi != None) and (not dataIsByte): |
| 8119 |
if len(tmpVgprPool) == maxNumTempToUse and doTailOpt == 0: |
| 8120 |
for v in tmpVgprPool: |
| 8121 |
self.vgprPool.checkIn(v) |
| 8122 |
tmpVgprPool = [] |
| 8123 |
destVgprHi = None |
| 8124 |
|
| 8125 |
r += 1 |
| 8126 |
|
| 8127 |
|
| 8128 |
|
| 8129 |
instOffset += instOffsetInc |
| 8130 |
|
| 8131 |
directToLdsLoads+=1 |
| 8132 |
|
| 8133 |
|
| 8134 |
|
| 8135 |
if dataIsByte and int8TempVgpr and not isLds: |
| 8136 |
assert packInt8Code != None and destVgprHi != None |
| 8137 |
module.add(packInt8Code) |
| 8138 |
if doTailOpt == 0: |
| 8139 |
self.vgprPool.checkIn(destVgprHi - int8TempVgpr) |
| 8140 |
destVgprHi = None |
| 8141 |
|
| 8142 |
|
| 8143 |
if is16b and doTailOpt == 0 and tmpVgprPool: |
| 8144 |
module.add(SWaitCnt(vlcnt=0, comment="Wait for previous GR to finish")) |
| 8145 |
module.add(packInst) |
| 8146 |
for v in tmpVgprPool: |
| 8147 |
self.vgprPool.checkIn(v) |
| 8148 |
tmpVgprPool = [] |
| 8149 |
|
| 8150 |
if kernel["ProblemType"]["Sparse"]: |
| 8151 |
if kernel["DirectToVgprSparseMetadata"]: |
| 8152 |
miWaveTile = kernel["MIWaveTileB"] if (tP["is_sparse"] and tP["isB"]) else kernel["MIWaveTileA"] if (tP["is_sparse"] and tP["isA"]) else 0 |
| 8153 |
for wtIdx in range(0, miWaveTile): |
| 8154 |
offsetVgpr= "GlobalReadOffsetMetadata+%u"%wtIdx |
| 8155 |
for unrollIdx in range(0, kernel["LoopIters"]): |
| 8156 |
bpl = kernel["MIInputPerThread"]//8 |
| 8157 |
constOffset = unrollIdx * kernel["MatrixInstK"] // 8 |
| 8158 |
for byteIdx in range(0, bpl): |
| 8159 |
constOffset += byteIdx |
| 8160 |
if byteIdx == 0: |
| 8161 |
|
| 8162 |
if kernel["PrefetchGlobalRead"] == 2: |
| 8163 |
offsetBlk = self.states.m.numVgprValuPerBlock * 2 |
| 8164 |
else: |
| 8165 |
offsetBlk = 0 |
| 8166 |
destVgprLow="ValuMetadata+%u+%u"%(offsetBlk, (wtIdx*kernel["LoopIters"]+unrollIdx)) |
| 8167 |
destVgpr=destVgprLow |
| 8168 |
else: |
| 8169 |
destVgprHi = self.vgprPool.checkOut(1, 'destVgprHi') |
| 8170 |
destVgpr=destVgprHi |
| 8171 |
module.add(self.chooseGlobalRead(kernel["BufferLoad"], \ |
| 8172 |
1, \ |
| 8173 |
destVgpr=destVgpr, \ |
| 8174 |
addr0=vgpr(offsetVgpr), addr1=sgpr("SrdMetadata",4), \ |
| 8175 |
soffset=0, offset=constOffset, \ |
| 8176 |
glc=isGlc, slc=isSlc, nt=isNT, lds=isLds, \ |
| 8177 |
hi16=0, \ |
| 8178 |
comment="G -> Reg ValuMetadata")) |
| 8179 |
if bpl == 2: |
| 8180 |
module.add(SWaitCnt(vlcnt=0)) |
| 8181 |
module.add(VLShiftLeftB32(dst=vgpr(destVgprHi), shiftHex="0x8", src=vgpr(destVgprHi), comment="shift left to higher 8 bits")) |
| 8182 |
module.add(VOrB32(dst=vgpr(destVgprLow), src0=vgpr(destVgprLow), src1=vgpr(destVgprHi), comment="pack 2 bytes")) |
| 8183 |
self.vgprPool.checkIn(destVgprHi) |
| 8184 |
destVgprHi = None |
| 8185 |
|
| 8186 |
return loadCnt, self.vgprs.globalReadRegisters[tc], directToLdsLoads |
| 8187 |
|
| 8188 |
loadCnt, vgprList, directToLdsLoads = globalReadGuardKBody(tP, optParams) |
| 8189 |
|
| 8190 |
if doTailOpt == 0 or \ |
| 8191 |
(doTailOpt == 2 and optParams.finalLoop == 1 and optParams.behavior == "MERGE"): |
| 8192 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 8193 |
if tP["is_sparse"]: |
| 8194 |
globalReadGuardKBody(tP["tpsMetadata"]) |
| 8195 |
|
| 8196 |
if self.db["ConservativeWaitCnt"] & 0x1: |
| 8197 |
module.add(SBarrier(comment="debug")) |
| 8198 |
module.add(SWaitCnt(dscnt=0, vlcnt=0, vscnt=0, comment="")) |
| 8199 |
module.add(SBarrier(comment="debug")) |
| 8200 |
|
| 8201 |
|
| 8202 |
if kernel["DirectToLds%s"%tP["tensorChar"]]: |
| 8203 |
module.add(SMovB32(dst=mgpr(0), src=hex(kernel["LdsNumBytes"]), \ |
| 8204 |
comment="Restore LDS clamp at %u bytes HERE"%(kernel["LdsNumBytes"]))) |
| 8205 |
|
| 8206 |
if isTr: |
| 8207 |
self.vgprPool.checkIn(maxGroVgpr) |
| 8208 |
elif not kernel["BufferLoad"]: |
| 8209 |
self.vgprPool.checkIn(maxAddrVgpr) |
| 8210 |
self.vgprPool.checkIn(bpeVgpr) |
| 8211 |
self.vgprPool.checkIn(zeroVgpr) |
| 8212 |
|
| 8213 |
if doTailOpt == 2: |
| 8214 |
return module, loadCnt, vgprList, directToLdsLoads |
| 8215 |
else: |
| 8216 |
return module |
| 8217 |
|
| 8218 |
|
| 8219 |
|
| 8220 |
|
| 8221 |
|
| 8222 |
def doTailLoopOpt(self, kernel, tP): |
| 8223 |
module = Module("doTailLoop") |
| 8224 |
tc = tP["tensorChar"] |
| 8225 |
orinrp = tP["nrp"] |
| 8226 |
orinrpv = tP["nrpv"] |
| 8227 |
orinrc = tP["nrc"] |
| 8228 |
orinrcv_div_nrcvpi = tP["nrcv"] // tP["nrcvpi"] |
| 8229 |
loadWidth = tP["globalReadInstruction"].totalWidth |
| 8230 |
oriNumLoadVectorComp = (int(loadWidth*self.states.bpr//tP["bpeGR"])) |
| 8231 |
numElementsPerLoad = 1 |
| 8232 |
glvwWorkaround = 8 * kernel["ProblemType"]["DataType"].numRegisters() |
| 8233 |
dataType = kernel["ProblemType"]["DataType"] if tP["glvw"] < glvwWorkaround \ |
| 8234 |
else kernel["ProblemType"]["DataType%s"%tc] |
| 8235 |
|
| 8236 |
if dataType.isHalf() or dataType.isBFloat16(): |
| 8237 |
if tP["glvw"] > 1 and kernel["AssertSummationElementMultiple"] % 2 == 0: |
| 8238 |
|
| 8239 |
numElementsPerLoad = 2 |
| 8240 |
oriNumLoadVectorComp = oriNumLoadVectorComp // 2 |
| 8241 |
totalVgprNum = (tP["nrp"] * tP["nrpv"] * tP["nrc"] * (tP["nrcv"] // tP["nrcvpi"])) * (oriNumLoadVectorComp) |
| 8242 |
if kernel["ProblemType"]["Gradient"] and kernel["ProblemType"]["UseBias"] \ |
| 8243 |
and (kernel["ProblemType"]["BiasSrc"] == "A" or kernel["ProblemType"]["BiasSrc"] == "B"): |
| 8244 |
totalVgprNum += 1 |
| 8245 |
|
| 8246 |
numLoadVectorComp = oriNumLoadVectorComp |
| 8247 |
nrp = orinrp |
| 8248 |
nrpv = orinrpv |
| 8249 |
nrc = orinrc |
| 8250 |
nrcv_div_nrcvpi = orinrcv_div_nrcvpi |
| 8251 |
currentSize = self.vgprPool.size() |
| 8252 |
VgprSlot = [] |
| 8253 |
VgprSlotBk = [] |
| 8254 |
|
| 8255 |
maxVgpr = 50 |
| 8256 |
while (len(VgprSlot) < totalVgprNum and len(VgprSlot) <= maxVgpr): |
| 8257 |
tempVgpr = self.vgprPool.checkOut(1,"") |
| 8258 |
if tempVgpr >= currentSize: |
| 8259 |
self.vgprPool.checkIn(tempVgpr) |
| 8260 |
break |
| 8261 |
if kernel["ProblemType"]["Gradient"] and kernel["ProblemType"]["UseBias"] \ |
| 8262 |
and (kernel["ProblemType"]["BiasSrc"] == "A" or kernel["ProblemType"]["BiasSrc"] == "B"): |
| 8263 |
if tempVgpr != self.states.bias.startVgprValu: |
| 8264 |
VgprSlot.append(tempVgpr) |
| 8265 |
else: |
| 8266 |
VgprSlot.append(tempVgpr) |
| 8267 |
VgprSlotBk.append(tempVgpr) |
| 8268 |
|
| 8269 |
loopNum = 1 |
| 8270 |
finalVgprNum = totalVgprNum |
| 8271 |
while (finalVgprNum > len(VgprSlot) and totalVgprNum > 0): |
| 8272 |
if nrp > 1: |
| 8273 |
nrp -= 1 |
| 8274 |
elif nrpv > 1: |
| 8275 |
nrpv -= 1 |
| 8276 |
elif nrc > 1: |
| 8277 |
nrc -= 1 |
| 8278 |
elif nrcv_div_nrcvpi > 1: |
| 8279 |
nrcv_div_nrcvpi -= 1 |
| 8280 |
else: |
| 8281 |
numLoadVectorComp = numLoadVectorComp - 1 |
| 8282 |
finalVgprNum = (nrp * nrpv * nrc * nrcv_div_nrcvpi) * (numLoadVectorComp) |
| 8283 |
|
| 8284 |
nrpLoopNum = ceil(tP["nrp"] / nrp) |
| 8285 |
nrpvLoopNum = ceil(tP["nrpv"] / nrpv) |
| 8286 |
nrcLoopNum = ceil(tP["nrc"] / nrc) |
| 8287 |
nrcv_div_nrcvpiLoopNum = ceil(tP["nrcv"] // tP["nrcvpi"] / nrcv_div_nrcvpi) |
| 8288 |
loadVectorCompLoopNum = int(ceil(oriNumLoadVectorComp / numLoadVectorComp)) |
| 8289 |
totalLoopNum = nrpLoopNum * nrpvLoopNum * nrcLoopNum * nrcv_div_nrcvpiLoopNum * loadVectorCompLoopNum |
| 8290 |
|
| 8291 |
i = 0 |
| 8292 |
globalReadRegisters = [] |
| 8293 |
directToLdsLoads = 0 |
| 8294 |
finalLoop = 0 |
| 8295 |
|
| 8296 |
self.optParamsLoad = TailOptParams() |
| 8297 |
self.optParamsMerge = TailOptParams() |
| 8298 |
for nrp_idx in range(nrpLoopNum): |
| 8299 |
for nrpv_idx in range(nrpvLoopNum): |
| 8300 |
for nrc_idx in range(nrcLoopNum): |
| 8301 |
for nrcv_div_nrcvpi_idx in range(nrcv_div_nrcvpiLoopNum): |
| 8302 |
for loadVectorComp_idx in range(loadVectorCompLoopNum): |
| 8303 |
firstLoop = 0 |
| 8304 |
if numElementsPerLoad == 2: |
| 8305 |
periodParam = [nrp_idx * nrp, min(orinrp, (nrp_idx + 1) * nrp), \ |
| 8306 |
nrpv_idx * nrpv, min(orinrpv, (nrpv_idx + 1) * nrpv), \ |
| 8307 |
nrc_idx * nrc, min(orinrc, (nrc_idx + 1) * nrc), \ |
| 8308 |
nrcv_div_nrcvpi_idx * nrcv_div_nrcvpi, \ |
| 8309 |
min(orinrcv_div_nrcvpi, (nrcv_div_nrcvpi_idx + 1) * nrcv_div_nrcvpi), \ |
| 8310 |
loadVectorComp_idx * numLoadVectorComp * 2, \ |
| 8311 |
min(oriNumLoadVectorComp * 2, (loadVectorComp_idx + 1) * numLoadVectorComp * 2)] |
| 8312 |
else: |
| 8313 |
periodParam = [nrp_idx * nrp, min(orinrp, (nrp_idx + 1) * nrp), \ |
| 8314 |
nrpv_idx * nrpv, min(orinrpv, (nrpv_idx + 1) * nrpv), \ |
| 8315 |
nrc_idx * nrc, min(orinrc, (nrc_idx + 1) * nrc), \ |
| 8316 |
nrcv_div_nrcvpi_idx * nrcv_div_nrcvpi, \ |
| 8317 |
min(orinrcv_div_nrcvpi, (nrcv_div_nrcvpi_idx + 1) * nrcv_div_nrcvpi), \ |
| 8318 |
loadVectorComp_idx * numLoadVectorComp, \ |
| 8319 |
min(oriNumLoadVectorComp, (loadVectorComp_idx + 1) * numLoadVectorComp)] |
| 8320 |
if i == 0: |
| 8321 |
firstLoop = 1 |
| 8322 |
|
| 8323 |
self.optParamsLoad.behavior = "LOAD" |
| 8324 |
self.optParamsLoad.vgprSlot = VgprSlot |
| 8325 |
self.optParamsLoad.periodParam = periodParam |
| 8326 |
self.optParamsLoad.directToLdsLoads = directToLdsLoads |
| 8327 |
self.optParamsLoad.firstLoop = firstLoop |
| 8328 |
self.optParamsLoad.finalLoop = finalLoop |
| 8329 |
imod, loadCnt, vgprList, directToLdsLoads = self.globalReadDo(kernel, 2, tP, -1, 0, 2, self.optParamsLoad) |
| 8330 |
module.add(imod) |
| 8331 |
|
| 8332 |
globalReadRegisters = globalReadRegisters + vgprList |
| 8333 |
|
| 8334 |
if i == totalLoopNum - 1: |
| 8335 |
finalLoop = 1 |
| 8336 |
|
| 8337 |
self.optParamsMerge.behavior = "MERGE" |
| 8338 |
self.optParamsMerge.vgprSlot = VgprSlot |
| 8339 |
self.optParamsMerge.periodParam = periodParam |
| 8340 |
self.optParamsMerge.loadNum = loadCnt |
| 8341 |
self.optParamsMerge.directToLdsLoads = directToLdsLoads |
| 8342 |
self.optParamsMerge.firstLoop = firstLoop |
| 8343 |
self.optParamsMerge.finalLoop = finalLoop |
| 8344 |
imod, loadCnt, vgprList, directToLdsLoads = self.globalReadDo(kernel, 2, tP, -1, 0, 2, self.optParamsMerge) |
| 8345 |
module.add(imod) |
| 8346 |
|
| 8347 |
i += 1 |
| 8348 |
|
| 8349 |
self.vgprs.globalReadRegisters[tc] = globalReadRegisters |
| 8350 |
while VgprSlotBk: |
| 8351 |
tempVgpr = VgprSlotBk.pop(0) |
| 8352 |
self.vgprPool.checkIn(tempVgpr) |
| 8353 |
|
| 8354 |
return module |
| 8355 |
|
| 8356 |
|
| 8357 |
|
| 8358 |
|
| 8359 |
def directToLdsM0Update(self, kernel, mode, tP, skipWait = False): |
| 8360 |
tc = tP["tensorChar"] |
| 8361 |
imod = Module("directToLdsM0Update%s_%u"%(tc,mode)) |
| 8362 |
DtldsModule = imod.add(Module("dtls_offset%s"%tP["tensorChar"])) |
| 8363 |
if not self.do["GlobalRead%s"%tP["tensorChar"]]: return imod |
| 8364 |
if kernel["DirectToLds%s"%tc]: |
| 8365 |
|
| 8366 |
|
| 8367 |
|
| 8368 |
assert (kernel["LocalWriteUseSgpr%s"%tc]) |
| 8369 |
if kernel["ExpandPointerSwap"]: |
| 8370 |
DtldsModule.add(SAddU32(dst=mgpr(0), src0=sgpr("LocalWriteAddr%s"%tc), \ |
| 8371 |
src1=tP["localWriteSwapByteOffset"], comment="m0 <- LDS write address")) |
| 8372 |
else: |
| 8373 |
DtldsModule.add(SMovB32(dst=mgpr(0), src=sgpr("LocalWriteAddr%s"%tc), comment="m0 <- LDS write address")) |
| 8374 |
|
| 8375 |
|
| 8376 |
if kernel["PrefetchGlobalRead"]==2 and not skipWait: |
| 8377 |
|
| 8378 |
DtldsModule.addComment0("before DirectToLds load, ensure prior ds_reads have finished") |
| 8379 |
DtldsModule.add(SWaitCnt(dscnt=0, comment="")) |
| 8380 |
if not kernel["NoLdsWriteCode"]: |
| 8381 |
waitStr = 0 |
| 8382 |
DtldsModule.add(SWaitCnt(vlcnt=waitStr, comment="")) |
| 8383 |
DtldsModule.add(SBarrier()) |
| 8384 |
|
| 8385 |
return imod |
| 8386 |
|
| 8387 |
|
| 8388 |
|
| 8389 |
|
| 8390 |
def globalReadDo(self, kernel, mode, tP, unrollLoopIdx=-1, g2lBufIdx=0, \ |
| 8391 |
doTailOpt = 0, optParams = None): |
| 8392 |
tc = tP["tensorChar"] |
| 8393 |
problemType = self.states.kernel["ProblemType"] |
| 8394 |
imod = StructuredModule("globalReadDo%s_%u"%(tc,mode)) |
| 8395 |
if not self.do["GlobalRead%s"%tP["tensorChar"]]: |
| 8396 |
return imod |
| 8397 |
|
| 8398 |
|
| 8399 |
guardK = (mode==2) |
| 8400 |
|
| 8401 |
loopIdx = self.states.unrollIdx |
| 8402 |
if kernel["SuppressNoLoadLoop"]: |
| 8403 |
if mode==1 and tP["isA"]: |
| 8404 |
imod.header.add(SCmpEQI32( |
| 8405 |
src0=self.loopCounter(kernel, loopIdx), \ |
| 8406 |
src1=1, \ |
| 8407 |
comment="%s"%"is this the last iteration")) |
| 8408 |
imod.header.add(SCMovB32( |
| 8409 |
dst=sgpr("SrdA+2"), src=0, |
| 8410 |
comment="Set limit to 0 for last iteration")) |
| 8411 |
imod.header.add(SCMovB32( |
| 8412 |
dst=sgpr("SrdB+2"), src=0, |
| 8413 |
comment="Set limit to 0 for last iteration")) |
| 8414 |
|
| 8415 |
|
| 8416 |
tc1st = 'A' |
| 8417 |
|
| 8418 |
if self.isSwapGlobalReadOrderForDtvOrDtl(kernel, prefetch1=(mode==0)): |
| 8419 |
tc1st = 'B' |
| 8420 |
if tc == tc1st and (kernel["DirectToLdsA"] or kernel["DirectToLdsB"]) \ |
| 8421 |
and not kernel["PrefetchGlobalRead"]==2: |
| 8422 |
if doTailOpt == 0 or \ |
| 8423 |
(doTailOpt == 2 and optParams.behavior == "LOAD" and optParams.firstLoop == 1): |
| 8424 |
|
| 8425 |
|
| 8426 |
imod.header.addComment0("before DirectToLds load, ensure prior ds_reads have finished") |
| 8427 |
if (kernel["DirectToVgprA"] or kernel["DirectToVgprB"]) and not guardK and mode != 3: |
| 8428 |
|
| 8429 |
imod.header.add(SWaitCnt(dscnt=0, comment="wait for LDS read/write")) |
| 8430 |
else: |
| 8431 |
imod.header.add(self._syncThreads(kernel)) |
| 8432 |
|
| 8433 |
if guardK: |
| 8434 |
if doTailOpt == 0: |
| 8435 |
imod.middle.add(self.globalReadGuardK(kernel, tP)) |
| 8436 |
return imod |
| 8437 |
else: |
| 8438 |
module, loadCnt, vgprList, directToLdsLoads = \ |
| 8439 |
self.globalReadGuardK(kernel, tP, doTailOpt, optParams) |
| 8440 |
|
| 8441 |
imod.middle.add(module) |
| 8442 |
return imod, loadCnt, vgprList, directToLdsLoads |
| 8443 |
|
| 8444 |
|
| 8445 |
record = [False, False] |
| 8446 |
record[0] = True if tc == "A" else False |
| 8447 |
record[1] = True if tc == "B" else False |
| 8448 |
def globalReadBody(tP): |
| 8449 |
tc = tP["tensorChar"] |
| 8450 |
self.vgprs.globalReadRegisters[tc] = [] |
| 8451 |
graIdx = 0 |
| 8452 |
g2lIdx = 0 |
| 8453 |
loadWidth = tP["globalReadInstruction"].totalWidth |
| 8454 |
bpe = tP["bpeGR"] if not tP["isM"] else tP["bpe"] |
| 8455 |
bpl = bpe * tP["glvw"] |
| 8456 |
isGlc = bool(tP["NonTemporal"] & 0x1) |
| 8457 |
isSlc = bool(tP["NonTemporal"] & 0x2) |
| 8458 |
isNT = bool(tP["NonTemporal"] & 0x4) |
| 8459 |
isLds = True if kernel["DirectToLds%s"%tc] else False |
| 8460 |
isTr = (tc == "A" or tc == "B") and kernel["enableGLTr%s"%tc] |
| 8461 |
|
| 8462 |
directToLdsLoads = 0 |
| 8463 |
instOffset = 0 |
| 8464 |
prevLdsOffset = 0 |
| 8465 |
|
| 8466 |
if g2lBufIdx >= 1: |
| 8467 |
|
| 8468 |
destVgprPrefix = "G2L%s%u"%(tc, g2lBufIdx + 1) |
| 8469 |
else: |
| 8470 |
destVgprPrefix = "G2L%s"%(tc) |
| 8471 |
|
| 8472 |
loopCnt = -1 |
| 8473 |
for perp in range(0, tP["nrp"]): |
| 8474 |
for sPerp in range(0, tP["nrpv"]): |
| 8475 |
for para in range(0, tP["nrc"]): |
| 8476 |
for sPara in range(0, tP["nrcv"]//tP["nrcvpi"]): |
| 8477 |
if tc != "Metadata" and kernel["DirectToVgpr%s"%tc] and kernel["reorderGRInstForDTV%s"%tc]: |
| 8478 |
i = perp + tP["nrp"] * (sPerp + tP["nrpv"] * (para + sPara * tP["nrc"])) |
| 8479 |
else: |
| 8480 |
i = sPara + (tP["nrcv"]//tP["nrcvpi"]) * (para + tP["nrc"] * (sPerp + tP["nrpv"] * perp)) |
| 8481 |
loopCnt += 1 |
| 8482 |
graIdx = i * self.states.rpgo if kernel["BufferLoad"] else i * self.states.rpga |
| 8483 |
g2lIdx = i * loadWidth * tP["bpeRatio"] |
| 8484 |
if (tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc] and kernel["ConvertAfterDS"]: |
| 8485 |
|
| 8486 |
if tP["bpe"] > tP["bpeGR"]: |
| 8487 |
g2lIdx *= tP["bpe"] // tP["bpeGR"] |
| 8488 |
|
| 8489 |
loadModule = Module("load%u"%loopCnt) |
| 8490 |
imod.middle.add(loadModule) |
| 8491 |
|
| 8492 |
if (self.states.archCaps["HasEccHalf"] or not self.states.asmCaps["HasWMMA_V1"]) and not tP["isM"]: |
| 8493 |
numVgprG2L = self.states.a.numVgprG2L if tc == 'A' else self.states.b.numVgprG2L if tc =='B' else self.states.m.numVgprG2L |
| 8494 |
eccBpe = tP["bpeDS"] if kernel["ConvertAfterDS"] else max(tP["bpeGR"], tP["bpe"]) |
| 8495 |
eccOffset = _getEccOffset(loadWidth, bpr=self.states.bpr, bpe=eccBpe, \ |
| 8496 |
glvw=tP["glvw"], idx=i, numVgprG2L=numVgprG2L) |
| 8497 |
else: |
| 8498 |
eccOffset = 0 |
| 8499 |
|
| 8500 |
if kernel["BufferLoad"]: |
| 8501 |
if kernel["_UseSgprForGRO"]: |
| 8502 |
offsetVgpr= "GlobalReadOffset%s+0"%(tc) |
| 8503 |
else: |
| 8504 |
offsetVgpr= "GlobalReadOffset%s+%u"%(tc, graIdx) |
| 8505 |
|
| 8506 |
|
| 8507 |
if not kernel["_UseSgprForGRO"]: |
| 8508 |
soffset = "0" |
| 8509 |
|
| 8510 |
elif kernel["DirectToLds%s"%tc] and kernel["UseInstOffsetForGRO"]: |
| 8511 |
soffset = sgpr("ScalarGlobalReadOffset%s+%u"%(tc, graIdx)) |
| 8512 |
|
| 8513 |
else: |
| 8514 |
soffset = "0" if graIdx == 0 else sgpr("ScalarGlobalReadOffset%s+%u"%(tc, graIdx-1)) |
| 8515 |
unrollMirrorWithSoffset = kernel["ProblemType"]["IndicesSummation"][self.states.unrollIdx] in problemType["MirrorDims%s"%tc] and soffset != "0" |
| 8516 |
|
| 8517 |
|
| 8518 |
if unrollMirrorWithSoffset: |
| 8519 |
codeMod = Module("mirrorIdx%u"%loopCnt) |
| 8520 |
codeMod.add(VSubU32(dst=vgpr(offsetVgpr), src0=vgpr(offsetVgpr), src1=soffset, comment="mirror unroll: GRO=GRO-SGRO, soffset=0")) |
| 8521 |
loadModule.add(codeMod) |
| 8522 |
soffset_prev = soffset |
| 8523 |
soffset = "0" |
| 8524 |
|
| 8525 |
if kernel["DirectToLds%s"%tc]: |
| 8526 |
|
| 8527 |
ldsInc = (self.states.kernel["WavefrontSize"] * kernel["GlobalReadVectorWidth%c"%tc] if kernel["WaveSeparateGlobalRead%c"%tc] else kernel["NumThreads"] * kernel["GlobalReadVectorWidth%c"%tc]) * tP["bpeGR"] |
| 8528 |
if kernel["LdsBlockSizePerPad%s"%tc] != 0: |
| 8529 |
ldsInc += (ldsInc // kernel["LdsBlockSizePerPad%s"%tc]) * kernel["LdsPad%s"%tc] * tP["bpeGR"] |
| 8530 |
else: |
| 8531 |
padInterval = (self.states.kernel["WavefrontSize"] if kernel["WaveSeparateGlobalRead%c"%tc] else kernel["NumThreads"]) * self.states.bpr |
| 8532 |
ldsInc += (ldsInc // padInterval) * kernel["LdsPad%s"%tc] * tP["bpeGR"] |
| 8533 |
|
| 8534 |
if kernel["UseInstOffsetForGRO"]: |
| 8535 |
|
| 8536 |
|
| 8537 |
if instOffset >= self.buff_load_inst_offset_max: |
| 8538 |
inc = (instOffset // self.buff_load_inst_offset_max) * self.buff_load_inst_offset_max |
| 8539 |
loadModule.add(SAddU32(dst=mgpr(0), src0=mgpr(0), src1=inc, comment="Move LDS write address to next base" )) |
| 8540 |
instOffset -= inc |
| 8541 |
elif directToLdsLoads != 0: |
| 8542 |
|
| 8543 |
|
| 8544 |
if tP["nrc"] > 1: |
| 8545 |
|
| 8546 |
divisorName = tP["lvc"] |
| 8547 |
divisor = kernel[divisorName] |
| 8548 |
|
| 8549 |
wSize = max(self.states.kernel["WavefrontSize"], divisor) |
| 8550 |
lscaOffset = para * wSize * tP["bpeGR"] * tP["glvw"] |
| 8551 |
ldsOffset = ldsInc * tP["nrc"] * (sPerp + tP["nrpv"] * perp) + lscaOffset |
| 8552 |
ldsInc = ldsOffset - prevLdsOffset |
| 8553 |
prevLdsOffset = ldsOffset |
| 8554 |
loadModule.add(SAddU32(dst=mgpr(0), src0=mgpr(0), src1=ldsInc, comment="Move LDS write address to next line" )) |
| 8555 |
directToLdsLoads+=1 |
| 8556 |
destVgpr=0 |
| 8557 |
self.vgprs.globalReadRegisters[tc].append(0) |
| 8558 |
else: |
| 8559 |
g2lIdxM = i * max(loadWidth * tP["bpeRatio"], 1) |
| 8560 |
destVgpr = destVgprPrefix + "+%u"%((g2lIdx+eccOffset+tP["shiftGR"]) if not tP["isM"] else g2lIdxM) |
| 8561 |
self.vgprs.globalReadRegisters[tc].append(g2lIdx+eccOffset+tP["shiftGR"] if not tP["isM"] else g2lIdxM) |
| 8562 |
if tP["isM"]: |
| 8563 |
assert(graIdx <= self.states.m.numVgprG2LAllocated) |
| 8564 |
|
| 8565 |
|
| 8566 |
datatype = kernel["ProblemType"]["DataType%s"%tc] if kernel["ConvertAfterDS"] else kernel["ProblemType"]["DataType"] |
| 8567 |
isHigh16Bits = (datatype.isHalf() or datatype.isBFloat16()) and loopCnt%2==1 if not tP["isM"] else False |
| 8568 |
if tc == "A" and record[0] == True: |
| 8569 |
self.globalread_gpr_record.a.addrVgpr.append(offsetVgpr) |
| 8570 |
self.globalread_gpr_record.a.offset.append(soffset) |
| 8571 |
elif tc == "B" and record[1] == True: |
| 8572 |
self.globalread_gpr_record.b.addrVgpr.append(offsetVgpr) |
| 8573 |
self.globalread_gpr_record.b.offset.append(soffset) |
| 8574 |
|
| 8575 |
useBuffer = not isTr |
| 8576 |
loadModule.add( self.chooseGlobalRead(useBuffer, \ |
| 8577 |
bpl, destVgpr=destVgpr, \ |
| 8578 |
addr0=vgpr(offsetVgpr), addr1=sgpr("Srd%s"%tc, 2 if isTr else 4), \ |
| 8579 |
soffset=soffset, offset=instOffset, \ |
| 8580 |
glc=isGlc, slc=isSlc, nt=isNT, lds=isLds, \ |
| 8581 |
tr=isTr, hi16=isHigh16Bits , \ |
| 8582 |
comment="G -> Reg %u_%u_%u_%u"%(para, sPara, perp, sPerp))) |
| 8583 |
|
| 8584 |
if unrollMirrorWithSoffset: |
| 8585 |
codeMod = Module("mirrorIdx%u"%loopCnt) |
| 8586 |
codeMod.add(VAddU32(dst=vgpr(offsetVgpr), src0=vgpr(offsetVgpr), src1=soffset_prev, comment="mirror unroll: restore GRO=GRO+SGRO")) |
| 8587 |
loadModule.add(codeMod) |
| 8588 |
|
| 8589 |
if kernel["DirectToLds%s"%tc] and kernel["UseInstOffsetForGRO"]: |
| 8590 |
instOffset += ldsInc |
| 8591 |
|
| 8592 |
|
| 8593 |
else: |
| 8594 |
destVgpr = destVgprPrefix + "+%u"%(g2lIdx + tP["shiftGR"]) |
| 8595 |
loadModule.add( self.chooseGlobalRead(False, \ |
| 8596 |
bpl, \ |
| 8597 |
destVgpr=destVgpr, \ |
| 8598 |
addr0=vgpr("GlobalReadAddr%s+%u"%(tc,graIdx),2), addr1="", \ |
| 8599 |
soffset=0, offset=0, \ |
| 8600 |
glc=isGlc, slc=isSlc, nt=isNT, lds=isLds, \ |
| 8601 |
hi16=(kernel["ProblemType"]["DataType"].isHalf() or kernel["ProblemType"]["DataType"].isBFloat16()) and loopCnt%2==1, \ |
| 8602 |
comment="G -> Reg %u_%u_%u_%u"%(para, sPara, perp, sPerp ))) |
| 8603 |
|
| 8604 |
if kernel["ProblemType"]["Sparse"] and kernel["DirectToVgprSparseMetadata"]: |
| 8605 |
if tP["is_sparse"]: |
| 8606 |
if kernel["PrefetchGlobalRead"] == 1 and unrollLoopIdx % 2 == 0: |
| 8607 |
offsetBlk = self.states.m.numVgprValuPerBlock |
| 8608 |
elif kernel["PrefetchGlobalRead"] == 2: |
| 8609 |
offsetBlk = self.states.m.numVgprValuPerBlock * 2 |
| 8610 |
else: |
| 8611 |
offsetBlk = 0 |
| 8612 |
miWaveTile = kernel["MIWaveTileA"] if tP["isA"] else kernel["MIWaveTileB"] |
| 8613 |
for wtIdx in range(0, miWaveTile): |
| 8614 |
offsetVgpr= "GlobalReadOffsetMetadata+%u"%wtIdx |
| 8615 |
for unrollIdx in range(0, kernel["LoopIters"]): |
| 8616 |
bpl = kernel["MIInputPerThread"]//8 |
| 8617 |
constOffset = unrollIdx * kernel["MatrixInstK"] // 8 |
| 8618 |
codeMod = Module("load metadata%u"%loopCnt) |
| 8619 |
imod.middle.add(codeMod) |
| 8620 |
codeMod.add( self.chooseGlobalRead(kernel["BufferLoad"], \ |
| 8621 |
bpl, \ |
| 8622 |
destVgpr="ValuMetadata+%u+%u"%(offsetBlk, (wtIdx*kernel["LoopIters"]+unrollIdx)), \ |
| 8623 |
addr0=vgpr(offsetVgpr), addr1=sgpr("SrdMetadata",4), \ |
| 8624 |
soffset=0, offset=constOffset, \ |
| 8625 |
glc=isGlc, slc=isSlc, nt=isNT, lds=isLds, \ |
| 8626 |
hi16=0, \ |
| 8627 |
comment="G -> Reg ValuMetadata")) |
| 8628 |
if tc == "A" and record[0] == True: |
| 8629 |
self.globalread_gpr_record.a.addrVgpr = [] |
| 8630 |
self.globalread_gpr_record.a.offset = [] |
| 8631 |
elif tc == "B" and record[1] == True: |
| 8632 |
self.globalread_gpr_record.b.addrVgpr = [] |
| 8633 |
self.globalread_gpr_record.b.offset = [] |
| 8634 |
|
| 8635 |
globalReadBody(tP) |
| 8636 |
|
| 8637 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"] and tP["is_sparse"]: |
| 8638 |
globalReadBody(tP["tpsMetadata"]) |
| 8639 |
|
| 8640 |
if self.db["ConservativeWaitCnt"] & 0x1: |
| 8641 |
imod.footer.add(SBarrier(comment="debug")) |
| 8642 |
imod.footer.add(SWaitCnt(dscnt=0, vlcnt=0, vscnt=0, comment="conservative wait")) |
| 8643 |
imod.footer.add(SBarrier(comment="debug")) |
| 8644 |
|
| 8645 |
|
| 8646 |
|
| 8647 |
if kernel["DirectToLds%s"%tP["tensorChar"]] and not (mode == 1 and kernel["PrefetchGlobalRead"]==2): |
| 8648 |
dst = mgpr(0) |
| 8649 |
src = hex(kernel["LdsNumBytes"]) |
| 8650 |
comment = "Restore LDS clamp at %u bytes"%(kernel["LdsNumBytes"]) |
| 8651 |
|
| 8652 |
if kernel["PrefetchGlobalRead"] == 2: |
| 8653 |
imod.middle.add(SMovB32(dst=dst, src=src, comment=comment)) |
| 8654 |
else: |
| 8655 |
imod.footer.add(SMovB32(dst=dst, src=src, comment=comment)) |
| 8656 |
|
| 8657 |
return imod |
| 8658 |
|
| 8659 |
|
| 8660 |
|
| 8661 |
|
| 8662 |
|
| 8663 |
|
| 8664 |
def localWriteSwapOffsets(self, kernel, internalPointerSwap, tP): |
| 8665 |
tc = tP["tensorChar"] |
| 8666 |
if not self.do["LocalWrite%s"%tc]: return Module("localWriteSwapOffsets (No local write%s)"%tc) |
| 8667 |
needSwap = False if kernel["1LDSBuffer"] else True |
| 8668 |
doMetadataCheck = kernel["ProblemType"]["Sparse"] and \ |
| 8669 |
((kernel["ProblemType"]["Sparse"] ==2 and tP["isB"]) or (kernel["ProblemType"]["Sparse"] == 1 and tP["isA"])) |
| 8670 |
needMetaSwap = needSwap and doMetadataCheck |
| 8671 |
|
| 8672 |
if (tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc]: |
| 8673 |
needSwap = False |
| 8674 |
if doMetadataCheck: |
| 8675 |
if kernel["DirectToVgprSparseMetadata"]: |
| 8676 |
needMetaSwap = (kernel["PrefetchGlobalRead"] == 2 and kernel["ExpandPointerSwap"]) |
| 8677 |
|
| 8678 |
if not (needSwap or needMetaSwap): return Module("localWriteSwapOffsets (Empty)") |
| 8679 |
module = Module("localWriteSwapOffsets") |
| 8680 |
|
| 8681 |
def localWriteSwapXOR(tc, src0Val, numLwa): |
| 8682 |
if kernel["LocalWriteUseSgpr%s"%tc]: |
| 8683 |
module.add(SXorB32( |
| 8684 |
dst=sgpr("LocalWriteAddr%s"%tc), \ |
| 8685 |
src0=src0Val, \ |
| 8686 |
src1=sgpr("LocalWriteAddr%s"%tc), \ |
| 8687 |
comment="swap Red Blk SGPR")) |
| 8688 |
else: |
| 8689 |
module.add(VXorB32( |
| 8690 |
dst=vgpr("LocalWriteAddr%s"%tc), \ |
| 8691 |
src0=src0Val, \ |
| 8692 |
src1=vgpr("LocalWriteAddr%s"%tc), \ |
| 8693 |
comment="swap Red Blk")) |
| 8694 |
for i in range(1,numLwa): |
| 8695 |
module.add(VAddU32( |
| 8696 |
dst=vgpr("LocalWriteAddr%s+%u"%(tc,i)), \ |
| 8697 |
src0=(i * self.states.regCaps["maxLDSConstOffset"]), \ |
| 8698 |
src1=vgpr("LocalWriteAddr%s"%tc), \ |
| 8699 |
comment="Final Offset Plus %uK"%((i * self.states.regCaps["maxLDSConstOffset"]) / 1024))) |
| 8700 |
|
| 8701 |
if needSwap: |
| 8702 |
|
| 8703 |
|
| 8704 |
if internalPointerSwap and not kernel["StoreSwapAddr"]: |
| 8705 |
tP["localWriteSwapByteOffset"] = 0 if tP["localWriteSwapByteOffset"] else kernel["LdsOffsetA_Blk"] |
| 8706 |
module.addComment1("(EPS=1) local write swap internal offset -> %u" % tP["localWriteSwapByteOffset"]) |
| 8707 |
else: |
| 8708 |
src0Val = None |
| 8709 |
if kernel["StoreSwapAddr"]: |
| 8710 |
if kernel["LocalWriteUseSgpr%s"%tc]: |
| 8711 |
src0Val = sgpr("Swap%s"%tc) |
| 8712 |
else: |
| 8713 |
src0Val = vgpr("LocalWriteSwapAddr%s"%tc) |
| 8714 |
else: |
| 8715 |
|
| 8716 |
src0Val = hex(kernel["LdsOffsetA_Blk"]) |
| 8717 |
numLwa = self.states.a.numVgprLocalWriteAddr if tP["isA"] else self.states.b.numVgprLocalWriteAddr |
| 8718 |
localWriteSwapXOR(tc, src0Val, numLwa) |
| 8719 |
|
| 8720 |
|
| 8721 |
if needMetaSwap: |
| 8722 |
if kernel["DirectToVgprSparseMetadata"]: |
| 8723 |
tP["metadataWriteSwapByteOffset"] = 0 if tP["metadataWriteSwapByteOffset"] else self.states.m.numVgprValuPerBlock |
| 8724 |
module.addComment1("metadata write swap offset -> %u" % tP["metadataWriteSwapByteOffset"]) |
| 8725 |
else: |
| 8726 |
tc = "Metadata" |
| 8727 |
tPM = tP["tpsMetadata"] |
| 8728 |
if internalPointerSwap and not kernel["StoreSwapAddr"]: |
| 8729 |
tPM["localWriteSwapByteOffset"] = 0 if tPM["localWriteSwapByteOffset"] else kernel["LdsOffsetA_Blk"] |
| 8730 |
module.addComment1("(EPS=1) local write swap internal offset -> %u" % tPM["localWriteSwapByteOffset"]) |
| 8731 |
else: |
| 8732 |
if kernel["StoreSwapAddr"]: |
| 8733 |
if kernel["LocalWriteUseSgpr%s"%tc]: |
| 8734 |
src0Val = sgpr("Swap%s"%tc) |
| 8735 |
else: |
| 8736 |
src0Val = vgpr("LocalWriteSwapAddr%s"%tc) |
| 8737 |
else: |
| 8738 |
|
| 8739 |
src0Val = hex(kernel["LdsOffsetA_Blk"]) |
| 8740 |
numLwa = self.states.m.numVgprLocalWriteAddr |
| 8741 |
localWriteSwapXOR(tc, src0Val, numLwa) |
| 8742 |
return module |
| 8743 |
|
| 8744 |
|
| 8745 |
|
| 8746 |
|
| 8747 |
|
| 8748 |
def localWriteResetOffsets(self, kernel, internalPointerSwap, tP): |
| 8749 |
tc = tP["tensorChar"] |
| 8750 |
if not self.do["LocalWrite%s"%tc]: return Module("localWriteResetOffsets (no local write%s)"%tc) |
| 8751 |
needReset = not (kernel["1LDSBuffer"]) |
| 8752 |
doMetadataCheck = kernel["ProblemType"]["Sparse"] and \ |
| 8753 |
((kernel["ProblemType"]["Sparse"] ==2 and tP["isB"]) or (kernel["ProblemType"]["Sparse"] == 1 and tP["isA"])) |
| 8754 |
needMetaReset = needReset and doMetadataCheck |
| 8755 |
|
| 8756 |
if (tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc]: |
| 8757 |
needReset = False |
| 8758 |
if doMetadataCheck: |
| 8759 |
if kernel["DirectToVgprSparseMetadata"]: |
| 8760 |
needMetaReset = (kernel["PrefetchGlobalRead"] == 2 and kernel["ExpandPointerSwap"]) |
| 8761 |
if not (needReset or needMetaReset): |
| 8762 |
return Module("localWriteResetOffsets (Empty)") |
| 8763 |
module = Module("localWriteResetOffsets") |
| 8764 |
if needReset: |
| 8765 |
resetMask = hex(kernel["LdsOffsetA_Blk"]-1 | self.consts.ldsOOB) |
| 8766 |
if internalPointerSwap or kernel["StoreSwapAddr"]: |
| 8767 |
if internalPointerSwap: |
| 8768 |
tP["localWriteSwapByteOffset"] = 0 |
| 8769 |
else: |
| 8770 |
if kernel["LocalWriteUseSgpr%s"%tc]: |
| 8771 |
tmpsgpr = self.sgprPool.checkOut(1) |
| 8772 |
module.add(SXorB32( |
| 8773 |
dst=sgpr(tmpsgpr), \ |
| 8774 |
src0=sgpr("Swap%s"%tc), \ |
| 8775 |
src1=sgpr("LocalWriteAddr%s"%tP["tensorChar"]), \ |
| 8776 |
comment="Get other lds buffer offset value")) |
| 8777 |
module.add(SMinU32(dst=sgpr("LocalWriteAddr%s"%tP["tensorChar"]), \ |
| 8778 |
src0=sgpr("LocalWriteAddr%s"%tP["tensorChar"]), \ |
| 8779 |
src1=sgpr(tmpsgpr), comment="Set LWA to first buffer offset" )) |
| 8780 |
self.sgprPool.checkIn(tmpsgpr) |
| 8781 |
else: |
| 8782 |
tmpvgpr = self.vgprPool.checkOut(1) |
| 8783 |
module.add(VXorB32( |
| 8784 |
dst=vgpr(tmpvgpr), \ |
| 8785 |
src0=vgpr("LocalWriteSwapAddr%s"%tc), \ |
| 8786 |
src1=vgpr("LocalWriteAddr%s"%tc), \ |
| 8787 |
comment="Get other lds buffer offset")) |
| 8788 |
module.add(VMinI32(dst=vgpr("LocalWriteAddr%s"%tc), \ |
| 8789 |
src0=vgpr("LocalWriteAddr%s"%tc), \ |
| 8790 |
src1=vgpr(tmpvgpr), |
| 8791 |
comment="Set LWA to first buffer offset")) |
| 8792 |
self.vgprPool.checkIn(tmpvgpr) |
| 8793 |
else: |
| 8794 |
if kernel["LocalWriteUseSgpr%s"%tc]: |
| 8795 |
module.add(SAndB32( |
| 8796 |
dst=sgpr("LocalWriteAddr%s"%tP["tensorChar"]), \ |
| 8797 |
src0=resetMask, \ |
| 8798 |
src1=sgpr("LocalWriteAddr%s"%tP["tensorChar"]), \ |
| 8799 |
comment="reset to Red")) |
| 8800 |
else: |
| 8801 |
module.add(VAndB32( |
| 8802 |
dst=vgpr("LocalWriteAddr%s"%tc), \ |
| 8803 |
src0=resetMask, \ |
| 8804 |
src1=vgpr("LocalWriteAddr%s"%tc), \ |
| 8805 |
comment="reset to Red")) |
| 8806 |
if needMetaReset: |
| 8807 |
if kernel["DirectToVgprSparseMetadata"]: |
| 8808 |
tP["metadataWriteSwapByteOffset"] = 0 |
| 8809 |
module.addComment1("reset metadata write offset to %u" % tP["metadataWriteSwapByteOffset"]) |
| 8810 |
else: |
| 8811 |
tPM = tP["tpsMetadata"] |
| 8812 |
resetMask = hex(kernel["LdsOffsetA_Blk"]-1 | self.consts.ldsOOB) |
| 8813 |
if internalPointerSwap: |
| 8814 |
tPM["localWriteSwapByteOffset"] = 0 |
| 8815 |
elif kernel["StoreSwapAddr"]: |
| 8816 |
if kernel["LocalWriteUseSgpr%s"%tPM["tensorChar"]]: |
| 8817 |
tmpsgpr = self.sgprPool.checkOut(1) |
| 8818 |
module.add(SXorB32( |
| 8819 |
dst=sgpr(tmpsgpr), \ |
| 8820 |
src0=sgpr("Swap%s"%tPM["tensorChar"]), \ |
| 8821 |
src1=sgpr("LocalWriteAddr%s"%tPM["tensorChar"]), \ |
| 8822 |
comment="Get other lds buffer offset value")) |
| 8823 |
module.add(SMinU32(dst=sgpr("LocalWriteAddr%s"%tPM["tensorChar"]), \ |
| 8824 |
src0=sgpr("LocalWriteAddr%s"%tPM["tensorChar"]), \ |
| 8825 |
src1=sgpr(tmpsgpr), comment="Set LWA to first buffer offset" )) |
| 8826 |
self.sgprPool.checkIn(tmpsgpr) |
| 8827 |
else: |
| 8828 |
tmpvgpr = self.vgprPool.checkOut(1) |
| 8829 |
module.add(VXorB32( |
| 8830 |
dst=vgpr(tmpvgpr), \ |
| 8831 |
src0=vgpr("LocalWriteSwapAddr%s"%tPM["tensorChar"]), \ |
| 8832 |
src1=vgpr("LocalWriteAddr%s"%tPM["tensorChar"]), \ |
| 8833 |
comment="Get other lds buffer offset")) |
| 8834 |
module.add(VMinI32(dst=vgpr("LocalWriteAddr%s"%tPM["tensorChar"]), \ |
| 8835 |
src0=vgpr("LocalWriteAddr%s"%tPM["tensorChar"]), \ |
| 8836 |
src1=vgpr(tmpvgpr), |
| 8837 |
comment="Set LWA to first buffer offset")) |
| 8838 |
self.vgprPool.checkIn(tmpvgpr) |
| 8839 |
else: |
| 8840 |
module.add(VAndB32( |
| 8841 |
dst=vgpr("LocalWriteAddr%s+%u"%(tPM["tensorChar"], 0)), \ |
| 8842 |
src0=resetMask, \ |
| 8843 |
src1=vgpr("LocalWriteAddr%s+%u"%(tPM["tensorChar"], 0)), \ |
| 8844 |
comment="reset to Red")) |
| 8845 |
|
| 8846 |
numLwa = 0 |
| 8847 |
if tP["isA"]: |
| 8848 |
numLwa = self.states.a.numVgprLocalWriteAddr |
| 8849 |
elif tP["isB"]: |
| 8850 |
numLwa = self.states.b.numVgprLocalWriteAddr |
| 8851 |
elif tP["isM"]: |
| 8852 |
numLwa = self.states.m.numVgprLocalWriteAddr |
| 8853 |
|
| 8854 |
for i in range(1, numLwa): |
| 8855 |
module.add(VAddU32( |
| 8856 |
dst=vgpr("LocalWriteAddr%s+%u"%(tc, i)), \ |
| 8857 |
src0=(i * self.states.regCaps["maxLDSConstOffset"]), \ |
| 8858 |
src1=vgpr("LocalWriteAddr%s"%tc), \ |
| 8859 |
comment="Final Offset Plus %uK"%((i * self.states.regCaps["maxLDSConstOffset"]) / 1024))) |
| 8860 |
return module |
| 8861 |
|
| 8862 |
|
| 8863 |
|
| 8864 |
|
| 8865 |
|
| 8866 |
|
| 8867 |
|
| 8868 |
|
| 8869 |
|
| 8870 |
|
| 8871 |
|
| 8872 |
|
| 8873 |
|
| 8874 |
|
| 8875 |
|
| 8876 |
|
| 8877 |
|
| 8878 |
|
| 8879 |
|
| 8880 |
|
| 8881 |
|
| 8882 |
|
| 8883 |
|
| 8884 |
|
| 8885 |
|
| 8886 |
|
| 8887 |
|
| 8888 |
def calculateLdsWriteOffset(self, perp, para, sPerp, sPara, kernel, tP): |
| 8889 |
tc = tP["tensorChar"] |
| 8890 |
mask = 0 |
| 8891 |
|
| 8892 |
lscaOffset = para * kernel[tP["lsc"]] |
| 8893 |
perp_masked = perp |
| 8894 |
perp_rem = 0 |
| 8895 |
lspaOffset = perp_masked * kernel[tP["lsp"]] |
| 8896 |
rem = 0 |
| 8897 |
|
| 8898 |
|
| 8899 |
|
| 8900 |
assert(sPerp==0 or sPara==0) |
| 8901 |
|
| 8902 |
if tP["tlu"] != kernel["UnrollMajorLDS%s" % tP["tensorChar"]]: |
| 8903 |
lspaOffset += sPerp & mask |
| 8904 |
lscaOffset += sPara |
| 8905 |
rem = (sPerp & ~mask) |
| 8906 |
i = sPara + (tP["nrcv"]//tP["nrcvpi"]) * (para + tP["nrc"] * (sPerp + tP["nrpv"] * perp_masked)) |
| 8907 |
|
| 8908 |
else: |
| 8909 |
lscaOffset += sPara |
| 8910 |
lspaOffset += sPerp |
| 8911 |
rem = 0 |
| 8912 |
i = sPara + (tP["nrcv"]//tP["nrcvpi"]) * (para * tP["glvw"] + tP["nrc"] * (sPerp + tP["glvw"] * tP["nrpv"] * perp )) |
| 8913 |
|
| 8914 |
|
| 8915 |
|
| 8916 |
|
| 8917 |
|
| 8918 |
|
| 8919 |
|
| 8920 |
|
| 8921 |
LdsPad = kernel["LdsPad%s"%tc] if kernel["LdsBlockSizePerPad%s"%tc] == 0 else 0 |
| 8922 |
lds_stride = (kernel["_DepthU%s"%tc] + LdsPad) if kernel["UnrollMajorLDS%s" % tP["tensorChar"]] \ |
| 8923 |
else (kernel[tP["mt"]] + LdsPad) |
| 8924 |
|
| 8925 |
if tP["tlu"] != kernel["UnrollMajorLDS%s" % tP["tensorChar"]]: |
| 8926 |
lspaOffset *= lds_stride |
| 8927 |
lspaOffset += rem + perp_rem |
| 8928 |
else: |
| 8929 |
lscaOffset *= lds_stride |
| 8930 |
lscaOffset += rem |
| 8931 |
|
| 8932 |
|
| 8933 |
|
| 8934 |
|
| 8935 |
|
| 8936 |
|
| 8937 |
|
| 8938 |
|
| 8939 |
|
| 8940 |
offsetElements = (lspaOffset + lscaOffset) |
| 8941 |
|
| 8942 |
offsetBytes = offsetElements*tP["bpeDS"] |
| 8943 |
|
| 8944 |
if kernel["LdsBlockSizePerPad%s"%tc] != 0 and kernel["LdsPad%s"%tc] != 0: |
| 8945 |
offsetBytes = offsetBytes + (offsetBytes // kernel["LdsBlockSizePerPad%s"%tc]) * kernel["LdsPad%s"%tc] * tP["bpeDS"] |
| 8946 |
|
| 8947 |
offsetBytes += tP["localWriteSwapByteOffset"] |
| 8948 |
|
| 8949 |
|
| 8950 |
|
| 8951 |
|
| 8952 |
comment = "lwo%s_%u_%u_%u_%u = (%s%d*%s)" \ |
| 8953 |
% (tP["tensorChar"], \ |
| 8954 |
para, sPara, perp, sPerp, \ |
| 8955 |
(("%u + "%sPara) if tP["wtc"] else ""), \ |
| 8956 |
para, tP["lsc"] ) |
| 8957 |
if not tP["tlu"]: |
| 8958 |
comment += "*(MT%s+PAD)" % (tP["tileChar"]) |
| 8959 |
comment += " + (%d*%s)" % (perp, tP["lsp"]) |
| 8960 |
if tP["tlu"]: |
| 8961 |
comment += "(*MT%s+PAD)" % (tP["tileChar"]) |
| 8962 |
comment += " = %u" % (offsetBytes) |
| 8963 |
|
| 8964 |
return (offsetBytes, i, comment) |
| 8965 |
|
| 8966 |
def recalcLocalWriteAddresses(self, kernel, tP): |
| 8967 |
|
| 8968 |
tc = tP["tensorChar"] |
| 8969 |
|
| 8970 |
module = Module("recalcLocalWriteAddresses") |
| 8971 |
module.addComment1("recalculate LocalWriteAddr{}".format(tc)) |
| 8972 |
|
| 8973 |
lwvw = getattr(self, "localWriteWidth{}".format(tc)) |
| 8974 |
newInstIdx = self.selectMemoryInstruction("LocalWrite", lwvw, \ |
| 8975 |
False, \ |
| 8976 |
tP["localWrite2Coalesced"], tP["localWrite2Perpendicular"], |
| 8977 |
[tP["localWriteStrideTile"], tP["localWriteStrideUnroll"]] ) |
| 8978 |
tP["localWriteInstruction"] = self.memoryInstructions["LocalWrite"][newInstIdx] |
| 8979 |
|
| 8980 |
loopComponent = Component.PersistentLoop.find(self) |
| 8981 |
module.add(loopComponent.recalcLocalWriteAddresses(self, kernel, tc)) |
| 8982 |
|
| 8983 |
|
| 8984 |
module.add(self.lwaTileAssignment(kernel, tP)) |
| 8985 |
|
| 8986 |
module.add(self.lwaUnrollAssignment(kernel, tP)) |
| 8987 |
|
| 8988 |
module.add(self.lwaFirstOffset(kernel, tP)) |
| 8989 |
|
| 8990 |
|
| 8991 |
module.add(self.graTileAssignment(kernel, tP)) |
| 8992 |
|
| 8993 |
module.add(self.graTileOffsets(kernel, tP)) |
| 8994 |
|
| 8995 |
module.add(self.graUnrollOffsets(kernel, tP)) |
| 8996 |
|
| 8997 |
|
| 8998 |
self.graFinalOffsets(kernel, tP) |
| 8999 |
|
| 9000 |
if kernel["ProblemType"]["Sparse"] and kernel["DirectToVgprSparseMetadata"]: |
| 9001 |
if (kernel["ProblemType"]["Sparse"] == 2 and tP["isB"]) or (kernel["ProblemType"]["Sparse"] == 1 and tP["isA"]): |
| 9002 |
graMetadataFinalOffsets(kernel, tP) |
| 9003 |
|
| 9004 |
return module |
| 9005 |
|
| 9006 |
def recalcLocalReadAddressesAB(self, kernel, tPA, tPB): |
| 9007 |
imod = Module() |
| 9008 |
|
| 9009 |
if self.states.inTailLoop: |
| 9010 |
|
| 9011 |
|
| 9012 |
|
| 9013 |
|
| 9014 |
if not kernel["UseDotInstruction"] and ((self.states.numReadsIterCoalescedA > 1 or self.states.numReadsIterCoalescedB > 1)): |
| 9015 |
loopComponent = Component.PersistentLoop.find(self) |
| 9016 |
imod.add(loopComponent.recalcLocalReadAddressesAB(self, kernel)) |
| 9017 |
|
| 9018 |
self.states.numReadsIterCoalescedA = 1 |
| 9019 |
self.states.numReadsIterCoalescedB = 1 |
| 9020 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 9021 |
self.states.numReadsIterCoalescedMetadata = 1 |
| 9022 |
tPM = tPA["tpsMetadata"] if tPA["is_sparse"] else tPB["tpsMetadata"] |
| 9023 |
imod.add(self.lraTileAssignment(kernel, tPA, tPB)) |
| 9024 |
imod.add(self.lraFinalOffset(kernel, tPA)) |
| 9025 |
imod.add(self.lraDeclareAddresses(kernel, tPA)) |
| 9026 |
imod.add(self.lraFinalOffset(kernel, tPB)) |
| 9027 |
imod.add(self.lraDeclareAddresses(kernel, tPB)) |
| 9028 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 9029 |
imod.add(self.lraFinalOffset(kernel, tPM)) |
| 9030 |
imod.add(self.lraDeclareAddresses(kernel, tPM)) |
| 9031 |
localRead2Perpendicular = False |
| 9032 |
instructions = self.memoryInstructions |
| 9033 |
|
| 9034 |
if kernel["UnrollMajorLDSA"]: |
| 9035 |
localReadWidth = (kernel["MIInputPerThreadA"] * tPA["bpeDS"]) // self.states.bpr |
| 9036 |
localReadInstructionIdxA = \ |
| 9037 |
self.selectMemoryInstruction("LocalRead", localReadWidth, \ |
| 9038 |
False, \ |
| 9039 |
tPA["localRead2Coalesced"], localRead2Perpendicular, |
| 9040 |
[tPB["localReadStrideCoalesced"]] ) |
| 9041 |
tPA["localReadInstruction"] = instructions["LocalRead"][localReadInstructionIdxA] |
| 9042 |
|
| 9043 |
|
| 9044 |
if kernel["UnrollMajorLDSB"]: |
| 9045 |
localReadWidth = (kernel["MIInputPerThreadB"] * tPB["bpeDS"]) // self.states.bpr |
| 9046 |
localReadInstructionIdxB = \ |
| 9047 |
self.selectMemoryInstruction("LocalRead", localReadWidth, \ |
| 9048 |
False, \ |
| 9049 |
tPB["localRead2Coalesced"], localRead2Perpendicular, |
| 9050 |
[tPB["localReadStrideCoalesced"]] ) |
| 9051 |
tPB["localReadInstruction"] = instructions["LocalRead"][localReadInstructionIdxB] |
| 9052 |
|
| 9053 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 9054 |
localReadWidth = tPM["bpeDS"] / self.states.bpr |
| 9055 |
if kernel["UnrollMajorLDSMetadata"]: |
| 9056 |
localReadWidth = (kernel["MIInputPerThreadMetadata"] * tPM["bpeDS"]) // self.states.bpr |
| 9057 |
|
| 9058 |
localReadInstructionIdxM = \ |
| 9059 |
self.selectMemoryInstruction("LocalRead", localReadWidth, \ |
| 9060 |
False, \ |
| 9061 |
tPM["localRead2Coalesced"], localRead2Perpendicular, |
| 9062 |
[ tPM["localReadStrideCoalesced"]] ) |
| 9063 |
tPM["localReadInstruction"] = instructions["LocalRead"][ \ |
| 9064 |
localReadInstructionIdxM] |
| 9065 |
|
| 9066 |
elif kernel["UseDotInstruction"] and (kernel["InnerUnroll"] > 1): |
| 9067 |
self.states.numReadsIterCoalescedA = 1 |
| 9068 |
self.states.numReadsIterCoalescedB = 1 |
| 9069 |
self.states.lrvwUnrollA //= kernel["InnerUnroll"] |
| 9070 |
self.states.lrvwUnrollB //= kernel["InnerUnroll"] |
| 9071 |
localRead2Perpendicular = False |
| 9072 |
instructions = self.memoryInstructions |
| 9073 |
if kernel["UnrollMajorLDSA"]: |
| 9074 |
localReadWidth = (kernel["NumDotElements"] * tPA["bpeDS"]) // self.states.bpr |
| 9075 |
localReadInstructionIdxA = \ |
| 9076 |
self.selectMemoryInstruction("LocalRead", localReadWidth, \ |
| 9077 |
False, \ |
| 9078 |
tPA["localRead2Coalesced"], localRead2Perpendicular, |
| 9079 |
[tPB["localReadStrideCoalesced"]] ) |
| 9080 |
tPA["localReadInstruction"] = instructions["LocalRead"][localReadInstructionIdxA] |
| 9081 |
if kernel["UnrollMajorLDSB"]: |
| 9082 |
localReadWidth = (kernel["NumDotElements"] * tPB["bpeDS"]) // self.states.bpr |
| 9083 |
localReadInstructionIdxB = \ |
| 9084 |
self.selectMemoryInstruction("LocalRead", localReadWidth, \ |
| 9085 |
False, \ |
| 9086 |
tPB["localRead2Coalesced"], localRead2Perpendicular, |
| 9087 |
[tPB["localReadStrideCoalesced"]] ) |
| 9088 |
tPB["localReadInstruction"] = instructions["LocalRead"][localReadInstructionIdxB] |
| 9089 |
imod.add(self.lraTileAssignment(kernel, tPA, tPB)) |
| 9090 |
imod.add(self.lraFinalOffset(kernel, tPA)) |
| 9091 |
imod.add(self.lraDeclareAddresses(kernel, tPA)) |
| 9092 |
imod.add(self.lraFinalOffset(kernel, tPB)) |
| 9093 |
imod.add(self.lraDeclareAddresses(kernel, tPB)) |
| 9094 |
|
| 9095 |
return imod |
| 9096 |
|
| 9097 |
|
| 9098 |
|
| 9099 |
|
| 9100 |
def preLoopLocalWriteDo(self, kernel, tPA, tPB): |
| 9101 |
imod = Module() |
| 9102 |
|
| 9103 |
LWDoMod = imod.add(Module()) |
| 9104 |
LWDoA = self.localWriteDo(kernel, tPA) if self.do["LocalWrite%s"%tPA["tensorChar"]] else Module() |
| 9105 |
LWDoB = self.localWriteDo(kernel, tPB) if self.do["LocalWrite%s"%tPB["tensorChar"]] else Module() |
| 9106 |
LWDoMod.addComment1("local write a") |
| 9107 |
LWDoMod.add(LWDoA) |
| 9108 |
LWDoMod.addComment1("local write b") |
| 9109 |
LWDoMod.add(LWDoB) |
| 9110 |
return imod |
| 9111 |
|
| 9112 |
|
| 9113 |
|
| 9114 |
|
| 9115 |
def localWriteDo(self, kernel, tP, swapAB=0): |
| 9116 |
|
| 9117 |
tc = tP["tensorChar"] |
| 9118 |
imod = Module() |
| 9119 |
isBpeInputLarger = True if tP["bpeGR"] > tP["bpe"] else False |
| 9120 |
|
| 9121 |
def localWriteBody(tP): |
| 9122 |
tc = tP["tensorChar"] |
| 9123 |
|
| 9124 |
instruction = tP["localWriteInstruction"] |
| 9125 |
numBlocks = instruction.numBlocks |
| 9126 |
numOffsets = instruction.numOffsets |
| 9127 |
blockWidth = instruction.blockWidth |
| 9128 |
|
| 9129 |
g2lIdx = 0 |
| 9130 |
|
| 9131 |
tmpLocalWriteAddr = -1 |
| 9132 |
|
| 9133 |
|
| 9134 |
tmpVgprOffset = ((self.states.a.numVgprG2L if (tP['tensorChar'] == 'A') else self.states.m.numVgprG2L if tP["isM"] else self.states.b.numVgprG2L) / 2) if (blockWidth == 0.25) else 0 |
| 9135 |
|
| 9136 |
|
| 9137 |
instructionCnt = 0 |
| 9138 |
Hcvt2BMap = {} |
| 9139 |
g2lIdxDict = {} |
| 9140 |
regTmpVgprBlock = None |
| 9141 |
|
| 9142 |
if swapAB == 1: |
| 9143 |
destVgprPrefix = "G2L%s2"%(tc) |
| 9144 |
else: |
| 9145 |
destVgprPrefix = "G2L%s"%(tc) |
| 9146 |
for perp in range(0, tP["nrp"]): |
| 9147 |
localWriteCode = imod.add(Module("LocalWrite%u perp=%d"%(instructionCnt,perp))) |
| 9148 |
lwa = "LocalWriteAddr%s"%tc |
| 9149 |
|
| 9150 |
for para in range(0, tP["nrc"]): |
| 9151 |
if para>=1: |
| 9152 |
localWriteCode = imod.add(Module("LocalWrite%u perp=%d para=%d"%(instructionCnt,perp,para))) |
| 9153 |
|
| 9154 |
for s in range(0, max(tP["nwcv"],tP["nwpv"])//tP["nwcvpi"]): |
| 9155 |
localWriteCVTCode = Module() |
| 9156 |
sPerp = 0 |
| 9157 |
sPara = 0 |
| 9158 |
needToSplitMetadata = False |
| 9159 |
metadataScalar = 1.0 |
| 9160 |
if tP["tlu"] != kernel["UnrollMajorLDS%s" % tP["tensorChar"]]: |
| 9161 |
if tP["wtc"]: |
| 9162 |
sPerp = s |
| 9163 |
else: |
| 9164 |
if tP["wtc"]: |
| 9165 |
sPara = s |
| 9166 |
if tP["isM"]: |
| 9167 |
needToSplitMetadata = True |
| 9168 |
metadataScalar = max(1, self.states.bpr // ( tP["glvw"] * tP["bpeGR"])) |
| 9169 |
|
| 9170 |
(offset, i, comment) = self.calculateLdsWriteOffset(perp, para, sPerp, sPara, kernel, tP) |
| 9171 |
|
| 9172 |
|
| 9173 |
|
| 9174 |
if tP["bpeGR"] == 4 and tP["bpeDS"] == 2: |
| 9175 |
|
| 9176 |
g2lIdx = int((i * blockWidth) * metadataScalar) * 2 |
| 9177 |
elif tP["glvw"] <= 2: |
| 9178 |
|
| 9179 |
g2lIdx = (i * blockWidth) * metadataScalar |
| 9180 |
if isBpeInputLarger: |
| 9181 |
g2lIdx *= (tP["bpeGR"]// tP["bpeDS"]) |
| 9182 |
g2lIdx = int(g2lIdx) |
| 9183 |
else: |
| 9184 |
g2lIdx = int((i * blockWidth) * metadataScalar) |
| 9185 |
if isBpeInputLarger: |
| 9186 |
g2lIdx *= (tP["bpeGR"]// tP["bpeDS"]) |
| 9187 |
|
| 9188 |
graIdx = i * self.states.rpgo if kernel["BufferLoad"] else i * self.states.rpga |
| 9189 |
|
| 9190 |
if tP["isM"]: |
| 9191 |
if not needToSplitMetadata: |
| 9192 |
g2lIdx = graIdx * ceil(blockWidth) |
| 9193 |
|
| 9194 |
|
| 9195 |
|
| 9196 |
instHi = 0 |
| 9197 |
if g2lIdx in g2lIdxDict: |
| 9198 |
g2lIdxDict[g2lIdx] += 1 |
| 9199 |
else: |
| 9200 |
g2lIdxDict[g2lIdx] = 0 |
| 9201 |
instHi = g2lIdxDict[g2lIdx] |
| 9202 |
|
| 9203 |
if self.states.archCaps["HasEccHalf"] or not self.states.asmCaps["HasWMMA_V1"]: |
| 9204 |
numVgprG2L = self.states.a.numVgprG2L if tc == 'A' else self.states.b.numVgprG2L if tc == 'B' else self.states.m.numVgprG2L |
| 9205 |
eccinstHi = instHi |
| 9206 |
|
| 9207 |
if tP["bpeDS"] == tP["bpeGR"] and (tP["globalReadInstruction"].totalWidth) == 0.5 and (blockWidth == 0.25) and not tP["isM"]: |
| 9208 |
eccinstHi = i // 2 |
| 9209 |
eccBpe = tP["bpeDS"] if kernel["ConvertAfterDS"] else max(tP["bpeGR"], tP["bpe"]) |
| 9210 |
eccOffset = _getEccOffset(tP["globalReadInstruction"].totalWidth, bpr=self.states.bpr, bpe=eccBpe, \ |
| 9211 |
glvw=tP["glvw"], idx=eccinstHi, numVgprG2L=numVgprG2L) |
| 9212 |
else: |
| 9213 |
eccOffset = 0 |
| 9214 |
|
| 9215 |
|
| 9216 |
if (blockWidth == 0.25) and ((s % 4) == 0) and (not tP["isM"] or needToSplitMetadata): |
| 9217 |
src = destVgprPrefix + "+%u" % (g2lIdx + eccOffset) |
| 9218 |
dst = destVgprPrefix + "+%u+%u" % (tmpVgprOffset, g2lIdx) |
| 9219 |
if tP["bpeDS"] != tP["bpeGR"]: |
| 9220 |
if kernel["ProblemType"]["DataType%s"%tc].isHalf(): |
| 9221 |
if tP["glvw"] > 1: |
| 9222 |
dst = destVgprPrefix + "+%u+%u" % (tmpVgprOffset, g2lIdx // 2) |
| 9223 |
localWriteCVTCode.add(VPackF16toB32(dst=vgpr(dst), src0=vgpr(src), src1=vgpr(destVgprPrefix + "+%u" % (g2lIdx+1)), \ |
| 9224 |
vop3=VOP3PModifiers(op_sel=[1,1,0]), comment="Pack with neighbor")) |
| 9225 |
localWriteCVTCode.add(VPackF16toB32(dst=vgpr(src), src0=vgpr(src), src1=vgpr(destVgprPrefix + "+%u" % (g2lIdx+1)), \ |
| 9226 |
vop3=VOP3PModifiers(op_sel=[0,0,0]), comment="Pack with neighbor")) |
| 9227 |
else: |
| 9228 |
printExit("Unsupported combination DataType%s (%s) -> DataType (%s)"%(tc, kernel["ProblemType"]["DataType%s"%tc].toChar(), kernel["ProblemType"]["DataType"].toChar())) |
| 9229 |
elif tP["glvw"] > 1: |
| 9230 |
localWriteCVTCode.add(VMovB32(dst=vgpr(dst), src=vgpr(src), comment="another VGPR storing lshr 8-bit value")) |
| 9231 |
localWriteCVTCode.add(VLShiftRightB32(dst=vgpr(dst), shiftHex=hex(8), src=vgpr(dst), comment="G2L Vpgr >> 8")) |
| 9232 |
if kernel["ExpertSchedulingMode"] > 0: |
| 9233 |
localWriteCVTCode.add(SWaitAlu(va_vdst=0, comment="wait for writes to complete")) |
| 9234 |
|
| 9235 |
|
| 9236 |
isHigh16Bits = False |
| 9237 |
isCvtHighBits = False |
| 9238 |
datatype = kernel["ProblemType"]["DataType%s"%tc] if kernel["ConvertAfterDS"] else kernel["ProblemType"]["DataType"] |
| 9239 |
if (datatype.isHalf() or datatype.isBFloat16()) and not tP["isM"]: |
| 9240 |
if s%2==1: |
| 9241 |
isHigh16Bits = True |
| 9242 |
if (blockWidth == 0.5) and (instHi % 2 == 1): |
| 9243 |
isHigh16Bits = True |
| 9244 |
if kernel["ProblemType"]["DataType%s"%tc].isAnyFloat8(): |
| 9245 |
if g2lIdx%2 == 1: |
| 9246 |
isCvtHighBits = True |
| 9247 |
|
| 9248 |
|
| 9249 |
|
| 9250 |
|
| 9251 |
|
| 9252 |
elif datatype.isInt8() or datatype.is8bitFloat() or tP["isM"]: |
| 9253 |
isHigh16Bits = (s % 4) > 1 |
| 9254 |
|
| 9255 |
|
| 9256 |
|
| 9257 |
|
| 9258 |
paramList = [] |
| 9259 |
numsOfRegister = [] |
| 9260 |
globalBlockWidth = tP["globalReadInstruction"].totalWidth |
| 9261 |
|
| 9262 |
|
| 9263 |
|
| 9264 |
for _ in range(0, numBlocks): |
| 9265 |
|
| 9266 |
if globalBlockWidth == 1 and blockWidth == 0.5 and tP["glvw"] == 1: |
| 9267 |
if isHigh16Bits: |
| 9268 |
paramList.append(vgpr(destVgprPrefix + "+%u"%(g2lIdx + 1))) |
| 9269 |
else: |
| 9270 |
paramList.append(vgpr(destVgprPrefix + "+%u"%(g2lIdx))) |
| 9271 |
numsOfRegister.append(blockWidth) |
| 9272 |
elif globalBlockWidth == blockWidth and tP["glvw"] == 1: |
| 9273 |
|
| 9274 |
|
| 9275 |
|
| 9276 |
|
| 9277 |
|
| 9278 |
|
| 9279 |
|
| 9280 |
paramList.append(vgpr(destVgprPrefix + "+%u"%(self.vgprs.globalReadRegisters[tc][i]), blockWidth)) |
| 9281 |
|
| 9282 |
elif blockWidth == 1: |
| 9283 |
paramList.append(vgpr(destVgprPrefix + "+%u"%(g2lIdx))) |
| 9284 |
numsOfRegister.append(1) |
| 9285 |
elif blockWidth == 0.25 and ((s % 2) == 1): |
| 9286 |
if tP["bpeDS"] != tP["bpeGR"] and tmpVgprOffset != 0: |
| 9287 |
paramList.append(vgpr(destVgprPrefix + "+%u+%u"%(tmpVgprOffset, g2lIdx // 2))) |
| 9288 |
else: |
| 9289 |
paramList.append(vgpr(destVgprPrefix + "+%u+%u"%(tmpVgprOffset, g2lIdx))) |
| 9290 |
numsOfRegister.append(1) |
| 9291 |
else: |
| 9292 |
paramList.append(vgpr(destVgprPrefix + "+%u"%(g2lIdx + eccOffset), blockWidth)) |
| 9293 |
numsOfRegister.append(blockWidth) |
| 9294 |
if self.db["ForceInputValue%s"%tc]: |
| 9295 |
localWriteCVTCode.add(VMovB32(dst=vgpr(destVgprPrefix + "+%u"%(g2lIdx)), src=self.db["ForceValue%s"], comment="ForceInputValue")) |
| 9296 |
if (kernel["ProblemType"]["DataType"].isBFloat16() and kernel["ProblemType"]["DataType%s"%tc].isHalf()) and not tP["isM"]: |
| 9297 |
numIters = 1 if blockWidth <= 1 else blockWidth |
| 9298 |
vgprTmp = self.vgprPool.checkOut(2) |
| 9299 |
for iter in range(0, numIters): |
| 9300 |
f16Tobf16Idx = g2lIdx + iter |
| 9301 |
if f16Tobf16Idx in Hcvt2BMap: |
| 9302 |
Hcvt2BMap[f16Tobf16Idx] += 2 |
| 9303 |
else: |
| 9304 |
Hcvt2BMap[f16Tobf16Idx] = 0 |
| 9305 |
f16Tobf16Idx += Hcvt2BMap[f16Tobf16Idx] |
| 9306 |
sdwa = SDWAModifiers(src0_sel=SelectBit.WORD_1) |
| 9307 |
localWriteCVTCode.add(VCvtF16toF32(dst=vgpr(vgprTmp), src=vgpr(destVgprPrefix + "+%u"%(f16Tobf16Idx)))) |
| 9308 |
localWriteCVTCode.add(VCvtF16toF32(dst=vgpr(vgprTmp+1), src=vgpr(destVgprPrefix + "+%u"%(f16Tobf16Idx)),sdwa=sdwa)) |
| 9309 |
localWriteCVTCode.add(VPackF16toB32(dst=vgpr(destVgprPrefix + "+%u"%(f16Tobf16Idx)), src0=vgpr(vgprTmp), src1=vgpr(vgprTmp+1), |
| 9310 |
vop3=VOP3PModifiers(op_sel=[1,1,0]))) |
| 9311 |
self.vgprPool.checkIn(vgprTmp) |
| 9312 |
|
| 9313 |
for oIdx in range(0, numOffsets): |
| 9314 |
paramList.append(offset) |
| 9315 |
|
| 9316 |
|
| 9317 |
|
| 9318 |
|
| 9319 |
if tP["bpeDS"] != tP["bpeGR"]: |
| 9320 |
assert numBlocks == 1 |
| 9321 |
if (kernel["ProblemType"]["DataType%s"%tc].isSingle() and kernel["ProblemType"]["DataType"].isHalf()): |
| 9322 |
newBlockWidth = (tP["bpeGR"] / tP["bpe"]) * blockWidth |
| 9323 |
if newBlockWidth == 1: |
| 9324 |
dst_sel = SelectBit.WORD_1 if isHigh16Bits else SelectBit.WORD_0 |
| 9325 |
new_src = deepcopy(paramList[0]) |
| 9326 |
if isHigh16Bits: |
| 9327 |
new_src.regName.addOffset(1) |
| 9328 |
localWriteCVTCode.add(VCvtF32toF16(dst=paramList[0], src=new_src, sdwa=SDWAModifiers(dst_sel=dst_sel), comment="convert C to fp16")) |
| 9329 |
else: |
| 9330 |
for vi in range(0, int(newBlockWidth)): |
| 9331 |
dst_sel = SelectBit.WORD_1 if vi%2==1 else SelectBit.WORD_0 |
| 9332 |
localWriteCVTCode.add(VCvtF32toF16(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi//2)), src=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi)), sdwa=SDWAModifiers(dst_sel=dst_sel), comment="convert C to fp16")) |
| 9333 |
elif (kernel["ProblemType"]["DataType%s"%tc].isHalf() and kernel["ProblemType"]["DataType"].is8bitFloat()): |
| 9334 |
|
| 9335 |
toF8 = False |
| 9336 |
if (kernel["ProblemType"]["DataType"].isAnyFloat8() or |
| 9337 |
(tc == "A" and kernel["ProblemType"]["DataType"].isAnyFloat8BFloat8()) or |
| 9338 |
(tc == "B" and kernel["ProblemType"]["DataType"].isAnyBFloat8Float8())): |
| 9339 |
toF8 = True |
| 9340 |
|
| 9341 |
newBlockWidth = (tP["bpeGR"] / tP["bpe"]) * blockWidth |
| 9342 |
if newBlockWidth == 0.5: |
| 9343 |
|
| 9344 |
|
| 9345 |
if kernel["ProblemType"]["StochasticRounding"]: |
| 9346 |
if self.states.asmCaps["v_prng_b32"]: |
| 9347 |
vgprTmp = self.vgprPool.checkOutAligned(2, 2) |
| 9348 |
else: |
| 9349 |
vgprTmp = self.vgprPool.checkOutAligned(4, 2) |
| 9350 |
else: |
| 9351 |
vgprTmp = self.vgprPool.checkOutAligned(1, 2) |
| 9352 |
src_sel = SelectBit.WORD_1 if isHigh16Bits else SelectBit.WORD_0 |
| 9353 |
sel = 1 if isHigh16Bits else 0 |
| 9354 |
localWriteCVTCode.add(VCvtF16toF32(dst=vgpr(vgprTmp), src=paramList[0], sdwa=SDWAModifiers(src0_sel=src_sel), comment="convert to F32")) |
| 9355 |
|
| 9356 |
|
| 9357 |
if kernel["ProblemType"]["UseScaleAB"] == "Scalar" and kernel["ProblemType"]["DataType%s"%tc].numRegisters() > kernel["ProblemType"]["DataType"].numRegisters(): |
| 9358 |
localWriteCVTCode.add(VMulF32(dst=vgpr(vgprTmp), src0=vgpr(vgprTmp), src1=sgpr("Scale%s"%tc), comment="Input *= scale %s"%tc)) |
| 9359 |
|
| 9360 |
if kernel["ProblemType"]["StochasticRounding"]: |
| 9361 |
vRand = vgprTmp+1 |
| 9362 |
if self.states.asmCaps["v_prng_b32"]: |
| 9363 |
localWriteCVTCode.add(VPrngB32(dst=vgpr(vRand),src=vgpr(vgprTmp),comment="Psudo Random Number Generator")) |
| 9364 |
else: |
| 9365 |
vTemp0 = vgprTmp+2 |
| 9366 |
vTemp1 = vgprTmp+3 |
| 9367 |
localWriteCVTCode.add(MacroInstruction(name="PRND_GENERATOR", args=[vRand, vgprTmp, vTemp0, vTemp1])) |
| 9368 |
|
| 9369 |
if (toF8): |
| 9370 |
localWriteCVTCode.add(VCvtSRF32toFP8(dst=paramList[0], src0=vgpr(vgprTmp), src1=vgpr(vRand), vop3=VOP3PModifiers(op_sel=[0,0,sel]), comment="Convert to FP8")) |
| 9371 |
else: |
| 9372 |
localWriteCVTCode.add(VCvtSRF32toBF8(dst=paramList[0], src0=vgpr(vgprTmp), src1=vgpr(vRand), vop3=VOP3PModifiers(op_sel=[0,0,sel]), comment="Convert to BF8")) |
| 9373 |
else: |
| 9374 |
if (toF8): |
| 9375 |
localWriteCVTCode.add(VCvtPkF32toFP8(dst=paramList[0], src0=vgpr(vgprTmp), src1=vgpr(vgprTmp), vop3=VOP3PModifiers(op_sel=[0,0,sel]), comment="Convert to FP8")) |
| 9376 |
else: |
| 9377 |
localWriteCVTCode.add(VCvtPkF32toBF8(dst=paramList[0], src0=vgpr(vgprTmp), src1=vgpr(vgprTmp), vop3=VOP3PModifiers(op_sel=[0,0,sel]), comment="Convert to BF8")) |
| 9378 |
self.vgprPool.checkIn(vgprTmp) |
| 9379 |
else: |
| 9380 |
if kernel["ProblemType"]["StochasticRounding"]: |
| 9381 |
if self.states.asmCaps["v_prng_b32"] : |
| 9382 |
vgprTmp = self.vgprPool.checkOutAligned(3, 2) |
| 9383 |
else: |
| 9384 |
vgprTmp = self.vgprPool.checkOutAligned(5, 2) |
| 9385 |
else: |
| 9386 |
vgprTmp = self.vgprPool.checkOutAligned(2, 2) |
| 9387 |
vgprTmp2 = vgprTmp + 1 |
| 9388 |
for vi in range(0, int(newBlockWidth)): |
| 9389 |
sel = 1 if vi %2 == 1 else 0 |
| 9390 |
if self.states.asmCaps["Hascvtfp8_f16"] and not kernel["ProblemType"]["StochasticRounding"] and not kernel["ProblemType"]["UseScaleAB"] == "Scalar": |
| 9391 |
if (toF8): |
| 9392 |
localWriteCVTCode.add(VCvtScalePkF16toFP8(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi//2)), src=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi)), scale=0x3f800000,\ |
| 9393 |
vop3=VOP3PModifiers(op_sel=[0,0,sel]), comment="convert F16 to F8")) |
| 9394 |
else: |
| 9395 |
localWriteCVTCode.add(VCvtScalePkF16toBF8(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi//2)), src=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi)), scale=0x3f800000,\ |
| 9396 |
vop3=VOP3PModifiers(op_sel=[0,0,sel]), comment="convert F16 to BF8")) |
| 9397 |
else: |
| 9398 |
localWriteCVTCode.add(VCvtF16toF32(dst=vgpr(vgprTmp), src=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi)), sdwa=SDWAModifiers(src0_sel=SelectBit.WORD_0), comment="convert to F32")) |
| 9399 |
localWriteCVTCode.add(VCvtF16toF32(dst=vgpr(vgprTmp2), src=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi)), sdwa=SDWAModifiers(src0_sel=SelectBit.WORD_1), comment="convert to F32")) |
| 9400 |
|
| 9401 |
if kernel["ProblemType"]["StochasticRounding"]: |
| 9402 |
|
| 9403 |
if kernel["ProblemType"]["UseScaleAB"] == "Scalar" and kernel["ProblemType"]["DataType%s"%tc].numRegisters() > kernel["ProblemType"]["DataType"].numRegisters(): |
| 9404 |
localWriteCVTCode.add(VMulPKF32S(dst=vgpr(vgprTmp, 2), src0=vgpr(vgprTmp, 2), src1=sgpr("Scale%s"%tc, 2), vop3=VOP3PModifiers(op_sel_hi=[1,0,1]), comment="Input *= scale %s"%tc)) |
| 9405 |
vRand = vgprTmp+2 |
| 9406 |
if self.states.asmCaps["v_prng_b32"]: |
| 9407 |
localWriteCVTCode.add(VPrngB32(dst=vgpr(vRand),src=vgpr(vgprTmp),comment="Psudo Random Number Generator")) |
| 9408 |
else: |
| 9409 |
vTemp0 = vgprTmp+3 |
| 9410 |
vTemp1 = vgprTmp+4 |
| 9411 |
localWriteCVTCode.add(MacroInstruction(name="PRND_GENERATOR", args=[vRand, vgprTmp, vTemp0, vTemp1])) |
| 9412 |
if (toF8): |
| 9413 |
localWriteCVTCode.add(VCvtSRF32toFP8(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi//2)), src0=vgpr(vgprTmp), src1=vgpr(vRand), vop3=VOP3PModifiers(op_sel=[0,0,0,sel]), comment="Convert to FP8")) |
| 9414 |
if self.states.asmCaps["v_prng_b32"]: |
| 9415 |
localWriteCVTCode.add(VPrngB32(dst=vgpr(vRand),src=vgpr(vgprTmp2),comment="Psudo Random Number Generator")) |
| 9416 |
else: |
| 9417 |
localWriteCVTCode.add(MacroInstruction(name="PRND_GENERATOR", args=[vRand, vgprTmp2, vTemp0, vTemp1])) |
| 9418 |
localWriteCVTCode.add(VCvtSRF32toFP8(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi//2)), src0=vgpr(vgprTmp2), src1=vgpr(vRand), vop3=VOP3PModifiers(op_sel=[0,0,1,sel]), comment="Convert to FP8")) |
| 9419 |
else: |
| 9420 |
localWriteCVTCode.add(VCvtSRF32toBF8(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi//2)), src0=vgpr(vgprTmp), src1=vgpr(vRand), vop3=VOP3PModifiers(op_sel=[0,0,0,sel]), comment="Convert to BF8")) |
| 9421 |
if self.states.asmCaps["v_prng_b32"]: |
| 9422 |
localWriteCVTCode.add(VPrngB32(dst=vgpr(vRand),src=vgpr(vgprTmp2),comment="Psudo Random Number Generator")) |
| 9423 |
else: |
| 9424 |
localWriteCVTCode.add(MacroInstruction(name="PRND_GENERATOR", args=[vRand, vgprTmp2, vTemp0, vTemp1])) |
| 9425 |
localWriteCVTCode.add(VCvtSRF32toBF8(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi//2)), src0=vgpr(vgprTmp2), src1=vgpr(vRand), vop3=VOP3PModifiers(op_sel=[0,0,1,sel]), comment="Convert to BF8")) |
| 9426 |
else: |
| 9427 |
|
| 9428 |
if kernel["ProblemType"]["UseScaleAB"] == "Scalar" and kernel["ProblemType"]["DataType%s"%tc].numRegisters() > kernel["ProblemType"]["DataType"].numRegisters(): |
| 9429 |
localWriteCVTCode.add(VMulPKF32S(dst=vgpr(vgprTmp, 2), src0=vgpr(vgprTmp, 2), src1=sgpr("Scale%s"%tc, 2), vop3=VOP3PModifiers(op_sel_hi=[1,0,1]), comment="Input *= scale %s"%tc)) |
| 9430 |
|
| 9431 |
if (toF8): |
| 9432 |
localWriteCVTCode.add(VCvtPkF32toFP8(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi//2)), src0=vgpr(vgprTmp), src1=vgpr(vgprTmp2), vop3=VOP3PModifiers(op_sel=[0,0,sel]), comment="Convert to FP8")) |
| 9433 |
else: |
| 9434 |
localWriteCVTCode.add(VCvtPkF32toBF8(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi//2)), src0=vgpr(vgprTmp), src1=vgpr(vgprTmp2), vop3=VOP3PModifiers(op_sel=[0,0,sel]), comment="Convert to BF8")) |
| 9435 |
self.vgprPool.checkIn(vgprTmp) |
| 9436 |
|
| 9437 |
elif (kernel["ProblemType"]["DataType%s"%tc].isAnyFloat8() and kernel["ProblemType"]["DataType"].isHalf()): |
| 9438 |
newBlockWidth = tP["globalReadInstruction"].blockWidth |
| 9439 |
if newBlockWidth == 0.25: |
| 9440 |
new_src = deepcopy(paramList[0]) |
| 9441 |
if tP["glvw"] == 1: |
| 9442 |
vgprTmp = self.vgprPool.checkOut(1) |
| 9443 |
new_src.regName.addOffset(tP["shiftGR"]) |
| 9444 |
src_sel = SelectBit.BYTE_2 if isHigh16Bits else SelectBit.BYTE_0 |
| 9445 |
dst_sel = SelectBit.WORD_1 if isHigh16Bits else SelectBit.WORD_0 |
| 9446 |
if self.states.asmCaps["Hascvtf16_fp8"]: |
| 9447 |
sel = [0,1,1,0] if isHigh16Bits else [0,0,0,0] |
| 9448 |
localWriteCVTCode.add(VCvtScaleFP8toF16(dst=paramList[0], src=new_src, scale=0x3f800000, vop3=VOP3PModifiers(op_sel=sel), comment="A convert fp8 to f16")) |
| 9449 |
else: |
| 9450 |
localWriteCVTCode.add(VCvtFP8toF32(dst=vgpr(vgprTmp), src=new_src , sdwa=SDWAModifiers(src0_sel=src_sel), comment="convert C to fp32")) |
| 9451 |
localWriteCVTCode.add(VCvtF32toF16(dst=paramList[0], src=vgpr(vgprTmp), sdwa=SDWAModifiers(dst_sel=dst_sel), comment="convert C to fp16")) |
| 9452 |
self.vgprPool.checkIn(vgprTmp) |
| 9453 |
else: |
| 9454 |
if isCvtHighBits and isHigh16Bits: |
| 9455 |
src_sel = SelectBit.BYTE_3 |
| 9456 |
elif isHigh16Bits and (not isCvtHighBits): |
| 9457 |
new_src.regName.addOffset(tP["shiftGR"]) |
| 9458 |
src_sel = SelectBit.BYTE_1 |
| 9459 |
elif (not isHigh16Bits) and isCvtHighBits: |
| 9460 |
src_sel = SelectBit.BYTE_2 |
| 9461 |
else: |
| 9462 |
new_src.regName.addOffset(tP["shiftGR"]) |
| 9463 |
src_sel = SelectBit.BYTE_0 |
| 9464 |
dst_sel = SelectBit.WORD_1 if isHigh16Bits else SelectBit.WORD_0 |
| 9465 |
if self.states.asmCaps["Hascvtf16_fp8"]: |
| 9466 |
if isHigh16Bits: |
| 9467 |
if isCvtHighBits: |
| 9468 |
sel = [1,1,1,0] |
| 9469 |
else: |
| 9470 |
sel = [0,1,1,0] |
| 9471 |
else: |
| 9472 |
if isCvtHighBits: |
| 9473 |
sel = [0,1,0,0] |
| 9474 |
else: |
| 9475 |
sel = [0,0,0,0] |
| 9476 |
|
| 9477 |
if new_src == paramList[0]: |
| 9478 |
if self.states.asmCaps["Hascvtf16_fp8"]: |
| 9479 |
localWriteCVTCode.add(VCvtScaleFP8toF16(dst=paramList[0], src=new_src , scale=0x3f800000, vop3=VOP3PModifiers(op_sel=sel), comment="B convert fp8 to f16")) |
| 9480 |
else: |
| 9481 |
if src_sel == SelectBit.BYTE_0 or src_sel == SelectBit.BYTE_2: |
| 9482 |
if regTmpVgprBlock == None: |
| 9483 |
regTmpVgprBlock = self.vgprPool.checkOutAligned(2, 2) |
| 9484 |
src_sel2 = SelectBit.WORD_0 if src_sel == SelectBit.BYTE_0 else SelectBit.WORD_1 |
| 9485 |
localWriteCVTCode.add(VCvtPkFP8toF32(dst=vgpr(regTmpVgprBlock, 2), src=new_src , sdwa=SDWAModifiers(src0_sel=src_sel2), comment="convert C to fp32")) |
| 9486 |
localWriteCVTCode.add(VCvtF32toF16(dst=paramList[0], src=vgpr(regTmpVgprBlock), sdwa=SDWAModifiers(dst_sel=dst_sel), comment="convert C to fp16")) |
| 9487 |
else: |
| 9488 |
localWriteCVTCode.add(VCvtF32toF16(dst=paramList[0], src=vgpr(regTmpVgprBlock + 1), sdwa=SDWAModifiers(dst_sel=dst_sel), comment="convert C to fp16")) |
| 9489 |
else: |
| 9490 |
vgprTmp = self.vgprPool.checkOut(1) |
| 9491 |
if self.states.asmCaps["Hascvtf16_fp8"]: |
| 9492 |
localWriteCVTCode.add(VCvtScaleFP8toF16(dst=paramList[0], src=new_src, scale=0x3f800000, vop3=VOP3PModifiers(op_sel=sel), comment="C convert fp8 to f16")) |
| 9493 |
else: |
| 9494 |
localWriteCVTCode.add(VCvtFP8toF32(dst=vgpr(vgprTmp), src=new_src , sdwa=SDWAModifiers(src0_sel=src_sel), comment="convert C to fp32")) |
| 9495 |
localWriteCVTCode.add(VCvtF32toF16(dst=paramList[0], src=vgpr(vgprTmp), sdwa=SDWAModifiers(dst_sel=dst_sel), comment="convert C to fp16")) |
| 9496 |
self.vgprPool.checkIn(vgprTmp) |
| 9497 |
elif newBlockWidth == 0.5: |
| 9498 |
vgprTmp = self.vgprPool.checkOutAligned(2, 2) |
| 9499 |
src_sel = SelectBit.WORD_1 if isCvtHighBits else SelectBit.WORD_0 |
| 9500 |
modNum = max(1, int(newBlockWidth / blockWidth)) |
| 9501 |
if self.states.asmCaps["Hascvtf16_fp8"]: |
| 9502 |
sel = [1,0,0,0] if isCvtHighBits else [0,0,0,0] |
| 9503 |
localWriteCVTCode.add(VCvtScalePkFP8toF16(dst=vgpr(destVgprPrefix + "+%u"%(g2lIdx)), src=vgpr(destVgprPrefix + "+%u"%(g2lIdx)), scale=0x3f800000,\ |
| 9504 |
vop3=VOP3PModifiers(op_sel=sel), comment="D convert fp8 to f16")) |
| 9505 |
else: |
| 9506 |
if (not isHigh16Bits) and (g2lIdx % modNum == 0): |
| 9507 |
localWriteCVTCode.add(VCvtPkFP8toF32(dst=vgpr(vgprTmp, 2), src=vgpr(destVgprPrefix + "+%u"%(g2lIdx)), sdwa=SDWAModifiers(src0_sel=src_sel), comment="convert to F32")) |
| 9508 |
localWriteCVTCode.add(VCvtF32toF16(dst=vgpr(destVgprPrefix + "+%u"%(g2lIdx)), src=vgpr(vgprTmp), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_0), comment="Convert to FP16")) |
| 9509 |
if (newBlockWidth <= blockWidth): |
| 9510 |
localWriteCVTCode.add(VCvtF32toF16(dst=vgpr(destVgprPrefix + "+%u"%(g2lIdx)), src=vgpr(vgprTmp+1), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_1), comment="Convert to FP16")) |
| 9511 |
elif (newBlockWidth > blockWidth): |
| 9512 |
localWriteCVTCode.add(VCvtF32toF16(dst=vgpr(destVgprPrefix + "+%u"%(g2lIdx)), src=vgpr(vgprTmp+1), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_1), comment="Convert to FP16")) |
| 9513 |
self.vgprPool.checkIn(vgprTmp) |
| 9514 |
else: |
| 9515 |
modNum = max(1, int(newBlockWidth / blockWidth)) |
| 9516 |
vgprTmp = self.vgprPool.checkOutAligned(2, 2) |
| 9517 |
if (not isHigh16Bits) and (newBlockWidth <= blockWidth): |
| 9518 |
for vi in range(0, int(newBlockWidth)): |
| 9519 |
if self.states.asmCaps["Hascvtf16_fp8"]: |
| 9520 |
localWriteCVTCode.add(VCvtScaleFP8toF16(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi * 2)), src=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx+tP["shiftGR"], vi)), scale=0x3f800000,\ |
| 9521 |
vop3=VOP3PModifiers(op_sel=[0,0,0,0]), comment="E convert fp8 to f16")) |
| 9522 |
localWriteCVTCode.add(VCvtScaleFP8toF16(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi * 2)), src=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx+tP["shiftGR"], vi)), scale=0x3f800000,\ |
| 9523 |
vop3=VOP3PModifiers(op_sel=[1,0,1,0]), comment="E convert fp8 to f16")) |
| 9524 |
localWriteCVTCode.add(VCvtScaleFP8toF16(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi * 2 + 1)), src=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx+tP["shiftGR"], vi)), scale=0x3f800000,\ |
| 9525 |
vop3=VOP3PModifiers(op_sel=[0,1,0,0]), comment="E convert fp8 to f16")) |
| 9526 |
localWriteCVTCode.add(VCvtScaleFP8toF16(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi * 2 + 1)), src=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx+tP["shiftGR"], vi)), scale=0x3f800000,\ |
| 9527 |
vop3=VOP3PModifiers(op_sel=[1,1,1,0]), comment="E convert fp8 to f16")) |
| 9528 |
else: |
| 9529 |
localWriteCVTCode.add(VCvtPkFP8toF32(dst=vgpr(vgprTmp, 2), src=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx+tP["shiftGR"], vi)), sdwa=SDWAModifiers(src0_sel=SelectBit.WORD_0), comment="convert to F32")) |
| 9530 |
localWriteCVTCode.add(VCvtF32toF16(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi * 2)), src=vgpr(vgprTmp), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_0), comment="Convert to FP16")) |
| 9531 |
localWriteCVTCode.add(VCvtF32toF16(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi * 2)), src=vgpr(vgprTmp+1), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_1), comment="Convert to FP16")) |
| 9532 |
localWriteCVTCode.add(VCvtPkFP8toF32(dst=vgpr(vgprTmp, 2), src=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx+tP["shiftGR"], vi)), sdwa=SDWAModifiers(src0_sel=SelectBit.WORD_1), comment="convert to F32")) |
| 9533 |
localWriteCVTCode.add(VCvtF32toF16(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi * 2 + 1)), src=vgpr(vgprTmp), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_0), comment="Convert to FP16")) |
| 9534 |
localWriteCVTCode.add(VCvtF32toF16(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi * 2 + 1)), src=vgpr(vgprTmp+1), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_1), comment="Convert to FP16")) |
| 9535 |
else: |
| 9536 |
if (not isHigh16Bits): |
| 9537 |
idxMod = g2lIdx % modNum |
| 9538 |
g2lIdxTmp = g2lIdx - idxMod |
| 9539 |
vi = idxMod // 2 |
| 9540 |
selectBit = SelectBit.WORD_0 if idxMod % 2 == 0 else SelectBit.WORD_1 |
| 9541 |
interOffset = 0 if idxMod % 2 == 0 else 1 |
| 9542 |
if self.states.asmCaps["Hascvtf16_fp8"]: |
| 9543 |
sel = 1 if idxMod % 2 == 0 else 1 |
| 9544 |
localWriteCVTCode.add(VCvtScalePkFP8toF16(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdxTmp, vi * 2 + interOffset)),\ |
| 9545 |
src=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdxTmp+tP["shiftGR"], vi)), scale=0x3f800000,\ |
| 9546 |
vop3=VOP3PModifiers(op_sel=[0,0,0,sel]), comment="F convert fp8 to f16")) |
| 9547 |
else: |
| 9548 |
localWriteCVTCode.add(VCvtPkFP8toF32(dst=vgpr(vgprTmp, 2), src=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdxTmp+tP["shiftGR"], vi)), sdwa=SDWAModifiers(src0_sel=selectBit), comment="convert to F32")) |
| 9549 |
localWriteCVTCode.add(VCvtF32toF16(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdxTmp, vi * 2 + interOffset)), src=vgpr(vgprTmp), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_0), comment="Convert to FP16")) |
| 9550 |
if blockWidth != 0.5: |
| 9551 |
localWriteCVTCode.add(VCvtF32toF16(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdxTmp, vi * 2 + interOffset)), src=vgpr(vgprTmp+1), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_1), comment="Convert to FP16")) |
| 9552 |
elif blockWidth == 0.5: |
| 9553 |
localWriteCVTCode.add(VCvtF32toF16(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdxTmp, vi * 2 + interOffset)), src=vgpr(vgprTmp+1), sdwa=SDWAModifiers(dst_sel=SelectBit.WORD_1), comment="Convert to FP16")) |
| 9554 |
self.vgprPool.checkIn(vgprTmp) |
| 9555 |
elif (kernel["ProblemType"]["DataType%s"%tc].isSingle() and kernel["ProblemType"]["DataType"].isBFloat16()): |
| 9556 |
newBlockWidth = (tP["bpeGR"] / tP["bpe"]) * blockWidth |
| 9557 |
if tP["glvw"] == 1: |
| 9558 |
|
| 9559 |
localWriteCVTCode.add(VCvtPkF32toBF16(dst=paramList[0], src0=paramList[0], src1=paramList[0], comment="convert one fp32 to bf16 to the same VGPR")) |
| 9560 |
elif newBlockWidth == 1: |
| 9561 |
new_src = deepcopy(paramList[0]) |
| 9562 |
if isHigh16Bits: |
| 9563 |
new_src.regName.addOffset(1) |
| 9564 |
localWriteCVTCode.add(VCvtPkF32toBF16(dst=paramList[0], src0=new_src, src1=new_src, comment="convert one fp32 to bf16 idx=%d"%g2lIdx)) |
| 9565 |
else: |
| 9566 |
for vi in range(0, int(newBlockWidth)): |
| 9567 |
if vi%2 == 0: |
| 9568 |
localWriteCVTCode.add(VCvtPkF32toBF16(dst=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi//2)), src0=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi)), src1=vgpr(destVgprPrefix + "+%u+%u"%(g2lIdx, vi+1)), comment="convert/pack fp32 to bf16")) |
| 9569 |
else: |
| 9570 |
printExit("Unsupported combination DataType%s (%s) -> DataType (%s)"%(tc, kernel["ProblemType"]["DataType%s"%tc].toChar(), kernel["ProblemType"]["DataType"].toChar())) |
| 9571 |
|
| 9572 |
LocalWriteX = tP["localWriteInstruction"].getInst(isHigh16Bits) |
| 9573 |
if numBlocks == 1: |
| 9574 |
if (paramList[1] >= 0x20000): |
| 9575 |
olwa = "LocalWriteAddr%s+2"%tc |
| 9576 |
dstAddr=vgpr(olwa) |
| 9577 |
paramList[1] = paramList[1] - 131072 |
| 9578 |
elif (paramList[1] >= 0x10000): |
| 9579 |
olwa = "LocalWriteAddr%s+1"%tc |
| 9580 |
dstAddr=vgpr(olwa) |
| 9581 |
paramList[1] = paramList[1] - 65536 |
| 9582 |
else: |
| 9583 |
dstAddr=vgpr(lwa) |
| 9584 |
ds = DSModifiers(na=1, offset=paramList[1]) |
| 9585 |
writeInst = LocalWriteX(dstAddr=dstAddr, src=paramList[0], ds=ds, comment=comment) |
| 9586 |
else: |
| 9587 |
ds = DSModifiers(na=2, offset0=paramList[2], offset1=paramList[3]) |
| 9588 |
writeInst = LocalWriteX(dstAddr=vgpr(lwa), src0=paramList[0], src1=paramList[1], ds=ds, comment=comment) |
| 9589 |
if self.do["LocalWriteCVT"]: |
| 9590 |
localWriteCode.add(localWriteCVTCode) |
| 9591 |
if self.do["LocalWrite%s"%tc]: |
| 9592 |
localWriteCode.add(writeInst) |
| 9593 |
instructionCnt += 1 if blockWidth < 8 else 2 |
| 9594 |
|
| 9595 |
if regTmpVgprBlock != None: |
| 9596 |
self.vgprPool.checkIn(regTmpVgprBlock) |
| 9597 |
|
| 9598 |
if tmpLocalWriteAddr != -1: |
| 9599 |
self.vgprPool.checkIn(tmpLocalWriteAddr) |
| 9600 |
|
| 9601 |
|
| 9602 |
|
| 9603 |
|
| 9604 |
if kernel["ProblemType"]["Sparse"] and kernel["DirectToVgprSparseMetadata"]: |
| 9605 |
miWaveTile = kernel["MIWaveTileB"] if (kernel["ProblemType"]["Sparse"] == 2 and tP["isB"]) else kernel["MIWaveTileA"] if (kernel["ProblemType"]["Sparse"] == 1 and tP["isA"]) else 0 |
| 9606 |
if kernel["PrefetchGlobalRead"] == 2 and miWaveTile > 0: |
| 9607 |
|
| 9608 |
offsetBlk = tP["metadataWriteSwapByteOffset"] |
| 9609 |
instructionCnt = -1 |
| 9610 |
for wtIdx in range(0, miWaveTile): |
| 9611 |
for unrollIdx in range(0, kernel["LoopIters"]): |
| 9612 |
instructionCnt +=1 |
| 9613 |
localWriteCode = imod.add(Module("MetadataWrite%u "%(instructionCnt))) |
| 9614 |
localWriteCode.add(VMovB32( \ |
| 9615 |
vgpr("ValuMetadata+%u+%u"%(offsetBlk, (wtIdx * kernel["LoopIters"]+unrollIdx))), \ |
| 9616 |
vgpr("ValuMetadata+%u+%u"%(self.states.m.numVgprValuPerBlock * 2, (wtIdx*kernel["LoopIters"]+unrollIdx))), \ |
| 9617 |
comment="copy ValuMetadata from blk2")) |
| 9618 |
|
| 9619 |
if 0 and tP["isB"]: |
| 9620 |
localWriteCode.add(SWaitCnt(dscnt=0, vlcnt=0, vscnt=0, comment="")) |
| 9621 |
localWriteCode.add(SBarrier(comment="dump LDS")) |
| 9622 |
localWriteCode.add(self.getCmpAssert(self.asmAssert.ne, sgpr("WorkGroup0"),1)) |
| 9623 |
|
| 9624 |
|
| 9625 |
if (not kernel["DirectToLds%s"%tc]) or \ |
| 9626 |
(((tP["isA"] and kernel["NonDTLTailLoopA"]) or \ |
| 9627 |
(tP["isB"] and kernel["NonDTLTailLoopB"])) and self.states.inTailLoop): |
| 9628 |
|
| 9629 |
if not ((tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc]): |
| 9630 |
localWriteBody(tP) |
| 9631 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 9632 |
if tP["is_sparse"]: |
| 9633 |
localWriteBody(tP["tpsMetadata"]) |
| 9634 |
|
| 9635 |
return imod |
| 9636 |
|
| 9637 |
|
| 9638 |
|
| 9639 |
|
| 9640 |
|
| 9641 |
|
| 9642 |
def localReadSwapOffsets(self, kernel, internalPointerSwap, tP): |
| 9643 |
tc=tP["tensorChar"] |
| 9644 |
if (not self.do["LocalRead%s"%tc]): |
| 9645 |
return Module("localReadSwapOffsets (no local read)") |
| 9646 |
if kernel["1LDSBuffer"] or ((tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc]): |
| 9647 |
return Module("localReadSwapOffsets (Empty)") |
| 9648 |
module = Module("localReadSwapOffsets") |
| 9649 |
|
| 9650 |
if internalPointerSwap or kernel["StoreSwapAddr"]: |
| 9651 |
if not kernel["StoreSwapAddr"]: |
| 9652 |
tP["localReadSwapByteOffset"] = 0 if tP["localReadSwapByteOffset"] else kernel["LdsOffsetA_Blk"] |
| 9653 |
module.addComment1("local read swap internal offset -> %u" % tP["localReadSwapByteOffset"]) |
| 9654 |
else: |
| 9655 |
module.add(VXorB32( |
| 9656 |
dst=vgpr("LocalReadAddr%s"%tc), \ |
| 9657 |
src0=vgpr("LocalReadSwapAddr%s"%tc), \ |
| 9658 |
src1=vgpr("LocalReadAddr%s"%tc), \ |
| 9659 |
comment="swap Red Blk")) |
| 9660 |
else: |
| 9661 |
module.add(VXorB32( |
| 9662 |
dst=vgpr("LocalReadAddr%s"%tc), \ |
| 9663 |
src0=hex(kernel["LdsOffsetA_Blk"]), \ |
| 9664 |
src1=vgpr("LocalReadAddr%s"%tc), \ |
| 9665 |
comment="swap Red Blk")) |
| 9666 |
|
| 9667 |
numLra = 0 |
| 9668 |
if tP["isA"]: |
| 9669 |
numLra = self.states.a.numVgprLocalReadAddr |
| 9670 |
elif tP["isB"]: |
| 9671 |
numLra = self.states.b.numVgprLocalReadAddr |
| 9672 |
elif tP["isM"]: |
| 9673 |
numLra = self.states.m.numVgprLocalReadAddr |
| 9674 |
|
| 9675 |
for i in range(1,numLra): |
| 9676 |
module.add(VAddU32( |
| 9677 |
dst=vgpr("LocalReadAddr%s+%u"%(tc,i)), \ |
| 9678 |
src0=(i * self.states.regCaps["maxLDSConstOffset"]), \ |
| 9679 |
src1=vgpr("LocalReadAddr%s"%tc), \ |
| 9680 |
comment="Final Offset Plus %uK"%((i * self.states.regCaps["maxLDSConstOffset"]) / 1024))) |
| 9681 |
|
| 9682 |
return module |
| 9683 |
|
| 9684 |
|
| 9685 |
|
| 9686 |
|
| 9687 |
|
| 9688 |
|
| 9689 |
|
| 9690 |
def localReadResetOffsets(self, kernel, tP): |
| 9691 |
tc=tP["tensorChar"] |
| 9692 |
if not self.do["LocalRead%s"%tc]: return Module("localReadResetOffsets (no local read)") |
| 9693 |
if kernel["1LDSBuffer"] or ((tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc]): |
| 9694 |
return Module("localReadResetOffsets (Empty)") |
| 9695 |
module = Module("localReadResetOffsets") |
| 9696 |
if tP["localReadInstruction"].numOffsets == 1: |
| 9697 |
tP["localReadSwapByteOffset"] = 0 |
| 9698 |
module.addComment1("localReadResetOffsets") |
| 9699 |
tP["localReadOffset"] = 0 |
| 9700 |
module.addComment0("handled internally") |
| 9701 |
|
| 9702 |
if kernel["StoreSwapAddr"]: |
| 9703 |
|
| 9704 |
tmpvgpr = self.vgprPool.checkOut(1) |
| 9705 |
module.add(VXorB32( |
| 9706 |
dst=vgpr(tmpvgpr), \ |
| 9707 |
src0=vgpr("LocalReadSwapAddr%s"%tc), \ |
| 9708 |
src1=vgpr("LocalReadAddr%s"%tc), \ |
| 9709 |
comment="Get other lds buffer offset value")) |
| 9710 |
module.add(VMinI32(dst=vgpr("LocalReadAddr%s"%tc), src0=vgpr("LocalReadAddr%s"%tc), src1=vgpr(tmpvgpr), |
| 9711 |
comment="Set LRA to first buffer offset")) |
| 9712 |
self.vgprPool.checkIn(tmpvgpr) |
| 9713 |
else: |
| 9714 |
|
| 9715 |
module.add(VAndB32( |
| 9716 |
dst=vgpr("LocalReadAddr%s+%u"%(tP["tensorChar"], 0)), \ |
| 9717 |
src0=hex(kernel["LdsOffsetA_Blk"]-1), \ |
| 9718 |
src1=vgpr("LocalReadAddr%s+%u"%(tP["tensorChar"], 0)), \ |
| 9719 |
comment="reset Red,Blk -> Red")) |
| 9720 |
|
| 9721 |
numLra = 0 |
| 9722 |
if tP["isA"]: |
| 9723 |
numLra = self.states.a.numVgprLocalReadAddr |
| 9724 |
elif tP["isB"]: |
| 9725 |
numLra = self.states.b.numVgprLocalReadAddr |
| 9726 |
elif tP["isM"]: |
| 9727 |
numLra = self.states.m.numVgprLocalReadAddr |
| 9728 |
|
| 9729 |
for i in range(1,numLra): |
| 9730 |
module.add(VAddU32( |
| 9731 |
dst=vgpr("LocalReadAddr%s+%u"%(tc,i)), \ |
| 9732 |
src0=(i * self.states.regCaps["maxLDSConstOffset"]), \ |
| 9733 |
src1=vgpr("LocalReadAddr%s"%tc), \ |
| 9734 |
comment="Final Offset Plus %uK"%((i * self.states.regCaps["maxLDSConstOffset"]) / 1024))) |
| 9735 |
|
| 9736 |
return module |
| 9737 |
|
| 9738 |
|
| 9739 |
|
| 9740 |
|
| 9741 |
def localReadInitPointers(self, kernel, tPA, tP): |
| 9742 |
tc=tP["tensorChar"] |
| 9743 |
if (not self.do["LocalRead%s"%tc]) or ((tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc]): |
| 9744 |
return Module("localReadInitPointers (Empty)") |
| 9745 |
module = Module("localReadInitPointers") |
| 9746 |
if tPA["localReadInstruction"].numOffsets == 1: |
| 9747 |
module.addComment1("localReadInitPointers") |
| 9748 |
tP["localReadOffset"] = 0 |
| 9749 |
else: |
| 9750 |
numLra = 0 |
| 9751 |
if tP["isA"]: |
| 9752 |
numLra = self.states.a.numVgprLocalReadAddr |
| 9753 |
elif tP["isB"]: |
| 9754 |
numLra = self.states.b.numVgprLocalReadAddr |
| 9755 |
elif tP["isM"]: |
| 9756 |
numLra = self.states.m.numVgprLocalReadAddr |
| 9757 |
module.add(VAndB32( |
| 9758 |
dst=vgpr("LocalReadAddr%s+%u"%(tP["tensorChar"], 0)), \ |
| 9759 |
src0=hex(kernel["LdsOffset%s_Blk"%tP["tensorChar"]]-1), \ |
| 9760 |
src1=vgpr("LocalReadAddr%s+%u"%(tP["tensorChar"], 0)), \ |
| 9761 |
comment="init Red,Blk -> Red")) |
| 9762 |
for i in range(1, numLra): |
| 9763 |
module.add(VAddU32( |
| 9764 |
dst=vgpr("LocalReadAddr%s+%u"%(tc,i)), \ |
| 9765 |
src0=(i * self.states.regCaps["maxLDSConstOffset"]), \ |
| 9766 |
src1=vgpr("LocalReadAddr%s"%tc), \ |
| 9767 |
comment="Final Offset Plus %uK"%((i * self.states.regCaps["maxLDSConstOffset"]) / 1024))) |
| 9768 |
return module |
| 9769 |
|
| 9770 |
|
| 9771 |
|
| 9772 |
|
| 9773 |
def localReadInc(self, kernel, iui, tP): |
| 9774 |
tc = tP["tensorChar"] |
| 9775 |
if (not self.do["LocalRead%s" % tc]) or ((tP["isA"] or tP["isB"]) and kernel["DirectToVgpr%s"%tc]): |
| 9776 |
return Module("localReadInc (Empty)") |
| 9777 |
|
| 9778 |
module = Module("localReadInc") |
| 9779 |
|
| 9780 |
offsetInc = 0 |
| 9781 |
LdsPad = kernel["LdsPad%s"%tc] if kernel["LdsBlockSizePerPad%s"%tc] == 0 else 0 |
| 9782 |
|
| 9783 |
if self.states.inTailLoop: |
| 9784 |
if kernel["UseDotInstruction"]: |
| 9785 |
|
| 9786 |
inc = self.states.lrvwUnrollA * kernel["NumWaveSplitK"] * tP["bpeDS"] |
| 9787 |
comment = "(LocalReadVectorWidth*NumWaveSplitK*bpeDS)" |
| 9788 |
else: |
| 9789 |
inc = (kernel["MacroTile%s" % tP["tensorChar"]] + LdsPad) * tP["bpeDS"] |
| 9790 |
comment = " ((MT+PAD)*bpeDS)" |
| 9791 |
if kernel["EnableMatrixInstruction"]: |
| 9792 |
matrixInstK = kernel["MatrixInstK"] |
| 9793 |
if kernel["UnrollMajorLDS%s" % tc]: |
| 9794 |
inc = tP["bpeDS"] * max(self.states.numReadsIterCoalescedA,self.states.numReadsIterCoalescedB) |
| 9795 |
comment = " (bpeDS)" |
| 9796 |
inc *= matrixInstK |
| 9797 |
if kernel["ProblemType"]["Sparse"]: |
| 9798 |
if (kernel["ProblemType"]["Sparse"] == 2 and tc == "B") or (kernel["ProblemType"]["Sparse"] == 1 and tc == "A"): |
| 9799 |
inc //= 2 |
| 9800 |
elif tc == "Metadata": |
| 9801 |
inc //= 8 |
| 9802 |
|
| 9803 |
if (kernel["LdsBlockSizePerPad%s"%tc] != 0) and (kernel["LdsPad%s"%tc] != 0): |
| 9804 |
inc = inc + (inc // kernel["LdsBlockSizePerPad%s"%tc]) * kernel["LdsPad%s"%tc] * tP["bpeDS"] |
| 9805 |
|
| 9806 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 9807 |
tmpSgpr = tmpSgprInfo.idx |
| 9808 |
module.add(SMovB32(dst=sgpr(tmpSgpr), src=hex(inc), comment="inc")) |
| 9809 |
numLra = 0 |
| 9810 |
if tP["isA"]: |
| 9811 |
numLra = self.states.a.numVgprLocalReadAddr |
| 9812 |
elif tP["isB"]: |
| 9813 |
numLra = self.states.b.numVgprLocalReadAddr |
| 9814 |
elif tP["isM"]: |
| 9815 |
numLra = self.states.m.numVgprLocalReadAddr |
| 9816 |
for i in range(numLra): |
| 9817 |
module.add(VAddCOU32( |
| 9818 |
dst=vgpr("LocalReadAddr%s+%u"%(tP["tensorChar"], i)), \ |
| 9819 |
dst1=VCC(), \ |
| 9820 |
src0=sgpr(tmpSgpr), \ |
| 9821 |
src1=vgpr("LocalReadAddr%s+%u"%(tP["tensorChar"], i)), \ |
| 9822 |
comment="lr%s += %u%s"%(tP["tensorChar"], inc, comment) )) |
| 9823 |
else: |
| 9824 |
if tP["localReadInstruction"].numOffsets == 1: |
| 9825 |
if kernel["EnableMatrixInstruction"]: |
| 9826 |
if kernel["UnrollMajorLDS%s" % tP["tensorChar"]]: |
| 9827 |
offsetInc = kernel["MatrixInstK"] * max(self.states.numReadsIterCoalescedA,self.states.numReadsIterCoalescedB) |
| 9828 |
if kernel["ProblemType"]["Sparse"]: |
| 9829 |
if (kernel["ProblemType"]["Sparse"] == 2 and tc == "B") or (kernel["ProblemType"]["Sparse"] == 1 and tc == "A"): |
| 9830 |
offsetInc //= 2 |
| 9831 |
elif tc == "Metadata": |
| 9832 |
offsetInc //= 8 |
| 9833 |
else: |
| 9834 |
if tc == "A": |
| 9835 |
sparseA = kernel["ProblemType"]["Sparse"] == 1 |
| 9836 |
lrvw = kernel["LocalReadVectorWidth"] // (2 if sparseA else 1) |
| 9837 |
wlr = lrvw//kernel["MIInputPerThreadA"] |
| 9838 |
wlr = 1 if wlr == 0 else wlr |
| 9839 |
if kernel["ProblemType"]["Sparse"] and lrvw < kernel["MIInputPerThreadA"]: |
| 9840 |
if not sparseA: |
| 9841 |
offsetInc = (kernel["MacroTile%s"%tP["tensorChar"]] + LdsPad) * (kernel["MatrixInstK"]) |
| 9842 |
else: |
| 9843 |
offsetInc = (kernel["MacroTile%s"%tP["tensorChar"]] + LdsPad) * (kernel["MatrixInstK"]*lrvw//kernel["MIInputPerThreadA"]) |
| 9844 |
elif (self.states.localReadDoCntA)%(wlr): |
| 9845 |
offsetInc = (kernel["MacroTile%s"%tP["tensorChar"]] + LdsPad) * kernel["MIInputPerThreadA"] |
| 9846 |
else: |
| 9847 |
isTF32EmuGfx950 = kernel["UseF32XEmulation"] and (kernel["MatrixInstK"] >= 32 or (kernel["MatrixInstM"] == 32 and kernel["MatrixInstK"] == 16)) |
| 9848 |
if kernel["UnrollMajorLDSA"] == False and kernel["MatrixInstK"] >= 64 or isTF32EmuGfx950: |
| 9849 |
offsetInc = (kernel["MacroTile%s"%tP["tensorChar"]] + LdsPad) * (kernel["MatrixInstK"]) |
| 9850 |
else: |
| 9851 |
offsetInc = (kernel["MacroTile%s"%tP["tensorChar"]] + LdsPad) * (kernel["MatrixInstK"]*lrvw//kernel["MIInputPerThreadA"]-kernel["MIInputPerThreadA"]*(lrvw//kernel["MIInputPerThreadA"]-1)) |
| 9852 |
if sparseA: |
| 9853 |
offsetInc //= 2 |
| 9854 |
elif tc == "Metadata": |
| 9855 |
lrvw = kernel["LocalReadVectorWidth"] // 8 |
| 9856 |
if lrvw < kernel["MIInputPerThreadMetadata"]: |
| 9857 |
offsetInc = (kernel["MacroTile%s"%tP["tensorChar"]] + LdsPad) * (kernel["MatrixInstK"]*lrvw//kernel["MIInputPerThreadMetadata"]) // 4 |
| 9858 |
elif (self.states.localReadDoCntMetadata)%(lrvw//kernel["MIInputPerThreadMetadata"]): |
| 9859 |
offsetInc = (kernel["MacroTile%s"%tP["tensorChar"]] + LdsPad) * kernel["MIInputPerThreadMetadata"] |
| 9860 |
else: |
| 9861 |
offsetInc = (kernel["MacroTile%s"%tP["tensorChar"]] + LdsPad) * (kernel["MatrixInstK"]*lrvw//kernel["MIInputPerThreadMetadata"]-kernel["MIInputPerThreadMetadata"]*(lrvw//kernel["MIInputPerThreadMetadata"]-1)) |
| 9862 |
offsetInc //= 8 |
| 9863 |
else: |
| 9864 |
sparseB = kernel["ProblemType"]["Sparse"] == 2 |
| 9865 |
lrvw = kernel["LocalReadVectorWidth"] // (2 if sparseB else 1) |
| 9866 |
wlr = lrvw//kernel["MIInputPerThreadB"] |
| 9867 |
wlr = 1 if wlr == 0 else wlr |
| 9868 |
|
| 9869 |
if kernel["ProblemType"]["Sparse"] and lrvw < kernel["MIInputPerThreadB"]: |
| 9870 |
if not sparseB: |
| 9871 |
offsetInc = (kernel["MacroTile%s"%tP["tensorChar"]] + LdsPad) * (kernel["MatrixInstK"]) |
| 9872 |
else: |
| 9873 |
offsetInc = (kernel["MacroTile%s"%tP["tensorChar"]] + LdsPad) * (kernel["MatrixInstK"]*lrvw//kernel["MIInputPerThreadB"]) |
| 9874 |
elif (self.states.localReadDoCntB)%(wlr): |
| 9875 |
offsetInc = (kernel["MacroTile%s"%tP["tensorChar"]] + LdsPad) * kernel["MIInputPerThreadB"] |
| 9876 |
|
| 9877 |
else: |
| 9878 |
isTF32EmuGfx950 = kernel["UseF32XEmulation"] and (kernel["MatrixInstK"] >= 32 or (kernel["MatrixInstM"] == 32 and kernel["MatrixInstK"] == 16)) |
| 9879 |
if kernel["UnrollMajorLDSB"] == False and kernel["MatrixInstK"] >= 64 or isTF32EmuGfx950: |
| 9880 |
offsetInc = (kernel["MacroTile%s"%tP["tensorChar"]] + LdsPad) * (kernel["MatrixInstK"]) |
| 9881 |
else: |
| 9882 |
offsetInc = (kernel["MacroTile%s"%tP["tensorChar"]] + LdsPad) * (kernel["MatrixInstK"]*lrvw//kernel["MIInputPerThreadB"]-kernel["MIInputPerThreadB"]*(lrvw//kernel["MIInputPerThreadB"]-1)) |
| 9883 |
|
| 9884 |
if sparseB: |
| 9885 |
offsetInc //= 2 |
| 9886 |
else: |
| 9887 |
|
| 9888 |
offsetInc = self.states.lrvwUnrollA * kernel["NumWaveSplitK"] if kernel["UseDotInstruction"] else (kernel["MacroTile%s"%tP["tensorChar"]] + LdsPad) |
| 9889 |
tP["localReadOffset"] += offsetInc |
| 9890 |
module.addComment0("N/A, lro->%d" % tP["localReadOffset"]) |
| 9891 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 9892 |
module.addComment0("self.localReadDoCntA %d self.localReadDoCntB %d self.localReadDoCntMetadata %d" % (self.states.localReadDoCntA,self.states.localReadDoCntB, self.states.localReadDoCntMetadata)) |
| 9893 |
else: |
| 9894 |
module.addComment0("self.localReadDoCntA %d self.localReadDoCntB %d" % (self.states.localReadDoCntA,self.states.localReadDoCntB)) |
| 9895 |
else: |
| 9896 |
inc = (kernel["MacroTile%s" % tP["tensorChar"]] + LdsPad) |
| 9897 |
numLra = 0 |
| 9898 |
if tP["isA"]: |
| 9899 |
numLra = self.states.a.numVgprLocalReadAddr |
| 9900 |
elif tP["isB"]: |
| 9901 |
numLra = self.states.b.numVgprLocalReadAddr |
| 9902 |
elif tP["isM"]: |
| 9903 |
numLra = self.states.m.numVgprLocalReadAddr |
| 9904 |
for i in range(numLra): |
| 9905 |
module.add(VAddCOU32( |
| 9906 |
dst=vgpr("LocalReadAddr%s+%u"%(tP["tensorChar"], i)), \ |
| 9907 |
dst1=VCC(), \ |
| 9908 |
src0=hex(inc), \ |
| 9909 |
src1=vgpr("LocalReadAddr%s+%u"%(tP["tensorChar"], i)), \ |
| 9910 |
comment="lr%s += %u ((MT+Pad)*bpe)"%(tP["tensorChar"], inc) )) |
| 9911 |
|
| 9912 |
return module |
| 9913 |
|
| 9914 |
|
| 9915 |
|
| 9916 |
|
| 9917 |
|
| 9918 |
|
| 9919 |
|
| 9920 |
def localReadDo(self, kernel, bufferIdx, iui, epsi, tP): |
| 9921 |
|
| 9922 |
if not self.do["LocalRead%s" % tP["tensorChar"]]: |
| 9923 |
imod = Module("LocalReadDo%s_I%s" % (tP["tensorChar"], iui)) |
| 9924 |
pack = Module("pack%s_I%s" % (tP["tensorChar"], iui)) |
| 9925 |
return imod, pack |
| 9926 |
|
| 9927 |
component = Component.LocalRead.find(self) |
| 9928 |
if component: |
| 9929 |
return component(self, kernel, bufferIdx, iui, epsi, tP) |
| 9930 |
|
| 9931 |
|
| 9932 |
|
| 9933 |
|
| 9934 |
|
| 9935 |
def saveLocalPointers(self, kernel, tPA, tPB): |
| 9936 |
tPA["savedLocalReadOffset"] = tPA["localReadOffset"] |
| 9937 |
tPB["savedLocalReadOffset"] = tPB["localReadOffset"] |
| 9938 |
tPM = tPA["tpsMetadata"] if tPA["is_sparse"] else tPB["tpsMetadata"] |
| 9939 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 9940 |
tPM["savedLocalReadOffset"] = tPM["localReadOffset"] |
| 9941 |
self.states.savedLocalReadDoCntA = self.states.localReadDoCntA |
| 9942 |
self.states.savedLocalReadDoCntB = self.states.localReadDoCntB |
| 9943 |
self.states.savedLocalReadDoCntMetadata = self.states.localReadDoCntMetadata |
| 9944 |
if kernel["ExpandPointerSwap"]: |
| 9945 |
tPA["savedLocalWriteSwapByteOffset"] = tPA["localWriteSwapByteOffset"] |
| 9946 |
tPB["savedLocalWriteSwapByteOffset"] = tPB["localWriteSwapByteOffset"] |
| 9947 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 9948 |
tPM["savedLocalWriteSwapByteOffset"] = tPM["localWriteSwapByteOffset"] |
| 9949 |
|
| 9950 |
|
| 9951 |
|
| 9952 |
|
| 9953 |
def restoreLocalPointers(self, kernel, tPA, tPB): |
| 9954 |
tPA["localReadOffset"] = tPA["savedLocalReadOffset"] |
| 9955 |
tPB["localReadOffset"] = tPB["savedLocalReadOffset"] |
| 9956 |
tPM = tPA["tpsMetadata"] if tPA["is_sparse"] else tPB["tpsMetadata"] |
| 9957 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 9958 |
tPM["localReadOffset"] = tPM["savedLocalReadOffset"] |
| 9959 |
self.states.localReadDoCntA = self.states.savedLocalReadDoCntA |
| 9960 |
self.states.localReadDoCntB = self.states.savedLocalReadDoCntB |
| 9961 |
self.states.localReadDoCntMetadata = self.states.savedLocalReadDoCntMetadata |
| 9962 |
if kernel["ExpandPointerSwap"]: |
| 9963 |
tPA["localWriteSwapByteOffset"] = tPA["savedLocalWriteSwapByteOffset"] |
| 9964 |
tPB["localWriteSwapByteOffset"] = tPB["savedLocalWriteSwapByteOffset"] |
| 9965 |
if kernel["ProblemType"]["Sparse"] and not kernel["DirectToVgprSparseMetadata"]: |
| 9966 |
tPM["localWriteSwapByteOffset"] = tPM["savedLocalWriteSwapByteOffset"] |
| 9967 |
|
| 9968 |
|
| 9969 |
|
| 9970 |
def shiftVectorComponents(self, kernel, tP): |
| 9971 |
component = Component.ShiftVectorComponents.find(self) |
| 9972 |
if component: |
| 9973 |
return component(self, kernel, tP) |
| 9974 |
|
| 9975 |
|
| 9976 |
|
| 9977 |
|
| 9978 |
|
| 9979 |
|
| 9980 |
def computeStoreSrdStart(self, kernel, srdTcList: list, sgprBpeList = [], useSize: list = [], noMultipleBuffer = False): |
| 9981 |
module = Module("computeStoreSrdStart") |
| 9982 |
|
| 9983 |
if useSize: |
| 9984 |
assert len(srdTcList) == len(useSize) |
| 9985 |
else: |
| 9986 |
useSize = [False for _ in srdTcList] |
| 9987 |
|
| 9988 |
with self.allocTmpSgpr(3) as tmpSgprInfo: |
| 9989 |
tmpS0 = tmpSgprInfo.idx |
| 9990 |
tmpS1 = tmpS0+1 |
| 9991 |
wgMT1 = tmpS0+2 |
| 9992 |
|
| 9993 |
|
| 9994 |
assert kernel["BufferStore"] |
| 9995 |
module.addSpaceLine() |
| 9996 |
module.add(SMulI32( |
| 9997 |
dst=sgpr(wgMT1), \ |
| 9998 |
src0="MT1", \ |
| 9999 |
src1=sgpr("WorkGroup1"), \ |
| 10000 |
comment="<- wg1*MT1")) |
| 10001 |
|
| 10002 |
|
| 10003 |
|
| 10004 |
|
| 10005 |
|
| 10006 |
|
| 10007 |
|
| 10008 |
|
| 10009 |
|
| 10010 |
|
| 10011 |
|
| 10012 |
|
| 10013 |
|
| 10014 |
|
| 10015 |
|
| 10016 |
|
| 10017 |
|
| 10018 |
|
| 10019 |
|
| 10020 |
indices = list(range(0, kernel["ProblemType"]["NumIndicesC"])) |
| 10021 |
numDim = len(indices) |
| 10022 |
addrSrcSgpr = "Address" |
| 10023 |
for i in range(1, numDim): |
| 10024 |
if i == kernel["ProblemType"]["Index0"]: |
| 10025 |
|
| 10026 |
addToSrd = False |
| 10027 |
elif i == kernel["ProblemType"]["Index1"] and len(kernel["PackedC1IndicesX"]) == 1: |
| 10028 |
coord = sgpr(wgMT1) |
| 10029 |
addToSrd = True |
| 10030 |
elif i != kernel["ProblemType"]["Index0"] and i != kernel["ProblemType"]["Index1"] and not isPackedIndex(kernel, i): |
| 10031 |
|
| 10032 |
isStridedBuffer = kernel["ProblemType"]["StridedBatched"] or kernel["_GlobalAccumulation"] |
| 10033 |
coord = sgpr("WorkGroup2") if isStridedBuffer else None |
| 10034 |
addToSrd = True if isStridedBuffer else False |
| 10035 |
else: |
| 10036 |
|
| 10037 |
coord = None |
| 10038 |
addToSrd = False |
| 10039 |
|
| 10040 |
if not sgprBpeList: |
| 10041 |
sgprBpeList = [""] * len(srdTcList) |
| 10042 |
assert len(srdTcList) == len(sgprBpeList) |
| 10043 |
if addToSrd: |
| 10044 |
for mat, sgprBpe, us in zip(srdTcList, sgprBpeList, useSize): |
| 10045 |
bpe = self.states.bpeCinternal if mat =="Bias" else (self.states.bpeE if mat == "E" else self.states.bpeCexternal) |
| 10046 |
bpe = int(self.states.bpr * kernel["ProblemType"]["DestDataType"].numRegisters()) if kernel["_GlobalAccumulation"] == 'MultipleBuffer' and mat =="C" else bpe |
| 10047 |
bpe = sgpr(sgprBpe) if sgprBpe else log2(bpe) |
| 10048 |
|
| 10049 |
if us: |
| 10050 |
if i == 0: |
| 10051 |
|
| 10052 |
continue |
| 10053 |
if i > 1: |
| 10054 |
strideC0 = "Size%s"%(INDEX_CHARS[0]) |
| 10055 |
strideC1 = "Size%s"%(INDEX_CHARS[1]) |
| 10056 |
module.add(SMulI32(dst=sgpr(tmpS0), src0=sgpr(strideC0), src1=sgpr(strideC1))) |
| 10057 |
for x in range(2, i - 1): |
| 10058 |
strideC = "Size%s"%(INDEX_CHARS[x]) |
| 10059 |
module.add(SMulI32(dst=sgpr(tmpS0), src0=sgpr(tmpS0), src1=sgpr(strideC))) |
| 10060 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(tmpS0), sgpr(tmpS1), coord, sgpr(tmpS0), comment="Scale%s %s by Stride"%(mat, coord))) |
| 10061 |
else: |
| 10062 |
strideC = "Size%s"%(INDEX_CHARS[i-1]) |
| 10063 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(tmpS0), sgpr(tmpS1), coord, sgpr(strideC), comment="Scale%s %s by Stride"%(mat, coord))) |
| 10064 |
else: |
| 10065 |
strideC = "Stride%s%s"%(mat, self.states.indexChars[i]) |
| 10066 |
module.addModuleAsFlatItems(self.s_mul_u64_u32(sgpr(tmpS0), sgpr(tmpS1), coord, sgpr(strideC), comment="Scale%s %s by Stride"%(mat, coord))) |
| 10067 |
module.add(SLShiftLeftB64(dst=sgpr(tmpS0,2), src=sgpr(tmpS0,2), shiftHex=bpe, comment="scale by bpe")) |
| 10068 |
|
| 10069 |
module.add(SAddU32(dst=sgpr("Srd%s+0"%mat), src0=sgpr("%s%s+0"%(addrSrcSgpr, mat)), src1=sgpr(tmpS0), comment="add lo to SRD")) |
| 10070 |
module.add(SAddCU32(dst=sgpr("Srd%s+1"%mat), src0=sgpr("%s%s+1"%(addrSrcSgpr, mat)), src1=sgpr(tmpS1), comment="add hi to SRD")) |
| 10071 |
|
| 10072 |
module.addSpaceLine() |
| 10073 |
|
| 10074 |
addrSrcSgpr = "Srd" |
| 10075 |
|
| 10076 |
if noMultipleBuffer: |
| 10077 |
return module |
| 10078 |
|
| 10079 |
gsuComponent = Component.GSU.find(self) |
| 10080 |
module.add(gsuComponent.computeStoreSrdStart(self, kernel)) |
| 10081 |
|
| 10082 |
skComponent = Component.StreamK.find(self) |
| 10083 |
module.add(skComponent.computeStoreSrdStart(self, kernel)) |
| 10084 |
|
| 10085 |
for cdir in (0,1): |
| 10086 |
indices = kernel["PackedC%uIndicesX"%cdir] |
| 10087 |
packedSizes = "PackedSize%u"%cdir |
| 10088 |
if len(indices) > 1: |
| 10089 |
for i,idx in enumerate(indices[1:]): |
| 10090 |
if i==0: |
| 10091 |
module.add(SMulI32(dst=sgpr(packedSizes), src0=self.sizeRef(indices[0]), \ |
| 10092 |
src1=self.sizeRef(idx), comment="first packed size")) |
| 10093 |
else: |
| 10094 |
module.add(SMulI32(dst=sgpr(packedSizes), src0=sgpr(packedSizes), \ |
| 10095 |
src1=self.sizeRef(idx), comment="first packed size")) |
| 10096 |
|
| 10097 |
return module |
| 10098 |
|
| 10099 |
|
| 10100 |
|
| 10101 |
|
| 10102 |
|
| 10103 |
|
| 10104 |
def computeStoreVgprs(self, kernel, divisor=None, tid0Scale=None, tid1Scale=None): |
| 10105 |
module = Module("computeStoreVgprs") |
| 10106 |
module.addComment0("computeStoreVgprs") |
| 10107 |
component = Component.ComputeStoreVgprs.find(self) |
| 10108 |
if component: |
| 10109 |
if kernel["EnableMatrixInstruction"]: |
| 10110 |
module.add(component(self, kernel)) |
| 10111 |
else: |
| 10112 |
module.add(component(self, kernel, divisor, tid0Scale, tid1Scale)) |
| 10113 |
return module |
| 10114 |
|
| 10115 |
|
| 10116 |
|
| 10117 |
|
| 10118 |
|
| 10119 |
def SrdTDInit(self, kernel): |
| 10120 |
module = Module("SrdTDInit") |
| 10121 |
tmpspgr0 = self.sgprPool.checkOut(1) |
| 10122 |
tmpspgr = self.sgprPool.checkOutAligned(2, 4, preventOverflow=False) |
| 10123 |
|
| 10124 |
module.addComment0("calculate SrdTD address") |
| 10125 |
|
| 10126 |
module.add(SMovB32(dst=sgpr("SrdTD+2"), src="BufferOOB")) |
| 10127 |
module.add(SMovB32(dst=sgpr("SrdTD+3"), src="Srd127_96", comment="Set bits 127_96 in post-loop SRD")) |
| 10128 |
|
| 10129 |
module.add(SMulI32(dst=sgpr(tmpspgr0), src0="MT1", src1=sgpr("WorkGroup1"), comment="")) |
| 10130 |
module.add(SMulHIU32(dst=sgpr(tmpspgr+1), src0=sgpr(tmpspgr0), src1=sgpr("StrideD1J"), comment="")) |
| 10131 |
module.add(SMulI32(dst=sgpr(tmpspgr+0), src0=sgpr(tmpspgr0), src1=sgpr("StrideD1J"), comment="")) |
| 10132 |
|
| 10133 |
bpe = int(self.states.bpr * kernel["ProblemType"]["DestDataType"].numRegisters()) |
| 10134 |
module.add(SLShiftLeftB64(dst=sgpr(tmpspgr,2), src=sgpr(tmpspgr,2), shiftHex=log2(bpe), comment="scale by bpe")) |
| 10135 |
|
| 10136 |
module.add(SAddU32(dst=sgpr("SrdTD+0"), src0=sgpr("AddressTD+0"), src1=sgpr(tmpspgr+0), comment="add lo to SRTD" )) |
| 10137 |
module.add(SAddCU32(dst=sgpr("SrdTD+1"), src0=sgpr("AddressTD+1"), src1=sgpr(tmpspgr+1), comment="add hi to SRTD" )) |
| 10138 |
|
| 10139 |
module.add(SMulHIU32(dst=sgpr(tmpspgr+1), src0=sgpr("WorkGroup2"), src1=sgpr("StrideDK"), comment="")) |
| 10140 |
module.add(SMulI32(dst=sgpr(tmpspgr+0), src0=sgpr("WorkGroup2"), src1=sgpr("StrideDK"), comment="")) |
| 10141 |
|
| 10142 |
module.add(SLShiftLeftB64(dst=sgpr(tmpspgr,2), src=sgpr(tmpspgr,2), shiftHex=log2(bpe), comment="scale by bpe")) |
| 10143 |
|
| 10144 |
module.add(SAddU32(dst=sgpr("SrdTD+0"), src0=sgpr("SrdTD+0"), src1=sgpr(tmpspgr+0), comment="add lo to SRTD" )) |
| 10145 |
module.add(SAddCU32(dst=sgpr("SrdTD+1"), src0=sgpr("SrdTD+1"), src1=sgpr(tmpspgr+1), comment="add hi to SRTD" )) |
| 10146 |
|
| 10147 |
self.sgprPool.checkIn(tmpspgr0) |
| 10148 |
self.sgprPool.checkIn(tmpspgr) |
| 10149 |
|
| 10150 |
return module |
| 10151 |
|
| 10152 |
def globalWriteWorkGroupInit(self, kernel): |
| 10153 |
module = Module("globalWriteWorkGroupInit") |
| 10154 |
if kernel["BufferStore"]: |
| 10155 |
module.add(self.allocPostLoopSrd("D")) |
| 10156 |
module.add(self.allocPostLoopSrd("C")) |
| 10157 |
sgprBpeList = ["GSULog2BpeC", "GSULog2BpeD"] if kernel["GlobalSplitU"] != 0 else [] |
| 10158 |
|
| 10159 |
|
| 10160 |
if kernel["StreamK"] == 3: |
| 10161 |
sgprLog2BpeC = self.sgprPool.checkOut(1, preventOverflow=False) |
| 10162 |
sgprLog2BpeD = self.sgprPool.checkOut(1, preventOverflow=False) |
| 10163 |
|
| 10164 |
module.add(SMovB32(dst=sgpr(sgprLog2BpeC), src=log2(self.states.bpeCexternalGSU1))) |
| 10165 |
module.add(SMovB32(dst=sgpr(sgprLog2BpeD), src=log2(self.states.bpeCexternalGSU1))) |
| 10166 |
|
| 10167 |
bpeDoneLabel = Label(label=self.labels.getNameInc("BPEDone"), comment="") |
| 10168 |
|
| 10169 |
module.add(SCmpEQU64(src0=sgpr("AddressFlags", 2), src1=hex(0), comment="Check for synchronizer")) |
| 10170 |
module.add(SCBranchSCC0(labelName=bpeDoneLabel.getLabelName(), comment="If synchronizer, use regular output BPE")) |
| 10171 |
module.add(SCmpEQU32(src0=sgpr("skTiles"), src1=1, comment="split == 1 ?")) |
| 10172 |
module.add(SCBranchSCC1(labelName=bpeDoneLabel.getLabelName(), comment="If split == 1, use reguler output BPE")) |
| 10173 |
|
| 10174 |
|
| 10175 |
module.add(SMovB32(dst=sgpr(sgprLog2BpeC), src=log2(int(self.states.bpr * kernel["ProblemType"]["DestDataType"].numRegisters())))) |
| 10176 |
module.add(SMovB32(dst=sgpr(sgprLog2BpeD), src=log2(self.states.bpeCinternal))) |
| 10177 |
|
| 10178 |
module.add(bpeDoneLabel) |
| 10179 |
sgprBpeList = [sgprLog2BpeC, sgprLog2BpeD] |
| 10180 |
|
| 10181 |
module.add(self.computeStoreSrdStart(kernel, ["C", "D"], sgprBpeList=sgprBpeList)) |
| 10182 |
if kernel["GlobalSplitU"] != 0: |
| 10183 |
module.add(self.undefineSgpr("GSULog2BpeC")) |
| 10184 |
if kernel["StreamK"] == 0: |
| 10185 |
module.add(self.undefineSgpr("AddressC")) |
| 10186 |
|
| 10187 |
if kernel["StreamK"] == 3: |
| 10188 |
self.sgprPool.checkIn(sgprLog2BpeD) |
| 10189 |
self.sgprPool.checkIn(sgprLog2BpeC) |
| 10190 |
return module |
| 10191 |
|
| 10192 |
|
| 10193 |
|
| 10194 |
|
| 10195 |
def localSplitUGlobalWriteIndices(self, kernel): |
| 10196 |
module = Module("localSplitUGlobalWriteIndices") |
| 10197 |
|
| 10198 |
|
| 10199 |
packedC1 = kernel["PackedC1IndicesX"] |
| 10200 |
strideC1 = "StrideC%s" % (self.states.indexChars[packedC1[0]]) |
| 10201 |
strideD1 = "StrideD%s" % (self.states.indexChars[packedC1[0]]) |
| 10202 |
wave_id = self.vgprPool.checkOut(1, "tmpWaveID") |
| 10203 |
tmpVgpr1 = self.vgprPool.checkOutAligned(2,2,"tmpVgpr1") |
| 10204 |
tmpVgpr1Res = ContinuousRegister(tmpVgpr1, 2) |
| 10205 |
module.add(vectorStaticDivide(wave_id, "Serial", kernel["WavefrontSize"], tmpVgpr1Res)) |
| 10206 |
numWaves = kernel["MIWaveGroup"][0] * kernel["MIWaveGroup"][1] |
| 10207 |
module.add(vectorStaticDivide(wave_id, wave_id, numWaves, tmpVgpr1Res)) |
| 10208 |
|
| 10209 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 10210 |
tmpSgpr = tmpSgprInfo.idx |
| 10211 |
if self.LSUValidOffset0 > 0: |
| 10212 |
module.add(SMovB32(dst=sgpr(tmpSgpr), \ |
| 10213 |
src=hex(self.LSUValidOffset0), comment="a valid offset")) |
| 10214 |
module.add(VMulLOU32(dst=vgpr(tmpVgpr1), src0=vgpr(wave_id), src1=sgpr(tmpSgpr), comment="wave LSU offset")) |
| 10215 |
module.add(VAddU32(dst=vgpr(self.vgprs.coord0), src0=vgpr(tmpVgpr1), src1=vgpr(self.vgprs.coord0), comment="coord0 += LSU offset0")) |
| 10216 |
else: |
| 10217 |
module.addComment0("valid offset coord0 is zero.") |
| 10218 |
|
| 10219 |
if self.LSUValidOffset1 > 0: |
| 10220 |
module.add(SMovB32(dst=sgpr(tmpSgpr), \ |
| 10221 |
src=hex(self.LSUValidOffset1), comment="a valid offset")) |
| 10222 |
module.add(VMulLOU32(dst=vgpr(tmpVgpr1), src0=vgpr(wave_id), src1=sgpr(tmpSgpr), comment="wave LSU offset")) |
| 10223 |
module.add(VAddU32(dst=vgpr(self.vgprs.coord1), src0=vgpr(tmpVgpr1), src1=vgpr(self.vgprs.coord1), comment="coord1 += LSU offset1")) |
| 10224 |
module.add(VAddU32(dst=vgpr(self.vgprs.coord1InMT), src0=vgpr(tmpVgpr1), src1=vgpr(self.vgprs.coord1InMT), comment="coord1InMT += LSU offset1")) |
| 10225 |
|
| 10226 |
|
| 10227 |
packedC1 = kernel["PackedC1IndicesX"] |
| 10228 |
strideC1 = "StrideC%s" % (self.states.indexChars[packedC1[0]]) |
| 10229 |
strideD1 = "StrideD%s" % (self.states.indexChars[packedC1[0]]) |
| 10230 |
module.add(VMulLOU32(dst=vgpr(self.vgprs.cinRowPtr), src0=vgpr(self.vgprs.coord1InMT), src1=sgpr(strideC1), comment=" offset 1")) |
| 10231 |
module.add(VMulLOU32(dst=vgpr(self.vgprs.coutRowPtrD), src0=vgpr(self.vgprs.coord1InMT), src1=sgpr(strideD1), comment=" offset 1")) |
| 10232 |
if kernel["ProblemType"]["UseE"] and ((kernel["GlobalSplitU"] == 1 or kernel["GlobalSplitU"] == -1) or kernel["StreamK"] > 0): |
| 10233 |
module.add(VMovB32(dst=vgpr(self.vgprs.coutRowPtrE), src=vgpr(self.vgprs.coord1InMT), comment=" save offset 1 for E")) |
| 10234 |
if self.vgprs.coutRowPtrBias != -1: |
| 10235 |
index = packedC1[0] - 1 |
| 10236 |
strideW1 = "Size%s" % "I" if index == 0 else ("J" if index == 1 else (self.states.indexChars[index])) |
| 10237 |
module.add(VMulLOU32(dst=vgpr(self.vgprs.coutRowPtrBias), src0=vgpr(self.vgprs.coord1InMT), src1=sgpr(strideW1), comment=" offset 1")) |
| 10238 |
else: |
| 10239 |
module.addComment0("valid offset coord1 is zero.") |
| 10240 |
|
| 10241 |
self.vgprPool.checkIn(tmpVgpr1) |
| 10242 |
self.vgprPool.checkIn(wave_id) |
| 10243 |
self.vgprPool.checkIn(self.lsuCoordOffset) |
| 10244 |
self.vgprPool.checkIn(self.vgprs.coord0InMT) |
| 10245 |
self.vgprPool.checkIn(self.vgprs.coord1InMT) |
| 10246 |
|
| 10247 |
if kernel["BufferStore"]: |
| 10248 |
|
| 10249 |
|
| 10250 |
self.vgprs.addrE = -1 |
| 10251 |
self.vgprs.addrD = -1 |
| 10252 |
self.vgprs.addrC = -1 |
| 10253 |
self.vgprs.addrBias = -1 |
| 10254 |
self.vgprs.addrScaleAVec = -1 |
| 10255 |
self.vgprs.addrScaleBVec = -1 |
| 10256 |
self.vgprs.addrScaleAlphaVec = -1 |
| 10257 |
else: |
| 10258 |
self.vgprs.addrD = self.vgprPool.checkOut(2) |
| 10259 |
module.add(VMovB32( |
| 10260 |
dst=vgpr(self.vgprs.addrD+0), \ |
| 10261 |
src=sgpr("AddressD+0"), \ |
| 10262 |
comment="sgpr -> vgpr")) |
| 10263 |
module.add(VMovB32( |
| 10264 |
dst=vgpr(self.vgprs.addrD+1), \ |
| 10265 |
src=sgpr("AddressD+1"), \ |
| 10266 |
comment="sgpr -> vgpr")) |
| 10267 |
self.vgprs.addrC = self.vgprPool.checkOut(2) |
| 10268 |
module.add(VMovB32( |
| 10269 |
dst=vgpr(self.vgprs.addrC+0), \ |
| 10270 |
src=sgpr("AddressC+0"), \ |
| 10271 |
comment="sgpr -> vgpr")) |
| 10272 |
module.add(VMovB32( |
| 10273 |
dst=vgpr(self.vgprs.addrC+1), \ |
| 10274 |
src=sgpr("AddressC+1"), \ |
| 10275 |
comment="sgpr -> vgpr")) |
| 10276 |
|
| 10277 |
if kernel["GlobalSplitU"] != 0: |
| 10278 |
gsuLabel = Label(label=self.labels.getNameInc("GSU"), comment="") |
| 10279 |
with self.allocTmpSgpr(1) as tmpSgprGSU: |
| 10280 |
module.add(SAndB32(dst=sgpr(tmpSgprGSU.idx), src0=sgpr("GSU"), src1=hex(0x3FFF), comment="Restore GSU")) |
| 10281 |
module.add(SCmpEQU32(src0=sgpr(tmpSgprGSU.idx), src1=1, comment="GSU == 1 ?")) |
| 10282 |
module.add(SCBranchSCC0(labelName=gsuLabel.getLabelName(), comment="branch if GSU != 1")) |
| 10283 |
if kernel["ProblemType"]["UseE"]: |
| 10284 |
self.vgprs.addrE = self.vgprPool.checkOut(2, 'addrE') |
| 10285 |
module.add(VMovB32( \ |
| 10286 |
dst=vgpr(self.vgprs.addrE+0), \ |
| 10287 |
src=sgpr("AddressE+0"), \ |
| 10288 |
comment="sgpr -> vgpr")) |
| 10289 |
module.add(VMovB32( \ |
| 10290 |
dst=vgpr(self.vgprs.addrE+1), \ |
| 10291 |
src=sgpr("AddressE+1"), \ |
| 10292 |
comment="sgpr -> vgpr")) |
| 10293 |
if self.states.useBias == DataDirection.READ: |
| 10294 |
self.vgprs.addrBias = self.vgprPool.checkOut(2, 'addrBias') |
| 10295 |
module.add(VMovB32( \ |
| 10296 |
dst=vgpr(self.vgprs.addrBias+0), \ |
| 10297 |
src=sgpr("AddressBias+0"), \ |
| 10298 |
comment="sgpr -> vgpr")) |
| 10299 |
module.add(VMovB32( \ |
| 10300 |
dst=vgpr(self.vgprs.addrBias+1), \ |
| 10301 |
src=sgpr("AddressBias+1"), \ |
| 10302 |
comment="sgpr -> vgpr")) |
| 10303 |
if (kernel["ProblemType"]["UseScaleAB"] == "Vector"): |
| 10304 |
self.vgprs.addrScaleAVec = self.vgprPool.checkOut(2, 'addrScaleAVec') |
| 10305 |
module.add(VMovB32( \ |
| 10306 |
dst=vgpr(self.vgprs.addrScaleAVec+0), \ |
| 10307 |
src=sgpr("AddressScaleA+0"), \ |
| 10308 |
comment="sgpr -> vgpr")) |
| 10309 |
module.add(VMovB32( \ |
| 10310 |
dst=vgpr(self.vgprs.addrScaleAVec+1), \ |
| 10311 |
src=sgpr("AddressScaleA+1"), \ |
| 10312 |
comment="sgpr -> vgpr")) |
| 10313 |
self.vgprs.addrScaleBVec = self.vgprPool.checkOut(2, 'addrScaleVVec') |
| 10314 |
module.add(VMovB32( \ |
| 10315 |
dst=vgpr(self.vgprs.addrScaleBVec+0), \ |
| 10316 |
src=sgpr("AddressScaleB+0"), \ |
| 10317 |
comment="sgpr -> vgpr")) |
| 10318 |
module.add(VMovB32( \ |
| 10319 |
dst=vgpr(self.vgprs.addrScaleBVec+1), \ |
| 10320 |
src=sgpr("AddressScaleB+1"), \ |
| 10321 |
comment="sgpr -> vgpr")) |
| 10322 |
if kernel["ProblemType"]["UseScaleAlphaVec"]: |
| 10323 |
self.vgprs.addrScaleAlphaVec = self.vgprPool.checkOut(2, 'addrScaleAlphaVec') |
| 10324 |
module.add(VMovB32( \ |
| 10325 |
dst=vgpr(self.vgprs.addrScaleAlphaVec+0), \ |
| 10326 |
src=sgpr("AddressScaleAlphaVec+0"), \ |
| 10327 |
comment="sgpr -> vgpr")) |
| 10328 |
module.add(VMovB32( \ |
| 10329 |
dst=vgpr(self.vgprs.addrScaleAlphaVec+1), \ |
| 10330 |
src=sgpr("AddressScaleAlphaVec+1"), \ |
| 10331 |
comment="sgpr -> vgpr")) |
| 10332 |
if kernel["GlobalSplitU"] != 0: |
| 10333 |
module.add(gsuLabel) |
| 10334 |
|
| 10335 |
return module |
| 10336 |
|
| 10337 |
|
| 10338 |
def allocPostLoopSrd(self, ch: str): |
| 10339 |
module = Module("allocPostLoopSrd") |
| 10340 |
|
| 10341 |
module.add(SMovB64(dst=sgpr("Srd%s+0"%ch, 2), src=sgpr("Address%s+0"%ch, 2), comment="init SRD base address" )) |
| 10342 |
module.add(SMovB32(dst=sgpr("Srd%s+2"%ch), src="BufferOOB")) |
| 10343 |
module.add(SMovB32(dst=sgpr("Srd%s+3"%ch), src="Srd127_96", comment="Set bits 127_96 in post-loop SRD")) |
| 10344 |
module.addSpaceLine() |
| 10345 |
return module |
| 10346 |
|
| 10347 |
|
| 10348 |
|
| 10349 |
|
| 10350 |
def notLocalSplitUGlobalWriteIndices(self, kernel): |
| 10351 |
|
| 10352 |
if not self.do["PostLoop"]: return "" |
| 10353 |
module = Module("notLocalSplitUGlobalWriteIndices") |
| 10354 |
|
| 10355 |
if kernel["EnableMatrixInstruction"]: |
| 10356 |
module.add(self.computeStoreVgprs(kernel)) |
| 10357 |
else: |
| 10358 |
module.add(self.computeStoreVgprs(kernel, |
| 10359 |
divisor = kernel["SubGroup0"], |
| 10360 |
tid0Scale = kernel["VectorWidthA"], |
| 10361 |
tid1Scale = kernel["VectorWidthB"])) |
| 10362 |
|
| 10363 |
if kernel["BufferStore"]: |
| 10364 |
|
| 10365 |
|
| 10366 |
self.vgprs.addrE = -1 |
| 10367 |
self.vgprs.addrD = -1 |
| 10368 |
self.vgprs.addrC = -1 |
| 10369 |
self.vgprs.addrBias = -1 |
| 10370 |
self.vgprs.addrScaleAVec = -1 |
| 10371 |
self.vgprs.addrScaleBVec = -1 |
| 10372 |
self.vgprs.addrScaleAlphaVec = -1 |
| 10373 |
else: |
| 10374 |
self.vgprs.addrD = self.vgprPool.checkOut(2, 'addrD') |
| 10375 |
module.add(VMovB32( |
| 10376 |
dst=vgpr(self.vgprs.addrD+0), \ |
| 10377 |
src=sgpr("AddressD+0"), \ |
| 10378 |
comment="sgpr -> vgpr")) |
| 10379 |
module.add(VMovB32( |
| 10380 |
dst=vgpr(self.vgprs.addrD+1), \ |
| 10381 |
src=sgpr("AddressD+1"), \ |
| 10382 |
comment="sgpr -> vgpr")) |
| 10383 |
self.vgprs.addrC = self.vgprPool.checkOut(2, 'addrC') |
| 10384 |
module.add(VMovB32( |
| 10385 |
dst=vgpr(self.vgprs.addrC+0), \ |
| 10386 |
src=sgpr("AddressC+0"), \ |
| 10387 |
comment="sgpr -> vgpr")) |
| 10388 |
module.add(VMovB32( |
| 10389 |
dst=vgpr(self.vgprs.addrC+1), \ |
| 10390 |
src=sgpr("AddressC+1"), \ |
| 10391 |
comment="sgpr -> vgpr")) |
| 10392 |
|
| 10393 |
if kernel["GlobalSplitU"] != 0: |
| 10394 |
gsuLabel = Label(label=self.labels.getNameInc("GSU"), comment="") |
| 10395 |
with self.allocTmpSgpr(1) as tmpSgprGSU: |
| 10396 |
module.add(SAndB32(dst=sgpr(tmpSgprGSU.idx), src0=sgpr("GSU"), src1=hex(0x3FFF), comment="Restore GSU")) |
| 10397 |
module.add(SCmpEQU32(src0=sgpr(tmpSgprGSU.idx), src1=1, comment="GSU == 1 ?")) |
| 10398 |
module.add(SCBranchSCC0(labelName=gsuLabel.getLabelName(), comment="branch if GSU != 1")) |
| 10399 |
if kernel["ProblemType"]["UseE"]: |
| 10400 |
self.vgprs.addrE = self.vgprPool.checkOut(2, 'addrE') |
| 10401 |
module.add(VMovB32( \ |
| 10402 |
dst=vgpr(self.vgprs.addrE+0), \ |
| 10403 |
src=sgpr("AddressE+0"), \ |
| 10404 |
comment="sgpr -> vgpr")) |
| 10405 |
module.add(VMovB32( \ |
| 10406 |
dst=vgpr(self.vgprs.addrE+1), \ |
| 10407 |
src=sgpr("AddressE+1"), \ |
| 10408 |
comment="sgpr -> vgpr")) |
| 10409 |
if self.states.useBias == DataDirection.READ: |
| 10410 |
self.vgprs.addrBias = self.vgprPool.checkOut(2, 'addrBias') |
| 10411 |
module.add(VMovB32( \ |
| 10412 |
dst=vgpr(self.vgprs.addrBias+0), \ |
| 10413 |
src=sgpr("AddressBias+0"), \ |
| 10414 |
comment="sgpr -> vgpr")) |
| 10415 |
module.add(VMovB32( \ |
| 10416 |
dst=vgpr(self.vgprs.addrBias+1), \ |
| 10417 |
src=sgpr("AddressBias+1"), \ |
| 10418 |
comment="sgpr -> vgpr")) |
| 10419 |
if (kernel["ProblemType"]["UseScaleAB"] == "Vector"): |
| 10420 |
self.vgprs.addrScaleAVec = self.vgprPool.checkOut(2, 'addrScaleAVec') |
| 10421 |
module.add(VMovB32( \ |
| 10422 |
dst=vgpr(self.vgprs.addrScaleAVec+0), \ |
| 10423 |
src=sgpr("AddressScaleA+0"), \ |
| 10424 |
comment="sgpr -> vgpr")) |
| 10425 |
module.add(VMovB32( \ |
| 10426 |
dst=vgpr(self.vgprs.addrScaleAVec+1), \ |
| 10427 |
src=sgpr("AddressScaleA+1"), \ |
| 10428 |
comment="sgpr -> vgpr")) |
| 10429 |
self.vgprs.addrScaleBVec = self.vgprPool.checkOut(2, 'addrScaleBVec') |
| 10430 |
module.add(VMovB32( \ |
| 10431 |
dst=vgpr(self.vgprs.addrScaleBVec+0), \ |
| 10432 |
src=sgpr("AddressScaleB+0"), \ |
| 10433 |
comment="sgpr -> vgpr")) |
| 10434 |
module.add(VMovB32( \ |
| 10435 |
dst=vgpr(self.vgprs.addrScaleBVec+1), \ |
| 10436 |
src=sgpr("AddressScaleB+1"), \ |
| 10437 |
comment="sgpr -> vgpr")) |
| 10438 |
if kernel["ProblemType"]["UseScaleAlphaVec"]: |
| 10439 |
self.vgprs.addrScaleAlphaVec = self.vgprPool.checkOut(2, 'addrScaleAlphaVec') |
| 10440 |
module.add(VMovB32( \ |
| 10441 |
dst=vgpr(self.vgprs.addrScaleAlphaVec+0), \ |
| 10442 |
src=sgpr("AddressScaleAlphaVec+0"), \ |
| 10443 |
comment="sgpr -> vgpr")) |
| 10444 |
module.add(VMovB32( \ |
| 10445 |
dst=vgpr(self.vgprs.addrScaleAlphaVec+1), \ |
| 10446 |
src=sgpr("AddressScaleAlphaVec+1"), \ |
| 10447 |
comment="sgpr -> vgpr")) |
| 10448 |
if kernel["GlobalSplitU"] != 0: |
| 10449 |
module.add(gsuLabel) |
| 10450 |
return module |
| 10451 |
|
| 10452 |
|
| 10453 |
|
| 10454 |
def cleanupGlobalWrite(self, kernel): |
| 10455 |
self.vgprPool.checkIn(self.vgprs.coord0) |
| 10456 |
self.vgprPool.checkIn(self.vgprs.coord1) |
| 10457 |
|
| 10458 |
if kernel["StoreRemapVectorWidth"]: |
| 10459 |
self.vgprPool.checkIn(self.vgprs.storeRemapLW) |
| 10460 |
self.vgprPool.checkIn(self.vgprs.storeRemapLR) |
| 10461 |
self.vgprPool.checkIn(self.vgprs.storeRemapCoord0) |
| 10462 |
self.vgprPool.checkIn(self.vgprs.storeRemapCoord1) |
| 10463 |
self.vgprPool.checkIn(self.vgprs.storeRemapOffsetCoord1) |
| 10464 |
for v in self.vgprs.storeRemapAS: |
| 10465 |
self.vgprPool.checkIn(v) |
| 10466 |
if kernel["BufferStore"]: |
| 10467 |
self.vgprPool.checkIn(self.vgprs.cinRowPtr) |
| 10468 |
self.vgprPool.checkIn(self.vgprs.coutRowPtrD) |
| 10469 |
if self.vgprs.coutRowPtrE != -1: |
| 10470 |
self.vgprPool.checkIn(self.vgprs.coutRowPtrE) |
| 10471 |
self.vgprs.coutRowPtrE = -1 |
| 10472 |
if self.vgprs.coutRowPtrBias != -1: |
| 10473 |
self.vgprPool.checkIn(self.vgprs.coutRowPtrBias) |
| 10474 |
self.vgprs.coutRowPtrBias = -1 |
| 10475 |
if not kernel["BufferStore"]: |
| 10476 |
self.vgprPool.checkIn(self.vgprs.addrD) |
| 10477 |
self.vgprPool.checkIn(self.vgprs.addrC) |
| 10478 |
if self.vgprs.addrE != -1: |
| 10479 |
self.vgprPool.checkIn(self.vgprs.addrE) |
| 10480 |
self.vgprs.addrE = -1 |
| 10481 |
if self.states.useBias == DataDirection.READ: |
| 10482 |
self.vgprPool.checkIn(self.vgprs.addrBias) |
| 10483 |
self.vgprs.addrBias = -1 |
| 10484 |
if self.vgprs.addrScaleAVec != -1: |
| 10485 |
self.vgprPool.checkIn(self.vgprs.addrScaleAVec) |
| 10486 |
self.vgprs.addrScaleAVec = -1 |
| 10487 |
if self.vgprs.addrScaleBVec != -1: |
| 10488 |
self.vgprPool.checkIn(self.vgprs.addrScaleBVec) |
| 10489 |
self.vgprs.addrScaleBVec = -1 |
| 10490 |
if self.vgprs.addrScaleAlphaVec != -1: |
| 10491 |
self.vgprPool.checkIn(self.vgprs.addrScaleAlphaVec) |
| 10492 |
self.vgprs.addrScaleAlphaVec = -1 |
| 10493 |
|
| 10494 |
|
| 10495 |
|
| 10496 |
def maxGwvw(self, kernel): |
| 10497 |
atomic = (kernel["GlobalSplitU"] > 1 or kernel["GlobalSplitU"] == -1) and (kernel["_GlobalAccumulation"] != 'MultipleBuffer' and kernel["_GlobalAccumulation"] != 'MultipleBufferSingleKernel') |
| 10498 |
|
| 10499 |
if kernel["BufferStore"]: |
| 10500 |
if atomic: |
| 10501 |
return self.getVectorAtomicWidth(kernel) |
| 10502 |
else: |
| 10503 |
return 1000 |
| 10504 |
else: |
| 10505 |
if atomic: |
| 10506 |
return 1 |
| 10507 |
else: |
| 10508 |
return 1000 |
| 10509 |
|
| 10510 |
def getVectorAtomicWidth(self, kernel): |
| 10511 |
if kernel["ProblemType"]["DataType"].isHalf() and (not kernel["_GlobalAccumulation"]): |
| 10512 |
return 2 |
| 10513 |
return 1 |
| 10514 |
|
| 10515 |
|
| 10516 |
|
| 10517 |
|
| 10518 |
|
| 10519 |
|
| 10520 |
def notLocalFullTileElements(self, kernel, edge): |
| 10521 |
component = Component.NotLocalFullTileElements.find(self) |
| 10522 |
if component: |
| 10523 |
return component(self, kernel, edge) |
| 10524 |
|
| 10525 |
|
| 10526 |
|
| 10527 |
|
| 10528 |
def storeRemapAddLocalWrite(self, kernel, ss, addrCalc, srcVgpr): |
| 10529 |
""" |
| 10530 |
Add localWrite for the element with addrCalc and srcVgpr. |
| 10531 |
""" |
| 10532 |
|
| 10533 |
module = Module("storeRemapAddLocalWrite srcVgpr %s"%str(srcVgpr)) |
| 10534 |
|
| 10535 |
bps = self.states.bpeCexternal * ss.cfg.gwvw |
| 10536 |
rpv = self.states.bpeCexternal * ss.cfg.gwvw / self.states.bpr |
| 10537 |
|
| 10538 |
addr0 = vgpr(self.vgprs.storeRemapLW) |
| 10539 |
offset = addrCalc.coordOffset0 * self.states.bpeCexternal |
| 10540 |
ds = DSModifiers(offset=offset) |
| 10541 |
|
| 10542 |
if bps==1: |
| 10543 |
module.add(DSStoreB8(dstAddr=addr0, src=vgpr(srcVgpr, rpv*4), \ |
| 10544 |
ds=ds, comment="storeRemap lw")) |
| 10545 |
elif bps==2: |
| 10546 |
module.add(DSStoreB16(dstAddr=addr0, src=vgpr(srcVgpr, rpv*2), \ |
| 10547 |
ds=ds, comment="storeRemap lw")) |
| 10548 |
elif bps==4: |
| 10549 |
module.add(DSStoreB32(dstAddr=addr0, src=vgpr(srcVgpr, rpv), \ |
| 10550 |
ds=ds, comment="storeRemap lw")) |
| 10551 |
elif bps==8: |
| 10552 |
module.add(DSStoreB64(dstAddr=addr0, src=vgpr(srcVgpr, rpv), \ |
| 10553 |
ds=ds, comment="storeRemap lw")) |
| 10554 |
elif bps==16: |
| 10555 |
module.add(DSStoreB128(dstAddr=addr0, src=vgpr(srcVgpr, rpv), \ |
| 10556 |
ds=ds, comment="storeRemap lw")) |
| 10557 |
else: |
| 10558 |
assert 0, "StoreRemap: bad bps!" |
| 10559 |
|
| 10560 |
return module |
| 10561 |
|
| 10562 |
|
| 10563 |
|
| 10564 |
|
| 10565 |
def storeRemapAddStore(self, kernel, tmpVgpr, tmpS01, edge, StoreRemapLastBatch): |
| 10566 |
module = Module("storeRemapAddStore") |
| 10567 |
|
| 10568 |
module.add(SWaitCnt(dscnt=0, comment="wait for LDS write")) |
| 10569 |
|
| 10570 |
numStoreInst = 0 |
| 10571 |
|
| 10572 |
|
| 10573 |
|
| 10574 |
if kernel["MIWaveGroup"][0] > 1: |
| 10575 |
|
| 10576 |
module.add(SBarrier(comment="wait all lds write finished")) |
| 10577 |
module.addSpaceLine() |
| 10578 |
|
| 10579 |
gwvw = kernel["StoreRemapVectorWidth"] |
| 10580 |
nElements = kernel["MacroTile0"]*kernel["MatrixInstN"]//kernel["MIWaveGroup"][0]//self.states.kernel["WavefrontSize"] |
| 10581 |
|
| 10582 |
bpe = self.states.bpeCexternal |
| 10583 |
bps = bpe * gwvw |
| 10584 |
rpe = self.states.bpeCexternal / self.states.bpr |
| 10585 |
rpv = rpe * gwvw |
| 10586 |
|
| 10587 |
storeRegs = self.vgprs.storeRemapAS |
| 10588 |
src = vgpr(self.vgprs.storeRemapLR) |
| 10589 |
for rIdx, i in enumerate(range(0, nElements, gwvw)): |
| 10590 |
offset = self.storeRemapLrOffset * bpe * (i//gwvw) |
| 10591 |
ds = DSModifiers(offset=offset) |
| 10592 |
dst = vgpr(storeRegs[rIdx], rpv) |
| 10593 |
if bps==4: |
| 10594 |
module.add(DSLoadB32(dst=dst, src=src, ds=ds, comment="storeRemap lr")) |
| 10595 |
elif bps==8: |
| 10596 |
module.add(DSLoadB64(dst=dst, src=src, ds=ds, comment="storeRemap lr")) |
| 10597 |
elif bps==16: |
| 10598 |
module.add(DSLoadB128(dst=dst, src=src, ds=ds, comment="storeRemap lr")) |
| 10599 |
else: |
| 10600 |
assert 0, "StoreRemap: bad bps!" |
| 10601 |
|
| 10602 |
module.addSpaceLine() |
| 10603 |
|
| 10604 |
|
| 10605 |
addr1 = sgpr("SrdD", 4) |
| 10606 |
packedD1 = kernel["PackedC1IndicesX"] |
| 10607 |
strideD1 = "StrideD%s" % (self.states.indexChars[packedD1[0]]) |
| 10608 |
|
| 10609 |
vTmp = self.vgprPool.checkOut(1, "SR Store temp addr0") |
| 10610 |
addr0 = vgpr(vTmp) |
| 10611 |
|
| 10612 |
isGlc = bool(kernel["NonTemporalD"] & 0x1) |
| 10613 |
isSlc = bool(kernel["NonTemporalD"] & 0x2) |
| 10614 |
isNT = bool(kernel["NonTemporalD"] & 0x4) |
| 10615 |
if kernel["GlobalSplitUAlgorithm"] == "MultipleBufferSingleKernel": |
| 10616 |
isGlc = True |
| 10617 |
isSlc = True |
| 10618 |
|
| 10619 |
if not edge: |
| 10620 |
for rIdx, i in enumerate(range(0, nElements, gwvw)): |
| 10621 |
if i == 0: |
| 10622 |
module.add(VMovB32(dst=addr0, src=vgpr(self.vgprs.storeRemapOffsetCoord1), comment="coord1")) |
| 10623 |
else: |
| 10624 |
currentStep = i//gwvw |
| 10625 |
module.add(VAddU32(dst=addr0, src0=vgpr(self.vgprs.storeRemapOffsetCoord1), src1=(self.storeRemapNCPL * currentStep), comment="coord1 += nColPerLoad")) |
| 10626 |
|
| 10627 |
module.add(VMulLOU32(dst=addr0, src0=addr0, src1=sgpr(strideD1), comment="coord1 offset = coord1 * StrideD")) |
| 10628 |
module.add(VAddLShiftLeftU32(dst=addr0, src0=addr0, src1=vgpr(self.vgprs.storeRemapCoord0), shiftHex=hex(log2(bpe)), comment="global write D address")) |
| 10629 |
|
| 10630 |
dscnt = min((nElements-i)//gwvw - 1, 15) |
| 10631 |
module.add(SWaitCnt(dscnt=dscnt, comment="wait for LDS read")) |
| 10632 |
|
| 10633 |
numStoreInst += 1 |
| 10634 |
|
| 10635 |
module.add(self.chooseGlobalWrite(True, bps, storeRegs[rIdx], rpv, addr0, addr1, 0, glc=isGlc, slc=isSlc, nt=isNT, comment="store D StoreRemapVectorWidth")) |
| 10636 |
|
| 10637 |
else: |
| 10638 |
tmpS23 = tmpS01+self.states.laneSGPRCount |
| 10639 |
coord0 = tmpVgpr |
| 10640 |
coord1 = coord0+1 |
| 10641 |
srvw = kernel["StoreRemapVectorWidth"] |
| 10642 |
edgeVw = min(kernel["AssertFree0ElementMultiple"],kernel["StoreRemapVectorWidth"]) |
| 10643 |
bps = self.states.bpeCexternal * edgeVw |
| 10644 |
rpv = self.states.bpeCexternal / self.states.bpr * edgeVw |
| 10645 |
for rIdx, i in enumerate(range(0, nElements, srvw)): |
| 10646 |
for vi in range (0, srvw, edgeVw): |
| 10647 |
|
| 10648 |
if vi == 0: |
| 10649 |
dscnt = min((nElements-i)//srvw - 1, 15) |
| 10650 |
module.add(SWaitCnt(dscnt=dscnt, comment="wait for LDS read")) |
| 10651 |
|
| 10652 |
sizeBoundary = [0,0] |
| 10653 |
sizeBoundary[0] = \ |
| 10654 |
sgpr("PackedSize0") if len(kernel["PackedC0IndicesX"]) > 1 \ |
| 10655 |
else self.sizeRef(kernel["ProblemType"]["Index0"]) |
| 10656 |
sizeBoundary[1] = \ |
| 10657 |
sgpr("PackedSize1") if len(kernel["PackedC1IndicesX"]) > 1 \ |
| 10658 |
else self.sizeRef(kernel["ProblemType"]["Index1"]) |
| 10659 |
|
| 10660 |
currentStep = i//srvw |
| 10661 |
|
| 10662 |
|
| 10663 |
module.add(VAddU32(dst=vgpr(coord1), src0=vgpr(self.vgprs.storeRemapCoord1), src1=(self.storeRemapNCPL * currentStep), comment="coord1 += nColPerLoad")) |
| 10664 |
module.add(VAddU32(dst=vgpr(coord0), src0=vgpr(self.vgprs.storeRemapCoord0), src1=vi, comment="coord0 += element index of load vector")) |
| 10665 |
module.add(VAddU32(dst=addr0, src0=vgpr(self.vgprs.storeRemapOffsetCoord1), src1=(self.storeRemapNCPL * currentStep), \ |
| 10666 |
comment="offset coord1 += nColPerLoad")) |
| 10667 |
|
| 10668 |
module.add(VCmpLtU32(dst=sgpr(tmpS01,self.states.laneSGPRCount), src0=vgpr(coord0), src1=sizeBoundary[0], comment="coord0 < size0" )) |
| 10669 |
module.add(VCmpLtU32(dst=sgpr(tmpS23,self.states.laneSGPRCount), src0=vgpr(coord1), src1=sizeBoundary[1], comment="coord1 < size1" )) |
| 10670 |
SAndBX = SAndB32 if self.states.kernel["WavefrontSize"] == 32 else SAndB64 |
| 10671 |
module.add(SAndBX( |
| 10672 |
dst=sgpr(tmpS23,self.states.laneSGPRCount), |
| 10673 |
src0=sgpr(tmpS01,self.states.laneSGPRCount), |
| 10674 |
src1=sgpr(tmpS23,self.states.laneSGPRCount), comment="in0 && in1" )) |
| 10675 |
|
| 10676 |
module.add(VMulLOU32(dst=addr0, src0=addr0, src1=sgpr(strideD1), comment="coord1 element offset = coord1 * StrideD")) |
| 10677 |
module.add(VAddLShiftLeftU32(dst=addr0, src0=addr0, src1=vgpr(coord0), shiftHex=hex(log2(bpe)), comment="scale to BPE")) |
| 10678 |
module.add(VCndMaskB32(dst=addr0, src0=-1, src1=addr0, src2=sgpr(tmpS23,self.states.laneSGPRCount), comment="clip if OOB. offset" )) |
| 10679 |
|
| 10680 |
sumIdx = storeRegs[rIdx] + int(vi*rpe) |
| 10681 |
numStoreInst += 1 |
| 10682 |
if bps == 2: |
| 10683 |
module.add(self.chooseGlobalWrite(True, bpe, sumIdx, rpe, addr0, addr1, 0, glc=isGlc, slc=isSlc, nt=isNT, hi16=vi%2, comment="store D StoreRemapVectorWidth")) |
| 10684 |
else: |
| 10685 |
module.add(self.chooseGlobalWrite(True, bps, sumIdx, rpv, addr0, addr1, 0, glc=isGlc, slc=isSlc, nt=isNT, comment="store D StoreRemapVectorWidth")) |
| 10686 |
|
| 10687 |
if bps == 1: |
| 10688 |
module.add(VAShiftRightI32(dst=vgpr("ValuC+%u"%sumIdx), shiftHex=8, src=vgpr("ValuC+%u"%sumIdx), comment=" shift 1 byte" )) |
| 10689 |
|
| 10690 |
module.addSpaceLine() |
| 10691 |
self.vgprPool.checkIn(vTmp) |
| 10692 |
|
| 10693 |
|
| 10694 |
|
| 10695 |
if kernel["MIWaveGroup"][0] > 1: |
| 10696 |
module.add(SBarrier(comment="wait all lds read finished")) |
| 10697 |
|
| 10698 |
return module, numStoreInst |
| 10699 |
|
| 10700 |
|
| 10701 |
|
| 10702 |
|
| 10703 |
def storeRemapComputeStoreVgprs(self, kernel): |
| 10704 |
module = Module("storeRemapComputeStoreVgprs") |
| 10705 |
module.addComment0("Store Remap Local Write address") |
| 10706 |
|
| 10707 |
with self.allocTmpSgpr(2) as tmpSgprInfo: |
| 10708 |
tmpS0 = tmpSgprInfo.idx |
| 10709 |
wgMT1 = tmpS0 + 1 |
| 10710 |
|
| 10711 |
wg0="WorkGroup0" |
| 10712 |
wg1="WorkGroup1" |
| 10713 |
|
| 10714 |
tid0 = self.vgprPool.checkOut(1, "SR coord0") |
| 10715 |
tid1 = self.vgprPool.checkOut(1, "SR coord1") |
| 10716 |
coord1Offset = self.vgprPool.checkOut(1, "SR coord1 offset") |
| 10717 |
storeRemapLW = self.vgprPool.checkOut(1, "SR local write") |
| 10718 |
storeRemapLR = self.vgprPool.checkOut(1, "SR local read") |
| 10719 |
|
| 10720 |
tmpV0 = self.vgprPool.checkOut(5, "tmpV0") |
| 10721 |
waveCoord0 = tmpV1 = tmpV0+1 |
| 10722 |
ldsStride = tmpV0+2 |
| 10723 |
coord0 = tmpV0+3 |
| 10724 |
waveCoord1 = tmpV0+4 |
| 10725 |
|
| 10726 |
gwvw = kernel["StoreRemapVectorWidth"] |
| 10727 |
ldsPad = max(kernel["StoreRemapVectorWidth"],kernel["MIOutputVectorWidth"]) |
| 10728 |
|
| 10729 |
|
| 10730 |
module.add(vectorStaticDivideAndRemainder(tid1, tid0, "Serial", self.states.kernel["WavefrontSize"]*kernel["MIWaveGroup"][0], \ |
| 10731 |
ContinuousRegister(tmpV0, 2))) |
| 10732 |
|
| 10733 |
module.add(VMulLOU32(dst=vgpr(waveCoord1), |
| 10734 |
src0=hex(kernel["MatrixInstN"]), src1=vgpr(tid1), comment="coord1 offset of LDS for each Wave")) |
| 10735 |
module.add(VAndB32(dst=vgpr(tid1), |
| 10736 |
src0=hex(kernel["MatrixInstN"]-1), src1=vgpr("Serial"), comment="coord1 offset of LDS for each thread")) |
| 10737 |
module.add(VAddU32(dst=vgpr(tid1), src0=vgpr(waveCoord1), src1=vgpr(tid1), comment="coord1 offset in MacroTile")) |
| 10738 |
module.add(VMovB32(dst=vgpr(ldsStride), src=hex(kernel["MacroTile0"]+ldsPad), \ |
| 10739 |
comment="lds stride = MT0 + PAD")) |
| 10740 |
module.add(VMulLOU32(dst=vgpr(tmpV0), src0=vgpr(tid1), src1=vgpr(ldsStride), \ |
| 10741 |
comment="lds coord1 offset = Col-id* lds stride")) |
| 10742 |
|
| 10743 |
module.add(vectorStaticDivideAndRemainder(waveCoord0, tid0, tid0, self.states.kernel["WavefrontSize"], ContinuousRegister(tmpV0, 2))) |
| 10744 |
module.add(VLShiftRightB32(dst=vgpr(coord0), |
| 10745 |
shiftHex=hex(log2(kernel["MatrixInstN"])), src=vgpr(tid0), \ |
| 10746 |
comment="tid / matrixInstN")) |
| 10747 |
|
| 10748 |
if kernel["MIOutputVectorWidth"] > 1: |
| 10749 |
module.add(VLShiftLeftB32(dst=vgpr(coord0), shiftHex=hex(log2(kernel["MIOutputVectorWidth"])), src=vgpr(coord0), \ |
| 10750 |
comment="lds coord0 offset *= 4 (each thread hold 4 element)")) |
| 10751 |
|
| 10752 |
module.add(VMadU32U24(dst=vgpr(coord0), src0=(kernel["MatrixInstM"]*kernel["MatrixInstBM"]), src1=vgpr(waveCoord0), src2=vgpr(coord0), \ |
| 10753 |
comment="coord0 += waveCoord0 * wave M shape(blockM*MiM)")) |
| 10754 |
|
| 10755 |
module.add(VAddLShiftLeftU32( |
| 10756 |
dst=vgpr(storeRemapLW), \ |
| 10757 |
src0=vgpr(tmpV0), \ |
| 10758 |
src1=vgpr(coord0), \ |
| 10759 |
shiftHex=sgpr("GSULog2BpeD"), \ |
| 10760 |
comment="local write C address")) |
| 10761 |
|
| 10762 |
module.addSpaceLine() |
| 10763 |
|
| 10764 |
|
| 10765 |
module.addComment0("Store Remap Local Read address") |
| 10766 |
|
| 10767 |
module.add(vectorStaticDivideAndRemainder(tid1, tid0, "Serial", self.states.kernel["WavefrontSize"], \ |
| 10768 |
ContinuousRegister(tmpV0, 2))) |
| 10769 |
module.add(VMulLOU32(dst=vgpr(waveCoord1), |
| 10770 |
src0=hex(kernel["MatrixInstN"]//kernel["MIWaveGroup"][0]), src1=vgpr(tid1), comment="coord1 offset of LDS for each Wave")) |
| 10771 |
|
| 10772 |
nThreadPerCol = kernel["MacroTile0"] // gwvw |
| 10773 |
nColPerLoad = self.states.kernel["WavefrontSize"] // nThreadPerCol |
| 10774 |
self.storeRemapLrOffset = (kernel["MacroTile0"]+ldsPad) * nColPerLoad |
| 10775 |
self.storeRemapNCPL = nColPerLoad |
| 10776 |
|
| 10777 |
module.add(VLShiftRightB32(dst=vgpr(tmpV1),\ |
| 10778 |
shiftHex=hex(log2(nThreadPerCol)), src=vgpr(tid0), \ |
| 10779 |
comment="tid / nThreadPerCol")) |
| 10780 |
module.add(VAddU32(dst=vgpr(coord1Offset), src0=vgpr(waveCoord1), src1=vgpr(tmpV1), comment="coord1 offset in MacroTile")) |
| 10781 |
module.add(VMulLOU32(dst=vgpr(tmpV0), src0=vgpr(coord1Offset), src1=vgpr(ldsStride), \ |
| 10782 |
comment="lds coord1 offset = Col-id* lds stride")) |
| 10783 |
|
| 10784 |
module.add(VAndB32(dst=vgpr(coord0), src0=hex(nThreadPerCol-1), src1=vgpr(tid0), |
| 10785 |
comment="coord0 offset of LDS for each thread")) |
| 10786 |
module.add(VLShiftLeftB32(dst=vgpr(coord0), shiftHex=hex(log2(gwvw)), src=vgpr(coord0), \ |
| 10787 |
comment="lds coord0 offset *= gwvw (each thread hold gwvw element)")) |
| 10788 |
|
| 10789 |
module.add(VAddLShiftLeftU32( |
| 10790 |
dst=vgpr(storeRemapLR), \ |
| 10791 |
src0=vgpr(tmpV0), \ |
| 10792 |
src1=vgpr(coord0), \ |
| 10793 |
shiftHex=sgpr("GSULog2BpeD"), \ |
| 10794 |
comment="local read C address")) |
| 10795 |
module.addSpaceLine() |
| 10796 |
|
| 10797 |
|
| 10798 |
module.addComment0("Store Remap global write coord0 and coord1") |
| 10799 |
module.add(vectorStaticDivideAndRemainder(tid1, tid0, "Serial", self.states.kernel["WavefrontSize"]*kernel["MIWaveGroup"][0], \ |
| 10800 |
ContinuousRegister(tmpV0, 2))) |
| 10801 |
|
| 10802 |
ColsPerBlockShape = kernel["MatrixInstN"] * kernel["MatrixInstBN"] |
| 10803 |
|
| 10804 |
module.add(VMulLOU32(dst=vgpr(waveCoord1), src0=hex(ColsPerBlockShape), src1=vgpr(tid1), |
| 10805 |
comment="coord1 offset of global memory for each Wave")) |
| 10806 |
|
| 10807 |
module.add(vectorStaticDivideAndRemainder(tid1, tid0, tid0, self.states.kernel["WavefrontSize"], \ |
| 10808 |
ContinuousRegister(tmpV0, 2))) |
| 10809 |
module.add(VMadU32U24(dst=vgpr(waveCoord1), src0=(kernel["MatrixInstN"]//kernel["MIWaveGroup"][0]), src1=vgpr(tid1), src2=vgpr(waveCoord1), \ |
| 10810 |
comment="waveCoord1 += waveCoord0 * MiN / WaveGroupM")) |
| 10811 |
|
| 10812 |
module.add(VLShiftRightB32(dst=vgpr(tmpV1), shiftHex=hex(log2(nThreadPerCol)), src=vgpr(tid0), \ |
| 10813 |
comment="tid / nThreadPerCol")) |
| 10814 |
|
| 10815 |
module.add(VAddU32(dst=vgpr(coord1Offset), src0=vgpr(waveCoord1), src1=vgpr(tmpV1), comment="coord1 offset in MacroTile")) |
| 10816 |
|
| 10817 |
module.add(SMulI32(dst=sgpr(tmpS0), src0=hex(kernel["MacroTile0"]), src1=sgpr(wg0), comment="%s = wg0*MT0"%sgpr(tmpS0))) |
| 10818 |
|
| 10819 |
module.add(VAddCOU32(dst=vgpr(tid0), dst1=VCC(), src0=sgpr(tmpS0), src1=vgpr(coord0), comment="coord0 = coord0 + wg0 * MT0")) |
| 10820 |
|
| 10821 |
module.add(SMulI32(dst=sgpr(wgMT1), src0="MT1", src1=sgpr(wg1), comment="<- wg1*MT1")) |
| 10822 |
module.add(VAddCOU32(dst=vgpr(tid1), dst1=VCC(), src0=sgpr(wgMT1), src1=vgpr(coord1Offset), comment="coord1 = tid1*VW + wg1*MT1")) |
| 10823 |
|
| 10824 |
module.addSpaceLine() |
| 10825 |
|
| 10826 |
module.add(self._syncThreads(kernel, "StoreRemap Start")) |
| 10827 |
|
| 10828 |
self.vgprs.storeRemapLW = storeRemapLW |
| 10829 |
self.vgprs.storeRemapLR = storeRemapLR |
| 10830 |
self.vgprs.storeRemapCoord0 = tid0 |
| 10831 |
self.vgprs.storeRemapCoord1 = tid1 |
| 10832 |
self.vgprs.storeRemapOffsetCoord1 = coord1Offset |
| 10833 |
|
| 10834 |
self.vgprPool.checkIn(tmpV0) |
| 10835 |
|
| 10836 |
nElements = kernel["MacroTile0"]*kernel["MatrixInstN"]//kernel["MIWaveGroup"][0]//self.states.kernel["WavefrontSize"] |
| 10837 |
rpe = self.states.bpeCexternal / self.states.bpr |
| 10838 |
rpv = rpe * gwvw |
| 10839 |
self.vgprs.storeRemapAS = [] |
| 10840 |
for i in range(0, nElements, gwvw): |
| 10841 |
self.vgprs.storeRemapAS.append(self.vgprPool.checkOutAligned(int(rpv), int(rpv), "store element d")) |
| 10842 |
return module |
| 10843 |
|
| 10844 |
|
| 10845 |
|
| 10846 |
|
| 10847 |
|
| 10848 |
|
| 10849 |
|
| 10850 |
|
| 10851 |
|
| 10852 |
|
| 10853 |
|
| 10854 |
def notLocalSplitUGlobalWrite(self, kernel, tPA, tPB): |
| 10855 |
if not self.do["PostLoop"]: return "" |
| 10856 |
elements = [[] for y in range(2)] |
| 10857 |
elements_1 = [[] for y in range(2)] |
| 10858 |
|
| 10859 |
(fullVw, elements[False], fullVw_1, elements_1[False]) = self.notLocalFullTileElements(kernel, False) |
| 10860 |
(edgeVw, elements[True], edgeVw_1, elements_1[True] ) = self.notLocalFullTileElements(kernel, True) |
| 10861 |
|
| 10862 |
|
| 10863 |
|
| 10864 |
vectorWidths = [fullVw, edgeVw] |
| 10865 |
vectorWidths_1 = [fullVw, edgeVw_1] |
| 10866 |
|
| 10867 |
noGSUBranch = (kernel["GlobalSplitU"] == 0 and kernel["StreamK"] != 3) |
| 10868 |
module = Module("notLocalSplitUGlobalWrite") |
| 10869 |
module.add(self.globalWriteElements(kernel, tPA, tPB, vectorWidths, vectorWidths_1, elements, elements_1, noGSUBranch=noGSUBranch)) |
| 10870 |
|
| 10871 |
self.cleanupGlobalWrite(kernel) |
| 10872 |
|
| 10873 |
return module |
| 10874 |
|
| 10875 |
|
| 10876 |
|
| 10877 |
|
| 10878 |
def localSplitUGlobalWrite(self, kernel, tPA, tPB): |
| 10879 |
if not self.do["PostLoop"]: return "" |
| 10880 |
|
| 10881 |
elements_0 = [[] for y in range(2)] |
| 10882 |
elements_1 = [[] for y in range(2)] |
| 10883 |
elements_f0 = [[] for y in range(2)] |
| 10884 |
elements_f1 = [[] for y in range(2)] |
| 10885 |
(fullVw, elements_0[False], fullVw_1, elements_1[False]) = self.notLocalFullTileElements(kernel, False) |
| 10886 |
(edgeVw, elements_0[True], edgeVw_1, elements_1[True] ) = self.notLocalFullTileElements(kernel, True) |
| 10887 |
edgeScaled_0 = len(elements_0[True]) // len(elements_1[False]) |
| 10888 |
edgeScaled_1 = len(elements_1[True]) // len(elements_1[False]) |
| 10889 |
noEgScaled_0 = len(elements_0[False]) // len(elements_1[False]) |
| 10890 |
|
| 10891 |
for i in range(0, len(elements_1[False])): |
| 10892 |
element = elements_1[False][i] |
| 10893 |
if element in self.LSUelementsPerLSUWave: |
| 10894 |
elements_f1[False].append(element) |
| 10895 |
for j in range(0, edgeScaled_0): |
| 10896 |
|
| 10897 |
element = elements_0[True][i*edgeScaled_0+j] |
| 10898 |
elements_f0[True].append(element) |
| 10899 |
for j in range(0, edgeScaled_1): |
| 10900 |
|
| 10901 |
element = elements_1[True][i*edgeScaled_1+j] |
| 10902 |
elements_f1[True].append(element) |
| 10903 |
for j in range(0, noEgScaled_0): |
| 10904 |
|
| 10905 |
element = elements_0[False][i*noEgScaled_0+j] |
| 10906 |
elements_f0[False].append(element) |
| 10907 |
|
| 10908 |
vectorWidths = [fullVw, edgeVw] |
| 10909 |
vectorWidths_1 = [fullVw_1, edgeVw_1] |
| 10910 |
|
| 10911 |
noGSUBranch = (kernel["GlobalSplitU"] == 0 and kernel["StreamK"] != 3) |
| 10912 |
module = Module("localSplitUGlobalWrite") |
| 10913 |
module.add(self.globalWriteElements(kernel, tPA, tPB, vectorWidths, vectorWidths_1, elements_f0, elements_f1, noGSUBranch=noGSUBranch)) |
| 10914 |
self.cleanupGlobalWrite(kernel) |
| 10915 |
self.vgprPool.checkIn(self.accVgprLdsReduction) |
| 10916 |
return module |
| 10917 |
|
| 10918 |
|
| 10919 |
|
| 10920 |
|
| 10921 |
|
| 10922 |
|
| 10923 |
def checkIsBetaZero(self, kernel, tmpSgprInfo, betaLabel, isLongBranch=False, placeHolder=None, posNeg: int=0): |
| 10924 |
module = Module("checkIsBetaZero label %s"%betaLabel) |
| 10925 |
assert(isinstance(betaLabel, Label)) |
| 10926 |
betaLabelName = betaLabel.getLabelName() |
| 10927 |
if kernel["ProblemType"]["UseBeta"]: |
| 10928 |
if self.states.bpeCinternal <= self.states.bpr: |
| 10929 |
|
| 10930 |
module.add(self.getSCMPKInstruction("EQU32", "Beta", 0, comment="Beta == 0")) |
| 10931 |
else: |
| 10932 |
module.add(SMovB32(dst=sgpr(tmpSgprInfo.idx), src=sgpr("Beta+0"), comment="tmp = Beta[0]")) |
| 10933 |
for i in range(1, self.states.bpeCinternal//self.states.bpr): |
| 10934 |
module.add(SOrB32(dst=sgpr(tmpSgprInfo.idx), src0=sgpr("Beta+%u"%i), src1=sgpr(tmpSgprInfo.idx), comment="tmp |= Beta[%u] " % i)) |
| 10935 |
|
| 10936 |
module.add(self.getSCMPKInstruction("EQU32", tmpSgprInfo.idx, 0, comment="Beta == 0")) |
| 10937 |
if placeHolder == None: |
| 10938 |
if isLongBranch: |
| 10939 |
module.add(self.longBranchScc0(betaLabel, posNeg, tmpSgprInfo)) |
| 10940 |
else: |
| 10941 |
module.add(SCBranchSCC0(labelName=betaLabelName, comment="Branch if Beta is not zero")) |
| 10942 |
else: |
| 10943 |
placeHolderModule = Module(placeHolder) |
| 10944 |
module.add(placeHolderModule) |
| 10945 |
module.addSpaceLine() |
| 10946 |
return module |
| 10947 |
|
| 10948 |
|
| 10949 |
|
| 10950 |
|
| 10951 |
|
| 10952 |
|
| 10953 |
def checkIsEdge(self, kernel, tmpSgprInfo, isEdgeTargetMT0, isEdgeTargetMT1, isLongBranch=False, placeHolder=None): |
| 10954 |
assert(isinstance(isEdgeTargetMT0, Label) and isinstance(isEdgeTargetMT1, Label)) |
| 10955 |
isEdgeTargetMT0Label = isEdgeTargetMT0.getLabelName() |
| 10956 |
isEdgeTargetMT1Label = isEdgeTargetMT1.getLabelName() |
| 10957 |
module = Module("checkIsEdge") |
| 10958 |
tmpS0 = tmpSgprInfo.idx |
| 10959 |
tmpS1 = tmpS0 + 1 |
| 10960 |
tmpS23 = tmpS1 + 1 |
| 10961 |
|
| 10962 |
wg0="WorkGroup0" |
| 10963 |
wg1="WorkGroup1" |
| 10964 |
|
| 10965 |
|
| 10966 |
|
| 10967 |
|
| 10968 |
sizeBoundary = [0,0] |
| 10969 |
sizeBoundary[0] = \ |
| 10970 |
sgpr("PackedSize0") if len(kernel["PackedC0IndicesX"]) > 1 \ |
| 10971 |
else self.sizeRef(kernel["ProblemType"]["Index0"]) |
| 10972 |
sizeBoundary[1] = \ |
| 10973 |
sgpr("PackedSize1") if len(kernel["PackedC1IndicesX"]) > 1 \ |
| 10974 |
else self.sizeRef(kernel["ProblemType"]["Index1"]) |
| 10975 |
|
| 10976 |
module.add(scalarStaticDivideAndRemainder(tmpS1, tmpS0, sizeBoundary[0], kernel["MacroTile0"], \ |
| 10977 |
ContinuousRegister(tmpS23, 2), 2)) |
| 10978 |
|
| 10979 |
module.add(SAddU32(dst=sgpr(tmpS1), src0=hex(-1), src1=sgpr("NumWorkGroups0"))) |
| 10980 |
module.add(SCmpGeU32(src0=sgpr(wg0), src1=sgpr(tmpS1), comment="wg0 >= nwg0-1 ?")) |
| 10981 |
module.add(SCSelectB32(dst=sgpr(tmpS0), src0=sgpr(tmpS0), src1=0, comment="set rMT0")) |
| 10982 |
|
| 10983 |
|
| 10984 |
|
| 10985 |
if self.do["EdgeWrite"]: |
| 10986 |
|
| 10987 |
module.add(self.getSCMPKInstruction("GTU32", tmpS0, 0, comment="rMT0 > 0")) |
| 10988 |
if self.db["ForceEdgeStores"]: |
| 10989 |
module.add(SCmpEQU32(src0=sgpr(tmpS0), src1=sgpr(tmpS0), comment="ForceEdgeStores!")) |
| 10990 |
if placeHolder == None: |
| 10991 |
if isLongBranch: |
| 10992 |
module.add(self.longBranchScc1(isEdgeTargetMT0, posNeg=1, tmpSgprInfo=tmpSgprInfo, comment="jump if edges required")) |
| 10993 |
else: |
| 10994 |
module.add(SCBranchSCC1(labelName=isEdgeTargetMT0Label, comment="jump if edges required")) |
| 10995 |
else: |
| 10996 |
placeHolderModule = Module(placeHolder) |
| 10997 |
placeHolderModule.addComment1("jump if edges required") |
| 10998 |
module.add(placeHolderModule) |
| 10999 |
|
| 11000 |
|
| 11001 |
|
| 11002 |
|
| 11003 |
|
| 11004 |
|
| 11005 |
|
| 11006 |
module.add(scalarStaticDivideAndRemainder(tmpS1, tmpS0, sizeBoundary[1], kernel["MacroTile1"], \ |
| 11007 |
ContinuousRegister(tmpS23, 2), 2)) |
| 11008 |
|
| 11009 |
|
| 11010 |
|
| 11011 |
module.add(SAddU32(dst=sgpr(tmpS1), src0=hex(-1), src1=sgpr("NumWorkGroups1"))) |
| 11012 |
module.add(SCmpGeU32(src0=sgpr(wg1), src1=sgpr(tmpS1), comment="wg1 >= nwg1-1")) |
| 11013 |
module.add(SCSelectB32(dst=sgpr(tmpS0), src0=sgpr(tmpS0), src1=0, comment="set rMT1")) |
| 11014 |
|
| 11015 |
|
| 11016 |
if self.do["EdgeWrite"]: |
| 11017 |
|
| 11018 |
module.add(self.getSCMPKInstruction("GTU32", tmpS0, 0, comment="rMT1 > 0")) |
| 11019 |
if placeHolder == None: |
| 11020 |
if isLongBranch: |
| 11021 |
module.add(self.longBranchScc1(isEdgeTargetMT1, posNeg=1, tmpSgprInfo=tmpSgprInfo, comment="jump if edges required")) |
| 11022 |
else: |
| 11023 |
module.add(SCBranchSCC1(labelName=isEdgeTargetMT1Label, comment="jump if edges required")) |
| 11024 |
else: |
| 11025 |
placeHolderModule = Module(placeHolder) |
| 11026 |
placeHolderModule.addComment1("jump if edges required") |
| 11027 |
module.add(placeHolderModule) |
| 11028 |
return module |
| 11029 |
|
| 11030 |
|
| 11031 |
|
| 11032 |
|
| 11033 |
|
| 11034 |
|
| 11035 |
def checkIsFactorDimZero(self, kernel, tmpSgprInfo, factorDimLabel, isLongBranch=False, posNeg: int=0): |
| 11036 |
module = Module("checkIsFactorDimZero label %s"%factorDimLabel) |
| 11037 |
assert(isinstance(factorDimLabel, Label)) |
| 11038 |
factorDimLabelName = factorDimLabel.getLabelName() |
| 11039 |
if kernel["ProblemType"]["UseBias"] or kernel["ProblemType"]["UseScaleAlphaVec"]: |
| 11040 |
if self.states.bpeCinternal <= self.states.bpr: |
| 11041 |
module.add(self.getSCMPKInstruction("EQU32", "FactorDim", 0, comment="FactorDim == 0")) |
| 11042 |
else: |
| 11043 |
module.add(SMovB32(dst=sgpr(tmpSgprInfo.idx), src=sgpr("FactorDim+0"), comment="tmp = FactorDim[0]")) |
| 11044 |
for i in range(1, self.states.bpeCinternal//self.states.bpr): |
| 11045 |
module.add(SOrB32(dst=sgpr(tmpSgprInfo.idx), src0=sgpr("FactorDim+%u"%i), src1=sgpr(tmpSgprInfo.idx), comment="tmp |= FactorDim[%u] " % i)) |
| 11046 |
module.add(self.getSCMPKInstruction("EQU32", tmpSgprInfo.idx, 0, comment="FactorDim == 0")) |
| 11047 |
if isLongBranch: |
| 11048 |
module.add(self.longBranchScc0(factorDimLabel, posNeg, tmpSgprInfo)) |
| 11049 |
else: |
| 11050 |
module.add(SCBranchSCC0(labelName=factorDimLabelName, comment="Branch if FactorDim is not zero")) |
| 11051 |
module.addSpaceLine() |
| 11052 |
return module |
| 11053 |
|
| 11054 |
|
| 11055 |
|
| 11056 |
|
| 11057 |
class BF16CVTVgprStruct(NamedTuple): |
| 11058 |
vgprBf16Temp: int = -1 |
| 11059 |
vgprBf16Mask: int = -1 |
| 11060 |
vgprFp32Nan: int = -1 |
| 11061 |
vgprBf16Inc: int = -1 |
| 11062 |
|
| 11063 |
class FP8CVTVgprStruct(NamedTuple): |
| 11064 |
vgprFp8NanInf: int = -1 |
| 11065 |
vgprFp8Temp: int = -1 |
| 11066 |
vgprFp8Min: int = -1 |
| 11067 |
vgprFp8Max: int = -1 |
| 11068 |
|
| 11069 |
class BF8CVTVgprStruct(NamedTuple): |
| 11070 |
vgprBF8NanInf: int = -1 |
| 11071 |
vgprBF8Temp: int = -1 |
| 11072 |
vgprBF8Min: int = -1 |
| 11073 |
vgprBF8Max: int = -1 |
| 11074 |
|
| 11075 |
class I8CVTVgprStruct(NamedTuple): |
| 11076 |
vgprI8Temp0: int = -1 |
| 11077 |
vgprI8Temp1: int = -1 |
| 11078 |
vgprI8Mask0: int = -1 |
| 11079 |
vgprI8Mask1: int = -1 |
| 11080 |
|
| 11081 |
class ActivationSetPCStruct(NamedTuple): |
| 11082 |
sgprOffsetActivation: int = -1 |
| 11083 |
sgprOffsetBack: int = -1 |
| 11084 |
vgprActCopy: int = -1 |
| 11085 |
|
| 11086 |
def globalWriteElements(self, kernel, tPA, tPB, vectorWidths_2, vectorWidths_1, elements_2, elements_1, |
| 11087 |
noGSUBranch=False, |
| 11088 |
applyAlpha=True, |
| 11089 |
betas=None, |
| 11090 |
edges=None): |
| 11091 |
if not self.do["PostLoop"]: return Module("GlobalWriteElements (Empty)") |
| 11092 |
module = Module("GlobalWriteElements") |
| 11093 |
|
| 11094 |
module.addComment2("Global Write Elements") |
| 11095 |
if kernel["ProblemType"]["OutputAmaxD"]: |
| 11096 |
module.add(VMovB32(dst=vgpr("AmaxOut"), src="0")) |
| 11097 |
if self.states.numStoreSgprToLoad or self.states.numStoreSgprToLoad2: |
| 11098 |
module.add(SWaitCnt(kmcnt=0, comment="wait for %u bytes of kern args."%((self.states.numStoreSgprToLoad+self.states.numStoreSgprToLoad2) * 4))) |
| 11099 |
|
| 11100 |
skBackup = kernel["StreamK"] |
| 11101 |
gsuBackup = kernel["GlobalSplitU"] |
| 11102 |
gsuAccumBackup = kernel["_GlobalAccumulation"] |
| 11103 |
bpeCexternalBackup = self.states.bpeCexternal |
| 11104 |
afcBackup = kernel["ActivationFuncCall"] |
| 11105 |
useBiasBackup = self.states.useBias |
| 11106 |
betasBackup = betas |
| 11107 |
edgesBackup = edges |
| 11108 |
gsuLimit = 1 if noGSUBranch or self.debugConfig.splitGSU else 2 |
| 11109 |
if gsuLimit > 1: |
| 11110 |
gsuLabel = Label(label=self.labels.getNameInc("GSU"), comment="") |
| 11111 |
if kernel["StreamK"]: |
| 11112 |
module.add(SCmpEQU64(src0=sgpr("AddressFlags", 2), src1=hex(0), comment="Check for synchronizer")) |
| 11113 |
module.add(SCBranchSCC0(labelName=gsuLabel.getLabelName(), comment="Branch to stream-k store code")) |
| 11114 |
module.add(SCmpEQU32(src0=sgpr("skTiles"), src1=1, comment="split == 1 ?")) |
| 11115 |
|
| 11116 |
module.add(SCBranchSCC1(labelName=gsuLabel.getLabelName(), comment="branch if split == 1")) |
| 11117 |
else: |
| 11118 |
skipSrdTDLabel = Label(label=self.labels.getNameInc("Skip_SrdTD"), comment="") |
| 11119 |
with self.allocTmpSgpr(1) as tmpSgprGSU: |
| 11120 |
module.add(SAndB32(dst=sgpr(tmpSgprGSU.idx), src0=sgpr("GSU"), src1=hex(0x3FFF), comment="Restore GSU")) |
| 11121 |
module.add(SCmpEQU32(src0=sgpr(tmpSgprGSU.idx), src1=1, comment="GSU == 1 ?")) |
| 11122 |
if (kernel["_GlobalAccumulation"] == 'MultipleBufferSingleKernel'): |
| 11123 |
module.add(self.longBranchScc1(label=gsuLabel, posNeg=1, comment="long branch if GSU == 1")) |
| 11124 |
|
| 11125 |
module.add(self.SrdTDInit(kernel)) |
| 11126 |
module.add(skipSrdTDLabel) |
| 11127 |
else: |
| 11128 |
module.add(SCBranchSCC1(labelName=gsuLabel.getLabelName(), comment="branch if GSU == 1")) |
| 11129 |
|
| 11130 |
gsuLimitRange = range(0, gsuLimit) |
| 11131 |
|
| 11132 |
|
| 11133 |
|
| 11134 |
|
| 11135 |
for gsuLimitIdx in gsuLimitRange: |
| 11136 |
if gsuLimit > 1: |
| 11137 |
betas = betasBackup |
| 11138 |
edges = edgesBackup |
| 11139 |
if gsuLimitIdx == 0: |
| 11140 |
self.states.bpeCexternal = self.states.bpeCinternal |
| 11141 |
if (kernel["_GlobalAccumulation"] != 'MultipleBufferSingleKernel'): |
| 11142 |
self.states.useBias = self.states.useBias if self.states.useBias == DataDirection.WRITE else DataDirection.NONE |
| 11143 |
if self.states.useBias == DataDirection.WRITE and kernel["ProblemType"]["BiasSrc"] == "D": |
| 11144 |
self.states.useBias = DataDirection.NONE |
| 11145 |
if (kernel["_GlobalAccumulation"] != 'MultipleBufferSingleKernel'): |
| 11146 |
kernel["LdsOffsetBias"] = kernel["LdsOffsetBiasGSU"] |
| 11147 |
kernel["ActivationFuncCall"] = False |
| 11148 |
kernel["GlobalSplitU"] = 2 |
| 11149 |
kernel["_GlobalAccumulation"] = kernel["_GlobalAccumulation"] |
| 11150 |
if kernel["StreamK"]: |
| 11151 |
kernel["StreamK"] = 0 |
| 11152 |
kernel["_GlobalAccumulation"] = "MultipleBuffer" |
| 11153 |
vectorWidths = vectorWidths_2 |
| 11154 |
elements = elements_2 |
| 11155 |
else: |
| 11156 |
module.add(gsuLabel) |
| 11157 |
self.states.bpeCexternal = self.states.bpeCexternalGSU1 |
| 11158 |
self.states.useBias = useBiasBackup |
| 11159 |
kernel["LdsOffsetBias"] = kernel["LdsOffsetBiasNonGSU"] |
| 11160 |
kernel["ActivationFuncCall"] = afcBackup |
| 11161 |
kernel["GlobalSplitU"] = 1 |
| 11162 |
kernel["_GlobalAccumulation"] = None |
| 11163 |
kernel["StreamK"] = skBackup |
| 11164 |
if kernel["StreamK"]: |
| 11165 |
kernel["GlobalSplitU"] = gsuBackup |
| 11166 |
kernel["_GlobalAccumulation"] = gsuAccumBackup |
| 11167 |
vectorWidths = vectorWidths_1 |
| 11168 |
elements = elements_1 |
| 11169 |
else: |
| 11170 |
if (kernel["GlobalSplitU"] > 1 or kernel["GlobalSplitU"] == -1) and (kernel["_GlobalAccumulation"] != 'MultipleBufferSingleKernel'): |
| 11171 |
self.states.useBias = self.states.useBias if self.states.useBias == DataDirection.WRITE else DataDirection.NONE |
| 11172 |
kernel["LdsOffsetBias"] = kernel["LdsOffsetBiasGSU"] |
| 11173 |
kernel["ActivationFuncCall"] = False |
| 11174 |
vectorWidths = vectorWidths_2 |
| 11175 |
elements = elements_2 |
| 11176 |
else: |
| 11177 |
kernel["LdsOffsetBias"] = kernel["LdsOffsetBiasNonGSU"] |
| 11178 |
vectorWidths = vectorWidths_1 |
| 11179 |
elements = elements_1 |
| 11180 |
''' |
| 11181 |
Post process for loop |
| 11182 |
''' |
| 11183 |
ssslist = [] |
| 11184 |
useSize = [] |
| 11185 |
|
| 11186 |
if gsuLimit > 1 and gsuLimitIdx > 0: |
| 11187 |
if kernel["ProblemType"]["UseScaleAB"]: |
| 11188 |
if not self.states.preloadScaleA: |
| 11189 |
module.add(self.setSgprToInUseState("AddressScaleA")) |
| 11190 |
if (kernel["ProblemType"]["UseScaleAB"] == "Vector"): |
| 11191 |
module.add(self.setSgprToInUseState("SrdScaleA")) |
| 11192 |
if not self.states.preloadScaleB: |
| 11193 |
module.add(self.setSgprToInUseState("AddressScaleB")) |
| 11194 |
if (kernel["ProblemType"]["UseScaleAB"] == "Vector"): |
| 11195 |
module.add(self.setSgprToInUseState("SrdScaleB")) |
| 11196 |
if kernel["ProblemType"]["UseScaleAlphaVec"]: |
| 11197 |
module.add(self.setSgprToInUseState("AddressScaleAlphaVec")) |
| 11198 |
module.add(self.setSgprToInUseState("SrdScaleAlphaVec")) |
| 11199 |
|
| 11200 |
isSingleKernel = ((kernel["GlobalSplitU"] == 1 or kernel["GlobalSplitU"] == -1) or kernel["_GlobalAccumulation"] == 'MultipleBufferSingleKernel') or kernel["StreamK"] > 0 |
| 11201 |
|
| 11202 |
if kernel["ProblemType"]["UseScaleAB"] == "Scalar" and \ |
| 11203 |
isSingleKernel and \ |
| 11204 |
((kernel["ProblemType"]["DataTypeA"].numRegisters() <= kernel["ProblemType"]["DataType"].numRegisters()) or \ |
| 11205 |
(kernel["ProblemType"]["DataTypeB"].numRegisters() <= kernel["ProblemType"]["DataType"].numRegisters())): |
| 11206 |
assert(kernel["ProblemType"]["ComputeDataType"].isSingle()) |
| 11207 |
sgprScaleA = self.sgprPool.checkOut(1, preventOverflow=False) |
| 11208 |
sgprScaleB = self.sgprPool.checkOut(1, preventOverflow=False) |
| 11209 |
for i,name in enumerate(['A','B']): |
| 11210 |
if kernel["ProblemType"]["DataType%s"%name].numRegisters() <= kernel["ProblemType"]["DataType"].numRegisters(): |
| 11211 |
sgprScale = sgprScaleA if name == 'A' else sgprScaleB |
| 11212 |
module.add(SMovB32(dst=sgpr(sgprScale), src=1.0 , comment="init as 1" )) |
| 11213 |
label = Label(self.labels.getNameInc("Scale%sValid"%name), "") |
| 11214 |
module.add(BranchIfZero("AddressScale%s"%name, DataType('int64').toEnum(), -1, int(kernel["WavefrontSize"]/32), label, kernel["WavefrontSize"])) |
| 11215 |
|
| 11216 |
module.add(SLoadB32(dst=sgpr(sgprScale), base=sgpr("AddressScale%s"%name,2), soffset=0, comment="load scale%s"%name)) |
| 11217 |
module.add(label) |
| 11218 |
|
| 11219 |
|
| 11220 |
if kernel["ProblemType"]["UseScaleCD"] and ((kernel["GlobalSplitU"] == 1 or kernel["GlobalSplitU"] == -1) or kernel["StreamK"] > 0): |
| 11221 |
module.add(SMovB32(dst=sgpr("ScaleD"), src=1.0 , comment="init as 1" )) |
| 11222 |
module.add(SMovB32(dst=sgpr("ScaleD+1"), src=1.0 , comment="init as 1" )) |
| 11223 |
label = Label(self.labels.getNameInc("ScaleDValid"), "") |
| 11224 |
module.add(BranchIfZero("AddressScaleD", DataType('int64').toEnum(), -1, int(kernel["WavefrontSize"]/32), label, kernel["WavefrontSize"])) |
| 11225 |
|
| 11226 |
module.add(SLoadB32(dst=sgpr("ScaleD"), base=sgpr("AddressScaleD",2), soffset=0, comment="load scaleD")) |
| 11227 |
module.add(label) |
| 11228 |
sgprScaleC = self.sgprPool.checkOut(1, preventOverflow=False) |
| 11229 |
module.add(SMovB32(dst=sgpr(sgprScaleC), src=1.0 , comment="init as 1" )) |
| 11230 |
label = Label(self.labels.getNameInc("ScaleCValid"), "") |
| 11231 |
module.add(BranchIfZero("AddressScaleC", DataType('int64').toEnum(), -1, int(kernel["WavefrontSize"]/32), label, kernel["WavefrontSize"])) |
| 11232 |
|
| 11233 |
module.add(SLoadB32(dst=sgpr(sgprScaleC), base=sgpr("AddressScaleC",2), soffset=0, comment="load scaleC")) |
| 11234 |
module.add(label) |
| 11235 |
|
| 11236 |
factorDims = [0] |
| 11237 |
if self.states.FactorDim == 3: |
| 11238 |
factorDims.append(1) |
| 11239 |
elif self.states.FactorDim == 2: |
| 11240 |
factorDims = [1] |
| 11241 |
|
| 11242 |
vectorDataTypes = VectorDataTypes() |
| 11243 |
if (kernel["ProblemType"]["UseScaleAlphaVec"]) and isSingleKernel: |
| 11244 |
labelStr = self.labels.getNameInc("ScaleAlphaVec") |
| 11245 |
if self.states.FactorDim == 3: |
| 11246 |
with self.allocTmpSgpr(1,1) as tmpSgprRes: |
| 11247 |
tmpSgpr = tmpSgprRes.idx |
| 11248 |
module.add(SCmpKEQU32(src=sgpr("FactorDim"), simm16=0, comment="FactorDim == 0")) |
| 11249 |
module.add(SCSelectB32(dst=sgpr(tmpSgpr), src0=sgpr("SizeI"), src1=sgpr("SizeJ"))) |
| 11250 |
module.add(allocPostLoopSrdSuppress("ScaleAlphaVec", labelStr, sgprLength=sgpr(tmpSgpr))) |
| 11251 |
elif self.states.FactorDim == 2: |
| 11252 |
module.add(allocPostLoopSrdSuppress("ScaleAlphaVec", labelStr, sgprLength=sgpr("SizeJ"))) |
| 11253 |
else: |
| 11254 |
module.add(allocPostLoopSrdSuppress("ScaleAlphaVec", labelStr, sgprLength=sgpr("SizeI"))) |
| 11255 |
module.add(SMulI32(dst=sgpr("SrdScaleAlphaVec+2"), src0=hex(self.states.bpeCinternal), src1=sgpr("SrdScaleAlphaVec+2"), comment="ScaleAlphaVec scaled by BPE")) |
| 11256 |
for d in range(len(factorDims)): |
| 11257 |
vectorDataTypes.scaleAlpha(d).dataType = kernel["ProblemType"]["ComputeDataType"] |
| 11258 |
|
| 11259 |
|
| 11260 |
|
| 11261 |
if ((kernel["ProblemType"]["UseScaleAB"] == "Vector")) and isSingleKernel: |
| 11262 |
labelStrA = self.labels.getNameInc("ScaleAVec") |
| 11263 |
labelStrB = self.labels.getNameInc("ScaleBVec") |
| 11264 |
module.add(allocPostLoopSrdSuppress("ScaleA", labelStrA, sgprLength=sgpr("SizeI"))) |
| 11265 |
module.add(allocPostLoopSrdSuppress("ScaleB", labelStrB, sgprLength=sgpr("SizeJ"))) |
| 11266 |
module.add(SMulI32(dst=sgpr("SrdScaleA+2"), src0=hex(self.states.bpeCinternal), src1=sgpr("SrdScaleA+2"), comment="ScaleAVec scaled by BPE")) |
| 11267 |
module.add(SMulI32(dst=sgpr("SrdScaleB+2"), src0=hex(self.states.bpeCinternal), src1=sgpr("SrdScaleB+2"), comment="ScaleBVec scaled by BPE")) |
| 11268 |
vectorDataTypes.scaleA.dataType = kernel["ProblemType"]["ComputeDataType"] |
| 11269 |
vectorDataTypes.scaleB.dataType = kernel["ProblemType"]["ComputeDataType"] |
| 11270 |
|
| 11271 |
factorDim0Label = Label(self.labels.getNameInc("Load_FactorDim_0"), "") |
| 11272 |
factorDim1Label = Label(self.labels.getNameInc("Load_FactorDim_1"), "") |
| 11273 |
|
| 11274 |
|
| 11275 |
isLdsLoaded = False |
| 11276 |
if self.states.useBias == DataDirection.READ and isSingleKernel: |
| 11277 |
|
| 11278 |
labelStr = self.labels.getNameInc("Bias") |
| 11279 |
with self.allocTmpSgpr(1,1) as tmpSgprRes: |
| 11280 |
set_bs_label = Label(self.labels.getNameInc("Dont_Set_BiasStride"), "") |
| 11281 |
tmpSgpr = tmpSgprRes.idx |
| 11282 |
module.add(SAddU32(dst=sgpr(tmpSgpr), src0=sgpr("WorkGroup2"), src1=hex(1))) |
| 11283 |
module.add(SMulI32(dst=sgpr(tmpSgpr), src0=sgpr("BiasStride"), src1=sgpr(tmpSgpr), comment="stride * (wg+1)")) |
| 11284 |
module.add(SCmpEQU32(sgpr(tmpSgpr), 0, comment="bias stride = 0?")) |
| 11285 |
if self.states.FactorDim == 3: |
| 11286 |
module.add(SCBranchSCC0(set_bs_label.getLabelName())) |
| 11287 |
module.add(self.getSCMPKInstruction("EQU32", "FactorDim", 0, comment="FactorDim == 0")) |
| 11288 |
module.add(SCSelectB32(dst=sgpr(tmpSgpr), src0=sgpr("SizeI"), src1=sgpr("SizeJ"))) |
| 11289 |
module.add(set_bs_label) |
| 11290 |
elif self.states.FactorDim == 2: |
| 11291 |
module.add(SCSelectB32(dst=sgpr(tmpSgpr), src0=sgpr("SizeJ"), src1=sgpr(tmpSgpr))) |
| 11292 |
else: |
| 11293 |
module.add(SCSelectB32(dst=sgpr(tmpSgpr), src0=sgpr("SizeI"), src1=sgpr(tmpSgpr))) |
| 11294 |
|
| 11295 |
module.add(allocPostLoopSrdSuppress("Bias", labelStr, sgprLength=sgpr(tmpSgpr))) |
| 11296 |
|
| 11297 |
loadBiasEndLabel = Label(self.labels.getNameInc("Load_Bias_End"), "") |
| 11298 |
if self.states.FactorDim == 3: |
| 11299 |
module.add(factorDim0Label) |
| 11300 |
module.add(self.getSCMPKInstruction("LGU32", "FactorDim", 0, comment="FactorDim != 0")) |
| 11301 |
module.add(SCBranchSCC1(factorDim1Label.getLabelName(), "Branch if true")) |
| 11302 |
|
| 11303 |
for d in range(len(factorDims)): |
| 11304 |
|
| 11305 |
vectorDataTypes.bias(d).dataType = kernel["ProblemType"]["BiasDataTypeList"][0] |
| 11306 |
totalTmpVgpr = self.getNumOfTempVgprs(vectorDataTypes, kernel, 1, factorDims[d]) |
| 11307 |
tmpVgpr = self.vgprPool.checkOutAligned(totalTmpVgpr, 2, "store tmps") |
| 11308 |
tmpVgprRes = ContinuousRegister(idx=tmpVgpr, size=4) |
| 11309 |
|
| 11310 |
if d == 1: |
| 11311 |
module.add(factorDim1Label) |
| 11312 |
multiBiasTypeLabel = [] |
| 11313 |
for i in kernel["ProblemType"]["BiasDataTypeList"]: |
| 11314 |
name = self.labels.getNameInc("Load_Bias%s_%u"%(i.toNameAbbrev(), factorDims[d])) |
| 11315 |
multiBiasTypeLabel.append(Label(name, "")) |
| 11316 |
multiBiasTypeLabel.append(loadBiasEndLabel) |
| 11317 |
offsetVgpr = self.vgprPool.checkOut(1) |
| 11318 |
if len(kernel["ProblemType"]["BiasDataTypeList"]) == 1: |
| 11319 |
vectorDataTypes.bias(d).dataType = kernel["ProblemType"]["BiasDataTypeList"][0] |
| 11320 |
module.add(self.readVectorToLDS(vectorDataTypes, kernel, 1, offsetVgpr, tmpVgprRes, factorDims[d])) |
| 11321 |
if len(factorDims) == 2: |
| 11322 |
if d == 0: |
| 11323 |
module.add(SBranch(labelName=loadBiasEndLabel.getLabelName(), comment="Branch to load bias end")) |
| 11324 |
else: |
| 11325 |
module.add(loadBiasEndLabel) |
| 11326 |
else: |
| 11327 |
for i, label in enumerate(multiBiasTypeLabel[1:]): |
| 11328 |
typeValue = kernel["ProblemType"]["BiasDataTypeList"][i].value |
| 11329 |
module.add(multiBiasTypeLabel[i]) |
| 11330 |
|
| 11331 |
module.add(self.getSCMPKInstruction("LGU32", "BiasType", typeValue, comment="BiasType != %u"%typeValue)) |
| 11332 |
module.add(SCBranchSCC1(label.getLabelName(), "Branch if true")) |
| 11333 |
vectorDataTypes.bias(d).dataType = kernel["ProblemType"]["BiasDataTypeList"][i] |
| 11334 |
module.add(self.readVectorToLDS(vectorDataTypes, kernel, 1, offsetVgpr, tmpVgprRes, factorDims[d])) |
| 11335 |
module.add(SBranch(labelName=loadBiasEndLabel.getLabelName(), comment="Branch to load bias end")) |
| 11336 |
if d == len(factorDims) -1: |
| 11337 |
module.add(loadBiasEndLabel) |
| 11338 |
isLdsLoaded = True |
| 11339 |
self.vgprPool.checkIn(offsetVgpr) |
| 11340 |
self.vgprPool.checkIn(tmpVgpr) |
| 11341 |
elif self.states.useBias == DataDirection.WRITE: |
| 11342 |
labelStr = self.labels.getNameInc("Bias") |
| 11343 |
if kernel["ProblemType"]["BiasSrc"] == "A" or kernel["ProblemType"]["BiasSrc"] == "B": |
| 11344 |
|
| 11345 |
tP = tPA if kernel["ProblemType"]["BiasSrc"] == "A" else tPB |
| 11346 |
tile01 = tP["tile01Idx"] |
| 11347 |
maxKId = self.states.lraTileProperties[tile01].maxKId |
| 11348 |
biasMaxVgpr = kernel["VectorWidthA"] * kernel["ProblemType"]["ComputeDataType"].numRegisters() * maxKId |
| 11349 |
maxAlign = max(1, (kernel["VectorWidthA"] + 1) // 2 * 2) |
| 11350 |
tmpVgpr = self.vgprPool.checkOutAligned(biasMaxVgpr, maxAlign, "store tmps") |
| 11351 |
tmpVgprRes = ContinuousRegister(idx=tmpVgpr, size=biasMaxVgpr) |
| 11352 |
|
| 11353 |
|
| 11354 |
skipGlobalStoreLabel = Label(self.labels.getNameInc("SkipBiasStore"), comment="") |
| 11355 |
wgIdx = 1 if tile01 == 0 else 0 |
| 11356 |
|
| 11357 |
module.add(self.getSCMPKInstruction("LGU32", "WorkGroup%d"%wgIdx, 0, comment="WorkGroup%d != 0"%wgIdx)) |
| 11358 |
module.add(SCBranchSCC1(skipGlobalStoreLabel.getLabelName(), "Branch if true")) |
| 11359 |
if (kernel["GlobalSplitU"] > 1 or kernel["GlobalSplitU"] == -1) and kernel["GlobalSplitUAlgorithm"] == "MultipleBuffer": |
| 11360 |
sourceAddress = "D" |
| 11361 |
else: |
| 11362 |
sourceAddress = "Bias" |
| 11363 |
numRecordsStr = "SizeI" if kernel["ProblemType"]["BiasSrc"] == "A" else "SizeJ" |
| 11364 |
|
| 11365 |
module.add(allocPostLoopSrdSuppressRaw("Bias", sourceAddress, labelStr, sgprLength=sgpr(numRecordsStr))) |
| 11366 |
if sourceAddress == "D": |
| 11367 |
module.add(self.undefineSgpr("AddressD")) |
| 11368 |
multiBiasTypeLabel = [] |
| 11369 |
for i in kernel["ProblemType"]["BiasDataTypeList"]: |
| 11370 |
name = self.labels.getNameInc("Write_Bias%s"%i.toNameAbbrev()) |
| 11371 |
multiBiasTypeLabel.append(Label(name, "")) |
| 11372 |
writeBiasEndLabel = Label(self.labels.getNameInc("Write_Bias_End"), "") |
| 11373 |
multiBiasTypeLabel.append(writeBiasEndLabel) |
| 11374 |
|
| 11375 |
''' |
| 11376 |
gwvw is set to max(mt // kernel["NumThreads"], kernel["VectorWidthA"]) instead of kernel["VectorWidthA"] is that we don't want batch exists. |
| 11377 |
If VW is set to 1, MT=512, and flat work group = 256. We will have to set gwvw to 2 to store all the bias data. |
| 11378 |
''' |
| 11379 |
tile01 = tP["tile01Idx"] |
| 11380 |
mt = kernel["MacroTile%u" % tile01] |
| 11381 |
gwvw = min(max(mt // kernel["NumThreads"], kernel["VectorWidthA"]), tP["glvw"]) |
| 11382 |
offsetVgpr = self.vgprPool.checkOut(gwvw) |
| 11383 |
with self.allocTmpSgpr(5, 2) as tmpSgprRes: |
| 11384 |
if kernel["GlobalSplitU"] > 1 or kernel["GlobalSplitU"] == -1: |
| 11385 |
module.add(self.writeBiasToGlobal(kernel["ProblemType"]["ComputeDataType"], kernel, tP, gwvw, offsetVgpr, tmpSgprRes, tmpVgprRes)) |
| 11386 |
elif len(kernel["ProblemType"]["BiasDataTypeList"]) == 1: |
| 11387 |
module.add(self.writeBiasToGlobal(kernel["ProblemType"]["BiasDataTypeList"][0], kernel, tP, gwvw, offsetVgpr, tmpSgprRes, tmpVgprRes)) |
| 11388 |
else: |
| 11389 |
for i, label in enumerate(multiBiasTypeLabel[1:]): |
| 11390 |
typeValue = kernel["ProblemType"]["BiasDataTypeList"][i].value |
| 11391 |
module.add(multiBiasTypeLabel[i]) |
| 11392 |
|
| 11393 |
module.add(self.getSCMPKInstruction("LGU32", "BiasType", typeValue, comment="BiasType != %u"%typeValue)) |
| 11394 |
module.add(SCBranchSCC1(label.getLabelName(), "Branch if true")) |
| 11395 |
module.add(self.writeBiasToGlobal(kernel["ProblemType"]["BiasDataTypeList"][i], kernel, tP, gwvw, offsetVgpr, tmpSgprRes, tmpVgprRes)) |
| 11396 |
module.add(SBranch(labelName=writeBiasEndLabel.getLabelName(), comment="Branch to write bias end")) |
| 11397 |
module.add(writeBiasEndLabel) |
| 11398 |
self.vgprPool.checkIn(offsetVgpr) |
| 11399 |
self.vgprPool.checkIn(tmpVgpr) |
| 11400 |
module.add(skipGlobalStoreLabel) |
| 11401 |
else: |
| 11402 |
|
| 11403 |
module.add(allocPostLoopSrdSuppress("Bias", labelStr, hex(0x80000000))) |
| 11404 |
ssslist.append("Bias") |
| 11405 |
useSize.append(True) |
| 11406 |
|
| 11407 |
if vectorDataTypes.isValid() and (not isLdsLoaded): |
| 11408 |
if self.states.FactorDim == 3: |
| 11409 |
module.add(factorDim0Label) |
| 11410 |
module.add(self.getSCMPKInstruction("LGU32", "FactorDim", 0, comment="FactorDim != 0")) |
| 11411 |
module.add(SCBranchSCC1(factorDim1Label.getLabelName(), "Branch if true")) |
| 11412 |
labelDimEnd = Label(self.labels.getNameInc("MultiDimEnd"), "") |
| 11413 |
for d in range(len(factorDims)): |
| 11414 |
totalTmpVgpr = self.getNumOfTempVgprs(vectorDataTypes, kernel, 1, factorDims[d]) |
| 11415 |
tmpVgpr = self.vgprPool.checkOutAligned(totalTmpVgpr, 2, "store tmps") |
| 11416 |
tmpVgprRes = ContinuousRegister(idx=tmpVgpr, size=4) |
| 11417 |
offsetVgpr = self.vgprPool.checkOut(1) |
| 11418 |
if d == 1: |
| 11419 |
module.add(factorDim1Label) |
| 11420 |
module.add(self.readVectorToLDS(vectorDataTypes, kernel, 1, offsetVgpr, tmpVgprRes, factorDims[d])) |
| 11421 |
if self.states.FactorDim == 3 and d == 0: |
| 11422 |
module.add(SBranch(labelName=labelDimEnd.getLabelName(), comment="Branch to load end")) |
| 11423 |
self.vgprPool.checkIn(offsetVgpr) |
| 11424 |
self.vgprPool.checkIn(tmpVgpr) |
| 11425 |
if len(factorDims) > 1: |
| 11426 |
module.add(labelDimEnd) |
| 11427 |
|
| 11428 |
|
| 11429 |
if gsuLimit > 1 and gsuLimitIdx == 0: |
| 11430 |
if kernel["ProblemType"]["UseScaleAB"]: |
| 11431 |
if not self.states.preloadScaleA: |
| 11432 |
module.add(self.setSgprToFreeState("AddressScaleA")) |
| 11433 |
if (kernel["ProblemType"]["UseScaleAB"] == "Vector"): |
| 11434 |
module.add(self.setSgprToFreeState("SrdScaleA")) |
| 11435 |
if not self.states.preloadScaleB: |
| 11436 |
module.add(self.setSgprToFreeState("AddressScaleB")) |
| 11437 |
if (kernel["ProblemType"]["UseScaleAB"] == "Vector"): |
| 11438 |
module.add(self.setSgprToFreeState("SrdScaleB")) |
| 11439 |
if kernel["ProblemType"]["UseScaleAlphaVec"]: |
| 11440 |
module.add(self.setSgprToFreeState("AddressScaleAlphaVec")) |
| 11441 |
module.add(self.setSgprToFreeState("SrdScaleAlphaVec")) |
| 11442 |
else: |
| 11443 |
if kernel["ProblemType"]["UseScaleAB"]: |
| 11444 |
if not self.states.preloadScaleA: |
| 11445 |
module.add(self.undefineSgpr("AddressScaleA")) |
| 11446 |
if (kernel["ProblemType"]["UseScaleAB"] == "Vector"): |
| 11447 |
module.add(self.undefineSgpr("SrdScaleA")) |
| 11448 |
if not self.states.preloadScaleB: |
| 11449 |
module.add(self.undefineSgpr("AddressScaleB")) |
| 11450 |
if (kernel["ProblemType"]["UseScaleAB"] == "Vector"): |
| 11451 |
module.add(self.undefineSgpr("SrdScaleB")) |
| 11452 |
if kernel["ProblemType"]["UseScaleAlphaVec"]: |
| 11453 |
module.add(self.undefineSgpr("AddressScaleAlphaVec")) |
| 11454 |
module.add(self.undefineSgpr("SrdScaleAlphaVec")) |
| 11455 |
|
| 11456 |
if kernel["ProblemType"]["UseScaleAB"] == "Scalar" and (((kernel["GlobalSplitU"] == 1 or kernel["GlobalSplitU"] == -1) or kernel["StreamK"] > 0) or kernel["_GlobalAccumulation"] == 'MultipleBufferSingleKernel') and \ |
| 11457 |
((kernel["ProblemType"]["DataTypeA"].numRegisters() <= kernel["ProblemType"]["DataType"].numRegisters()) or \ |
| 11458 |
(kernel["ProblemType"]["DataTypeB"].numRegisters() <= kernel["ProblemType"]["DataType"].numRegisters())): |
| 11459 |
assert(kernel["ProblemType"]["ComputeDataType"].isSingle()) |
| 11460 |
newAlphaVgpr = self.vgprPool.checkOut(1) |
| 11461 |
module.add(VMovB32(dst=vgpr(newAlphaVgpr), src=sgpr("Alpha"))) |
| 11462 |
module.add(SWaitCnt(kmcnt=0, comment="wait for scaleAB load")) |
| 11463 |
if kernel["ProblemType"]["DataTypeA"].numRegisters() <= kernel["ProblemType"]["DataType"].numRegisters(): |
| 11464 |
module.add(VMulF32(dst=vgpr(newAlphaVgpr), src0=vgpr(newAlphaVgpr), src1=sgpr(sgprScaleA))) |
| 11465 |
if kernel["ProblemType"]["DataTypeB"].numRegisters() <= kernel["ProblemType"]["DataType"].numRegisters(): |
| 11466 |
module.add(VMulF32(dst=vgpr(newAlphaVgpr), src0=vgpr(newAlphaVgpr), src1=sgpr(sgprScaleB))) |
| 11467 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 11468 |
if kernel["StreamK"] > 0: |
| 11469 |
oldAlpha = self.sgprPool.checkOut(1) |
| 11470 |
module.add(SMovB32(dst=sgpr(oldAlpha), src=sgpr("Alpha"), comment="Save alpha value")) |
| 11471 |
module.add(VReadfirstlaneB32(dst=sgpr("Alpha"), src=vgpr(newAlphaVgpr), comment="Update Alpha")) |
| 11472 |
self.vgprPool.checkIn(newAlphaVgpr) |
| 11473 |
self.sgprPool.checkIn(sgprScaleA) |
| 11474 |
self.sgprPool.checkIn(sgprScaleB) |
| 11475 |
|
| 11476 |
|
| 11477 |
if kernel["ProblemType"]["UseScaleCD"] and ((kernel["GlobalSplitU"] == 1 or kernel["GlobalSplitU"] == -1) or kernel["StreamK"] > 0): |
| 11478 |
assert(kernel["ProblemType"]["ComputeDataType"].isSingle()) |
| 11479 |
newBetaVgpr = self.vgprPool.checkOut(1) |
| 11480 |
module.add(VMovB32(dst=vgpr(newBetaVgpr), src=sgpr("Beta"))) |
| 11481 |
if (not ((kernel["GlobalSplitU"] == 1 or kernel["GlobalSplitU"] == -1) or kernel["_GlobalAccumulation"] == 'MultipleBufferSingleKernel')) or kernel["StreamK"] > 0: |
| 11482 |
module.add(SWaitCnt(kmcnt=0, comment="wait for scaleC load")) |
| 11483 |
module.add(VMulF32(dst=vgpr(newBetaVgpr), src0=vgpr(newBetaVgpr), src1=sgpr(sgprScaleC))) |
| 11484 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 11485 |
module.add(VReadfirstlaneB32(dst=sgpr("Beta"), src=vgpr(newBetaVgpr), comment="Update Beta")) |
| 11486 |
self.vgprPool.checkIn(newBetaVgpr) |
| 11487 |
self.sgprPool.checkIn(sgprScaleC) |
| 11488 |
|
| 11489 |
module.add(SMovB32(dst=sgpr("ScaleD+1"), src=sgpr("ScaleD"))) |
| 11490 |
|
| 11491 |
if kernel["ProblemType"]["UseE"] and ((kernel["GlobalSplitU"] == 1 or kernel["GlobalSplitU"] == -1) or kernel["StreamK"] > 0): |
| 11492 |
|
| 11493 |
strideE1 = "StrideE%s" % (self.states.indexChars[kernel["PackedC1IndicesX"][0]]) |
| 11494 |
module.add(VMulLOU32(dst=vgpr(self.vgprs.coutRowPtrE), src0=vgpr(self.vgprs.coutRowPtrE), src1=sgpr(strideE1), comment=" offset 1")) |
| 11495 |
labelEStr = self.labels.getNameInc("E") |
| 11496 |
module.add(allocPostLoopSrdSuppress("E", labelEStr, hex(0x80000000))) |
| 11497 |
ssslist.append("E") |
| 11498 |
useSize.append(False) |
| 11499 |
|
| 11500 |
if ssslist: |
| 11501 |
module.add(self.computeStoreSrdStart(kernel, ssslist, useSize=useSize, noMultipleBuffer=True)) |
| 11502 |
|
| 11503 |
''' |
| 11504 |
Post process for loop end |
| 11505 |
''' |
| 11506 |
|
| 11507 |
atomic = (kernel["GlobalSplitU"] > 1 or kernel["GlobalSplitU"] == -1) and (kernel["_GlobalAccumulation"] != 'MultipleBuffer' and kernel["_GlobalAccumulation"] != 'MultipleBufferSingleKernel') |
| 11508 |
activation = self.exclasses.activation |
| 11509 |
|
| 11510 |
|
| 11511 |
|
| 11512 |
if betas is None: |
| 11513 |
hasBeta = kernel["ProblemType"]["UseBeta"] and \ |
| 11514 |
(kernel["_GlobalAccumulation"] != 'MultipleBuffer' or \ |
| 11515 |
kernel["GlobalSplitUAlgorithm"] == 'MultipleBufferSingleKernel' or \ |
| 11516 |
kernel["GlobalSplitU"] == 0) |
| 11517 |
betas = [False, True] if hasBeta else [False] |
| 11518 |
if edges is None: |
| 11519 |
edges = [False, True] if self.do["EdgeWrite"] else [False] |
| 11520 |
if factorDims is None: |
| 11521 |
factorDims = [0, 1] if self.states.FactorDim == 3 else [1] if self.states.FactorDim == 2 else [0] |
| 11522 |
writeLabels = {} |
| 11523 |
splitMN = False |
| 11524 |
if True in edges: |
| 11525 |
if vectorWidths[0] != vectorWidths[1]: |
| 11526 |
splitMN = True |
| 11527 |
for beta in betas: |
| 11528 |
writeLabels[beta] = {} |
| 11529 |
for edge in edges: |
| 11530 |
writeLabels[beta]["EdgeCheck0"] = Label(self.labels.getNameInc("GW_B%u_E%u_EdgeCheck0" % ( 1 if beta else 0, 1 if edge else 0) ), "") |
| 11531 |
writeLabels[beta]["EdgeCheck1"] = Label(self.labels.getNameInc("GW_B%u_E%u_EdgeCheck1" % ( 1 if beta else 0, 1 if edge else 0) ), "") |
| 11532 |
writeLabels[beta][edge] = {} |
| 11533 |
if len(factorDims) == 1: |
| 11534 |
if edge: |
| 11535 |
if splitMN: |
| 11536 |
writeLabels[beta][edge][factorDims[0]] = [] |
| 11537 |
writeLabels[beta][edge][factorDims[0]].append(Label(self.labels.getNameInc("GW_B%u_E%u_M" % ( 1 if beta else 0, 1 if edge else 0) ), "")) |
| 11538 |
writeLabels[beta][edge][factorDims[0]].append(Label(self.labels.getNameInc("GW_B%u_E%u_N" % ( 1 if beta else 0, 1 if edge else 0) ), "")) |
| 11539 |
else: |
| 11540 |
writeLabels[beta][edge][factorDims[0]] = [Label(self.labels.getNameInc("GW_B%u_E%u" % ( 1 if beta else 0, 1 if edge else 0) ), "")] |
| 11541 |
else: |
| 11542 |
writeLabels[beta][edge][factorDims[0]] = [Label(self.labels.getNameInc("GW_B%u_E%u" % ( 1 if beta else 0, 1 if edge else 0) ), "")] |
| 11543 |
else: |
| 11544 |
for factorDim in factorDims: |
| 11545 |
if edge: |
| 11546 |
if splitMN: |
| 11547 |
writeLabels[beta][edge][factorDim] = [] |
| 11548 |
writeLabels[beta][edge][factorDim].append(Label(self.labels.getNameInc("GW_B%u_E%u_FD%u_M" % ( 1 if beta else 0, 1 if edge else 0, factorDim) ), "")) |
| 11549 |
writeLabels[beta][edge][factorDim].append(Label(self.labels.getNameInc("GW_B%u_E%u_FD%u_N" % ( 1 if beta else 0, 1 if edge else 0, factorDim) ), "")) |
| 11550 |
else: |
| 11551 |
writeLabels[beta][edge][factorDim]= [Label(self.labels.getNameInc("GW_B%u_E%u_FD%u" % ( 1 if beta else 0, 1 if edge else 0, factorDim) ), "")] |
| 11552 |
else: |
| 11553 |
writeLabels[beta][edge][factorDim] = [Label(self.labels.getNameInc("GW_B%u_E%u_FD%u" % ( 1 if beta else 0, 1 if edge else 0, factorDim) ), "")] |
| 11554 |
endLabel = Label(self.labels.getNameInc("GW_End"), "") |
| 11555 |
|
| 11556 |
|
| 11557 |
""" |
| 11558 |
if B1 goto label_B1 |
| 11559 |
if E1 goto label_B0_E1_FD0 |
| 11560 |
if BD1 goto label_B0_E0_FD1 |
| 11561 |
label_B0_E0_FD0: |
| 11562 |
writes |
| 11563 |
goto label_End |
| 11564 |
label_B0_E0_FD1: |
| 11565 |
writes |
| 11566 |
goto label_End |
| 11567 |
label_B0_E1_FD0: |
| 11568 |
if BD1 goto label_B0_E1_FD1 |
| 11569 |
writes |
| 11570 |
goto label_End |
| 11571 |
label_B0_E1_FD1: |
| 11572 |
writes |
| 11573 |
goto label_End |
| 11574 |
label_B1: |
| 11575 |
if E1 goto label_B1_E1_FD0 |
| 11576 |
if BD1 goto label_B1_E0_FD1 |
| 11577 |
label_B1_E0_FD0: |
| 11578 |
writes |
| 11579 |
goto label_End |
| 11580 |
label_B1_E0_FD1: |
| 11581 |
writes |
| 11582 |
goto label_End |
| 11583 |
label_B1_E1_FD0: |
| 11584 |
if BD1 goto label_B1_E1_FD1 |
| 11585 |
writes |
| 11586 |
goto label_End |
| 11587 |
label_B1_E1_FD1: |
| 11588 |
writes |
| 11589 |
goto label_End |
| 11590 |
label_End |
| 11591 |
""" |
| 11592 |
|
| 11593 |
|
| 11594 |
|
| 11595 |
maxAlign = 2 |
| 11596 |
if kernel["BufferStore"]: |
| 11597 |
numTmpVgpr = 2 |
| 11598 |
if len(kernel["PackedC0IndicesX"]) > 1: |
| 11599 |
numTmpVgpr += 1 |
| 11600 |
else: |
| 11601 |
numTmpVgpr = 2 + 3 |
| 11602 |
|
| 11603 |
if (kernel["_GlobalAccumulation"] == "MultipleBufferSingleKernel") or kernel["BufferStore"]: |
| 11604 |
numTmpVgpr += 1 |
| 11605 |
else: |
| 11606 |
numTmpVgpr = None |
| 11607 |
|
| 11608 |
if kernel["NumWaveSplitK"] > 1: |
| 11609 |
numTmpVgpr += 1 |
| 11610 |
|
| 11611 |
actPCGwvwVgpr = 0 |
| 11612 |
actPCMaxTempSgpr = 0 |
| 11613 |
actTempSgpr = 0 |
| 11614 |
actExportType = ActivationType.Export.GRADONLY if kernel["ProblemType"]["Gradient"] else ActivationType.Export.NORMAL |
| 11615 |
if kernel["ActivationFuncCall"] or \ |
| 11616 |
((((kernel["GlobalSplitU"] == 1 or kernel["GlobalSplitU"] == -1) or kernel["StreamK"] > 0) and kernel["ActivationFused"]) and \ |
| 11617 |
(kernel["ProblemType"]["ActivationType"] != 'none')): |
| 11618 |
maxVw = max(vectorWidths) |
| 11619 |
|
| 11620 |
usage = activation.getAllGprUsage(kernel["ProblemType"]["ActivationComputeDataType"], kernel["ProblemType"]["ActivationType"], exportType=actExportType) |
| 11621 |
actPCMaxTempVgpr = 0 |
| 11622 |
for _, gprs in usage.items(): |
| 11623 |
actPCMaxTempVgpr = max(actPCMaxTempVgpr, gprs["vgpr"]) |
| 11624 |
actPCMaxTempSgpr = max(actPCMaxTempSgpr, gprs["sgpr"]) |
| 11625 |
actPCGwvwVgpr = int(ceil(maxVw * kernel["ProblemType"]["ActivationComputeDataType"].numRegisters())) |
| 11626 |
numTmpVgpr = max(numTmpVgpr, actPCMaxTempVgpr + actPCGwvwVgpr) |
| 11627 |
if kernel["ProblemType"]["UseE"] and (not kernel["ProblemType"]["Gradient"]): |
| 11628 |
maxVw = max(vectorWidths) |
| 11629 |
gwvwVgpr = int(ceil(maxVw * kernel["ProblemType"]["ActivationComputeDataType"].numRegisters())) |
| 11630 |
if kernel["ActivationFuncCall"]: |
| 11631 |
gwvwVgpr += actPCMaxTempVgpr + actPCGwvwVgpr |
| 11632 |
numTmpVgpr = max(numTmpVgpr, gwvwVgpr) |
| 11633 |
tmpVgpr = self.vgprPool.checkOutAligned(numTmpVgpr, maxAlign, "store tmps") |
| 11634 |
tmpVgpr = ContinuousRegister(idx=tmpVgpr, size=numTmpVgpr) |
| 11635 |
|
| 11636 |
cvtVgprStruct = None |
| 11637 |
cvtVgpr = None |
| 11638 |
if kernel["ProblemType"]["DestDataType"].isBFloat16() and kernel["ProblemType"]["HighPrecisionAccumulate"]: |
| 11639 |
cvtVgpr = self.vgprPool.checkOut(4) |
| 11640 |
cvtVgprStruct = self.BF16CVTVgprStruct(vgprBf16Temp=cvtVgpr, vgprBf16Mask=(cvtVgpr+1), \ |
| 11641 |
vgprFp32Nan=(cvtVgpr+2), vgprBf16Inc=(cvtVgpr+3)) |
| 11642 |
elif kernel["ProblemType"]["DestDataType"].isAnyFloat8() and kernel["ProblemType"]["HighPrecisionAccumulate"]: |
| 11643 |
cvtVgpr = self.vgprPool.checkOut(4) |
| 11644 |
cvtVgprStruct = self.FP8CVTVgprStruct(vgprFp8Temp=cvtVgpr, vgprFp8NanInf=(cvtVgpr+1), \ |
| 11645 |
vgprFp8Min=(cvtVgpr+2), vgprFp8Max=(cvtVgpr+3)) |
| 11646 |
elif kernel["ProblemType"]["DestDataType"].isAnyBFloat8(): |
| 11647 |
cvtVgpr = self.vgprPool.checkOut(4) |
| 11648 |
cvtVgprStruct = self.BF8CVTVgprStruct(vgprBF8Temp=cvtVgpr, vgprBF8NanInf=(cvtVgpr+1), \ |
| 11649 |
vgprBF8Min=(cvtVgpr+2), vgprBF8Max=(cvtVgpr+3)) |
| 11650 |
elif kernel["ProblemType"]["DestDataType"].isInt8(): |
| 11651 |
cvtVgpr = self.vgprPool.checkOut(4) |
| 11652 |
cvtVgprStruct = self.I8CVTVgprStruct(vgprI8Temp0=cvtVgpr, vgprI8Temp1=(cvtVgpr+1), \ |
| 11653 |
vgprI8Mask0=(cvtVgpr+2), vgprI8Mask1=(cvtVgpr+3)) |
| 11654 |
|
| 11655 |
activationSetPCStruct = None |
| 11656 |
activationLabelList = None |
| 11657 |
activationEnumStrList = None |
| 11658 |
toActModuleList = None |
| 11659 |
isInsertActFunctionCallAddrCalc = True |
| 11660 |
if kernel["ActivationFuncCall"]: |
| 11661 |
sgprOffsetActivation = self.sgprPool.checkOutAligned(2, 2, preventOverflow=False) |
| 11662 |
sgprOffsetBack = self.sgprPool.checkOutAligned(2, 2, preventOverflow=False) |
| 11663 |
activationSetPCStruct = self.ActivationSetPCStruct(sgprOffsetActivation=sgprOffsetActivation, \ |
| 11664 |
sgprOffsetBack=sgprOffsetBack, vgprActCopy=tmpVgpr.idx) |
| 11665 |
activationCDataType = kernel["ProblemType"]["ActivationComputeDataType"] |
| 11666 |
activationLabelList = {} |
| 11667 |
toActModuleList = {} |
| 11668 |
supportedBy = ActivationType.SupportedBy.ALL if kernel["ProblemType"]["ActivationType"] == 'all' else ActivationType.SupportedBy.HIPBLASLT |
| 11669 |
activationEnumStrList = ActivationType.getEnumStrList(activationCDataType, supportedBy, exportType=actExportType) |
| 11670 |
for gwvw in vectorWidths: |
| 11671 |
if gwvw in activationLabelList: |
| 11672 |
continue |
| 11673 |
activationLabelList[gwvw] = [] |
| 11674 |
toActModuleList[gwvw] = [] |
| 11675 |
for enumStr in activationEnumStrList: |
| 11676 |
name = self.labels.getNameInc("Activation_%s_VW%u"% (enumStr.capitalize(), gwvw)) |
| 11677 |
activationLabelList[gwvw].append(Label(name, "")) |
| 11678 |
toActModuleList[gwvw].append(Label("To_%s"% (name), "")) |
| 11679 |
|
| 11680 |
if vectorWidths.count(vectorWidths[0]) == len(vectorWidths): |
| 11681 |
isInsertActFunctionCallAddrCalc = False |
| 11682 |
module.add(self.insertActFunctionCallAddrCalc(activationSetPCStruct.sgprOffsetActivation, \ |
| 11683 |
vectorWidths[0], toActModuleList, activationEnumStrList, activationLabelList)) |
| 11684 |
|
| 11685 |
|
| 11686 |
|
| 11687 |
|
| 11688 |
|
| 11689 |
|
| 11690 |
|
| 11691 |
betaLabel = Label(self.labels.getNameInc("GW_Beta"), "") |
| 11692 |
skPartialsLabel = Label(label=self.labels.getNameInc("SK_Partials"), comment="") |
| 11693 |
skComponent = Component.StreamK.find(self) |
| 11694 |
module.add(skComponent.storeBranches(self, kernel, skPartialsLabel, vectorWidths_1, elements_1, tmpVgpr.idx, cvtVgprStruct)) |
| 11695 |
|
| 11696 |
if (kernel["_GlobalAccumulation"] == 'MultipleBufferSingleKernel'): |
| 11697 |
reductionStartLabel = Label(self.labels.getNameInc("Reduction_Start"), comment="Reduction start") |
| 11698 |
reductionEndLabel = Label(self.labels.getNameInc("Reduction_End"), comment="Reduction end") |
| 11699 |
|
| 11700 |
|
| 11701 |
|
| 11702 |
|
| 11703 |
|
| 11704 |
|
| 11705 |
|
| 11706 |
|
| 11707 |
|
| 11708 |
|
| 11709 |
gsuComponent = Component.GSU.find(self) |
| 11710 |
if kernel["GlobalSplitU"] > 1 and kernel["GlobalSplitUAlgorithm"] == "MultipleBufferSingleKernel": |
| 11711 |
self.defineSgpr("GSUStartWGIdx", 1) |
| 11712 |
module.add(RegSet("s", "sgprGSUStartWGIdx", self.sgprs["GSUStartWGIdx"])) |
| 11713 |
module.addSpaceLine() |
| 11714 |
module.add(reductionStartLabel) |
| 11715 |
module.add(gsuComponent.reductionBranches(self, kernel, tPB, vectorWidths_1, elements_1, tmpVgpr, cvtVgprStruct, \ |
| 11716 |
vectorDataTypes, factorDims, reductionEndLabel, endLabel)) |
| 11717 |
module.add(reductionEndLabel) |
| 11718 |
module.add(self.undefineSgpr("GSUStartWGIdx")) |
| 11719 |
module.addSpaceLine() |
| 11720 |
|
| 11721 |
|
| 11722 |
betaModules = Module("Betas") |
| 11723 |
currentInstLength = 0 |
| 11724 |
for idx0 in reversed(range(len(betas))): |
| 11725 |
beta = betas[idx0] |
| 11726 |
if beta and kernel["_GlobalAccumulation"] == "SingleBuffer" and (kernel["GlobalSplitU"] > 1 or kernel["GlobalSplitU"] == -1): |
| 11727 |
continue |
| 11728 |
betaModule = Module("Beta_%u"%idx0) |
| 11729 |
|
| 11730 |
if beta: |
| 11731 |
betaModule.add(betaLabel) |
| 11732 |
mod_pos = len(betaModule.items()) |
| 11733 |
|
| 11734 |
for idx1 in reversed(range(len(edges))): |
| 11735 |
edge = edges[idx1] |
| 11736 |
loopMN = 2 if (edge and splitMN) else 1 |
| 11737 |
for idxMN in range(loopMN): |
| 11738 |
edgeStr = "" |
| 11739 |
if loopMN == 2: |
| 11740 |
edgeStr = "_M" if idxMN == 0 else "_N" |
| 11741 |
edgeModule = Module("edge_%u%s"%(idx1, edgeStr)) |
| 11742 |
|
| 11743 |
vectorWidthsNew = vectorWidths |
| 11744 |
elementsNew = elements |
| 11745 |
if edge and idxMN == 1: |
| 11746 |
vectorWidthsNew = [vectorWidths[0], vectorWidths[0]] |
| 11747 |
elementsNew = [elements[0], elements[0]] |
| 11748 |
|
| 11749 |
edge_mode_pos = 0 |
| 11750 |
for idx2 in range(len(factorDims)): |
| 11751 |
edge_mode_pos, currentInstLength, activationTypeStr = \ |
| 11752 |
self.globalWriteElementBatch(kernel, tPA, tPB, activation, |
| 11753 |
applyAlpha, beta, edge, atomic, |
| 11754 |
vectorWidthsNew, elementsNew, activationLabelList, |
| 11755 |
tmpVgpr, cvtVgprStruct, activationSetPCStruct, activationEnumStrList, |
| 11756 |
actPCMaxTempSgpr, isInsertActFunctionCallAddrCalc, toActModuleList, |
| 11757 |
edgeModule, writeLabels, endLabel, |
| 11758 |
edge_mode_pos, currentInstLength, |
| 11759 |
idx0, idx1, idx2, idxMN, vectorDataTypes, factorDims) |
| 11760 |
if len(factorDims) == 2: |
| 11761 |
isLongBranch = True if currentInstLength >= 16384 else False |
| 11762 |
with self.allocTmpSgpr(3) as tmpSgprInfo: |
| 11763 |
checkIsFactorDimZero = edgeModule.add(self.checkIsFactorDimZero(kernel, tmpSgprInfo, \ |
| 11764 |
writeLabels[beta][edge][factorDims[1]][idxMN], isLongBranch=isLongBranch), pos=edge_mode_pos) |
| 11765 |
currentInstLength += countInstruction(checkIsFactorDimZero) |
| 11766 |
|
| 11767 |
betaModule.add(edgeModule, pos=mod_pos) |
| 11768 |
|
| 11769 |
|
| 11770 |
|
| 11771 |
if False in edges and True in edges: |
| 11772 |
isLongBranch = True if currentInstLength >= 16384 else False |
| 11773 |
with self.allocTmpSgpr(4) as tmpSgprInfo: |
| 11774 |
labelMT1 = writeLabels[beta][True][factorDims[0]][0] if len(writeLabels[beta][True][factorDims[0]]) == 1 else writeLabels[beta][True][factorDims[0]][1] |
| 11775 |
checkIsEdge = betaModule.add(self.checkIsEdge(kernel, tmpSgprInfo, \ |
| 11776 |
writeLabels[beta][True][factorDims[0]][0], labelMT1, isLongBranch=isLongBranch), pos=mod_pos) |
| 11777 |
currentInstLength += countInstruction(checkIsEdge) |
| 11778 |
betaModules.add(betaModule, pos=0) |
| 11779 |
|
| 11780 |
|
| 11781 |
if False in betas and True in betas: |
| 11782 |
isBetaLongBranch = False |
| 11783 |
count = 0 |
| 11784 |
count, found = findInstCount(betaModules, betaLabel, count) |
| 11785 |
if found: |
| 11786 |
if count >= 16384: |
| 11787 |
isBetaLongBranch = True |
| 11788 |
with self.allocTmpSgpr(3 if isBetaLongBranch else 1) as tmpSgprInfo: |
| 11789 |
module.add(self.checkIsBetaZero(kernel, tmpSgprInfo, betaLabel, isBetaLongBranch, posNeg=1)) |
| 11790 |
module.appendModule(betaModules) |
| 11791 |
|
| 11792 |
if activationLabelList: |
| 11793 |
assert activationEnumStrList and activationSetPCStruct |
| 11794 |
for key, activationLabelModules in activationLabelList.items(): |
| 11795 |
gwvw = key |
| 11796 |
actModules = Module(getActFuncModuleName(gwvw, \ |
| 11797 |
activationSetPCStruct.vgprActCopy, tmpVgpr.idx, actTempSgpr)) |
| 11798 |
for index, activationLabelModule in enumerate(activationLabelModules): |
| 11799 |
actModule = Module(activationLabelModule.getLabelName()) |
| 11800 |
actModule.add(activationLabelModule) |
| 11801 |
activationTypeStr = activationEnumStrList[index] |
| 11802 |
vgprIdx = activationSetPCStruct.vgprActCopy |
| 11803 |
if self.insertActivationAfterPacked(kernel, activationTypeStr): |
| 11804 |
actModule.appendModule(self.getActivationDestDataType(kernel, activation, \ |
| 11805 |
activationTypeStr, gwvw, vgprIdx, vgprIdx, (tmpVgpr.idx + actPCGwvwVgpr), \ |
| 11806 |
actTempSgpr)) |
| 11807 |
else: |
| 11808 |
actModule.appendModule(self.getActivationActivationComputeType(kernel, activation, \ |
| 11809 |
activationTypeStr, gwvw, vgprIdx, vgprIdx, (tmpVgpr.idx + actPCGwvwVgpr), \ |
| 11810 |
actTempSgpr)) |
| 11811 |
actModule.add(SSetPCB64(src=sgpr(activationSetPCStruct.sgprOffsetBack,2))) |
| 11812 |
actModules.add(actModule) |
| 11813 |
module.add(actModules) |
| 11814 |
self.sgprPool.checkIn(activationSetPCStruct.sgprOffsetActivation) |
| 11815 |
self.sgprPool.checkIn(activationSetPCStruct.sgprOffsetBack) |
| 11816 |
|
| 11817 |
module.add(skComponent.writePartials(self, kernel, skPartialsLabel, vectorWidths_1, elements_1, tmpVgpr.idx, cvtVgprStruct, endLabel)) |
| 11818 |
|
| 11819 |
|
| 11820 |
module.add(endLabel) |
| 11821 |
|
| 11822 |
if kernel["ProblemType"]["UseScaleAB"] == "Scalar" and kernel["StreamK"] > 0 and \ |
| 11823 |
((kernel["ProblemType"]["DataTypeA"].numRegisters() <= kernel["ProblemType"]["DataType"].numRegisters()) or \ |
| 11824 |
(kernel["ProblemType"]["DataTypeB"].numRegisters() <= kernel["ProblemType"]["DataType"].numRegisters())): |
| 11825 |
assert(kernel["ProblemType"]["ComputeDataType"].isSingle()) |
| 11826 |
module.add(SMovB32(dst=sgpr("Alpha"), src=sgpr(oldAlpha), comment="Restore alpha value")) |
| 11827 |
self.sgprPool.checkIn(oldAlpha) |
| 11828 |
|
| 11829 |
if self.states.FactorDim == 3: |
| 11830 |
self.updateBranchPlaceHolder(module, ["end_placeholder"], [endLabel.label], ["SBranch"]) |
| 11831 |
self.vgprPool.checkIn(tmpVgpr.idx) |
| 11832 |
if cvtVgpr is not None: |
| 11833 |
self.vgprPool.checkIn(cvtVgpr) |
| 11834 |
if gsuLimit > 1 and gsuLimitIdx == 0: |
| 11835 |
with self.allocTmpSgpr(3) as tmpSgprInfo: |
| 11836 |
module.add(SLongBranchPositive(Label("KernelEnd", ""), tmpSgprInfo)) |
| 11837 |
|
| 11838 |
kernel["GlobalSplitU"] = gsuBackup |
| 11839 |
kernel["_GlobalAccumulation"] = gsuAccumBackup |
| 11840 |
self.states.bpeCexternal = bpeCexternalBackup |
| 11841 |
return module |
| 11842 |
|
| 11843 |
def getMBSKGSUTotal(self, kernel): |
| 11844 |
if kernel["MbskPrefetchMethod"]: |
| 11845 |
return kernel["NumMbskPrefetchElements"] + 1 |
| 11846 |
GSUtotal = 16 |
| 11847 |
if (kernel["MIWaveTile"][0] * kernel["MIWaveTile"][1]) * (kernel["MIWaveGroup"][0] * kernel["MIWaveGroup"][1]) > 8: |
| 11848 |
GSUtotal = int(GSUtotal/int((kernel["MIWaveTile"][0] * kernel["MIWaveTile"][1]) * (kernel["MIWaveGroup"][0] * kernel["MIWaveGroup"][1])/8)) |
| 11849 |
GSUtotal = max(2,GSUtotal) |
| 11850 |
return GSUtotal |
| 11851 |
|
| 11852 |
def setOccupancy(self, kernel): |
| 11853 |
|
| 11854 |
|
| 11855 |
requiredSgprs = min(self.sgprPool.size() + 5, self.states.regCaps["MaxSgpr"]) |
| 11856 |
maxVgprs, occupancy = self.getMaxRegsForOccupancy(kernel["NumThreads"], self.vgprPool.size(), requiredSgprs, \ |
| 11857 |
self.getLdsSize(kernel), self.agprPool.size(), self.states.doubleVgpr) |
| 11858 |
|
| 11859 |
|
| 11860 |
if kernel["ISA"][0] != 12: |
| 11861 |
self.vgprPool.setOccupancyLimit(self.states.regCaps["MaxVgpr"], self.states.regCaps["PhysicalMaxVgpr"] // occupancy) |
| 11862 |
self.sgprPool.setOccupancyLimit(self.states.regCaps["MaxSgpr"], self.states.regCaps["PhysicalMaxSgpr"] // occupancy) |
| 11863 |
return maxVgprs, occupancy |
| 11864 |
|
| 11865 |
def refineOccupancy(self, kernel, atomic, elements, actPCMaxTempSgpr, \ |
| 11866 |
edgeI, gwvw, maxVgprs, ss): |
| 11867 |
|
| 11868 |
|
| 11869 |
numVgprAvailable = self.vgprPool.availableBlockMaxVgpr(maxVgprs, ss.numVgprsPerElement, ss.align) |
| 11870 |
|
| 11871 |
|
| 11872 |
|
| 11873 |
|
| 11874 |
|
| 11875 |
|
| 11876 |
|
| 11877 |
|
| 11878 |
|
| 11879 |
|
| 11880 |
minElements = 2 if (kernel["ProblemType"]["DataType"].isHalf() or kernel["ProblemType"]["DataType"].isBFloat16()) else 1 |
| 11881 |
if numVgprAvailable < minElements*ss.numVgprsPerElement: |
| 11882 |
self.vgprPool.resetOccupancyLimit() |
| 11883 |
print2("info: growing pool += %d * %d for GlobalWrite\n" \ |
| 11884 |
% (minElements,ss.numVgprsPerElement)) |
| 11885 |
self.vgprPool.growPool(0, minElements, ss.numVgprsPerElement, \ |
| 11886 |
"grow-pool for GlobalWrite") |
| 11887 |
maxVgprsN, occupancy = self.setOccupancy(kernel) |
| 11888 |
if maxVgprs != maxVgprsN: |
| 11889 |
|
| 11890 |
return self.refineOccupancy(kernel, atomic, elements, actPCMaxTempSgpr, edgeI, gwvw, maxVgprsN, ss) |
| 11891 |
numVgprAvailable = self.vgprPool.available() |
| 11892 |
|
| 11893 |
|
| 11894 |
if ss.numVgprsPerElement: |
| 11895 |
numElementsPerBatch = numVgprAvailable // ss.numVgprsPerElement |
| 11896 |
else: |
| 11897 |
numElementsPerBatch = len(elements[edgeI]) |
| 11898 |
|
| 11899 |
assert(self.states.c.numVgprValu % gwvw == 0) |
| 11900 |
|
| 11901 |
numElementsPerBatch = numElementsPerBatch if not kernel["NumElementsPerBatchStore"] else min(kernel["NumElementsPerBatchStore"],numElementsPerBatch) |
| 11902 |
|
| 11903 |
|
| 11904 |
|
| 11905 |
if ss.cfg.numElementsPerBatchLimitedBySgprs < numElementsPerBatch: |
| 11906 |
numElementsPerBatch = ss.cfg.numElementsPerBatchLimitedBySgprs |
| 11907 |
|
| 11908 |
|
| 11909 |
if (kernel["ProblemType"]["DataType"].isHalf() or kernel["ProblemType"]["DataType"].isBFloat16()): |
| 11910 |
|
| 11911 |
if numElementsPerBatch > 1: |
| 11912 |
numElementsPerBatch = int(numElementsPerBatch/2)*2 |
| 11913 |
|
| 11914 |
elif not kernel["EnableMatrixInstruction"] and not kernel["UseDotInstruction"]: |
| 11915 |
|
| 11916 |
|
| 11917 |
|
| 11918 |
|
| 11919 |
|
| 11920 |
|
| 11921 |
print2("WARNING: half requires at least two elements per batch") |
| 11922 |
self.states.overflowedResources = 3 |
| 11923 |
|
| 11924 |
assert numElementsPerBatch > 0, "numElementsPerBatch=0 for %s"%self.states.kernelName |
| 11925 |
|
| 11926 |
|
| 11927 |
if atomic and (ss.optSingleColVgpr or ss.optSharedColVgpr): |
| 11928 |
|
| 11929 |
|
| 11930 |
|
| 11931 |
|
| 11932 |
firstRow = [e for e in elements[edgeI] if e[0]==0 and e[2]==0] |
| 11933 |
numElementsPerBatch=min(len(firstRow),numElementsPerBatch) |
| 11934 |
|
| 11935 |
|
| 11936 |
|
| 11937 |
nBatchesPerRow = 0 |
| 11938 |
if kernel["StoreRemapVectorWidth"]: |
| 11939 |
firstRow = [e for e in elements[edgeI] if e[0]==0 and e[2]==0] |
| 11940 |
|
| 11941 |
nBatchesPerRow = 1 |
| 11942 |
for d in range(1, len(firstRow)+1): |
| 11943 |
largestFactor = len(firstRow)//d |
| 11944 |
if len(firstRow)%d == 0 and largestFactor <= numElementsPerBatch: |
| 11945 |
numElementsPerBatch = largestFactor |
| 11946 |
nBatchesPerRow = d |
| 11947 |
break |
| 11948 |
|
| 11949 |
|
| 11950 |
|
| 11951 |
|
| 11952 |
|
| 11953 |
|
| 11954 |
numBatches = max(1, ceilDivide(len(elements[edgeI]),numElementsPerBatch)) |
| 11955 |
|
| 11956 |
|
| 11957 |
|
| 11958 |
numVgprAvailable = self.vgprPool.availableBlock(ss.numVgprsPerElement, ss.align) |
| 11959 |
totalNeededVgpr = ss.numVgprsPerElement * numElementsPerBatch |
| 11960 |
|
| 11961 |
if numVgprAvailable < totalNeededVgpr: |
| 11962 |
self.vgprPool.resetOccupancyLimit() |
| 11963 |
print2("info: growing pool += %d * %d for GlobalWrite\n" \ |
| 11964 |
% (numBatches,ss.numVgprsPerElement)) |
| 11965 |
availableBlock = min(0, self.vgprPool.available() - numVgprAvailable) |
| 11966 |
self.vgprPool.growPool(0, totalNeededVgpr + availableBlock, 1, "grow-pool for GlobalWrite") |
| 11967 |
maxVgprsN, occupancy = self.setOccupancy(kernel) |
| 11968 |
if maxVgprs != maxVgprsN: |
| 11969 |
|
| 11970 |
return self.refineOccupancy(kernel, atomic, elements, actPCMaxTempSgpr, edgeI, gwvw, maxVgprsN, ss) |
| 11971 |
|
| 11972 |
|
| 11973 |
|
| 11974 |
|
| 11975 |
|
| 11976 |
numSgprs = ss.cfg.numTempSgprPerBatch + ss.cfg.numMaskSgprPerBatch + ss.cfg.numMaskSgprPerElement * numElementsPerBatch |
| 11977 |
|
| 11978 |
if actPCMaxTempSgpr: |
| 11979 |
numSgprs = max(actPCMaxTempSgpr, numSgprs) |
| 11980 |
|
| 11981 |
self.sgprPool.resetOccupancyLimit() |
| 11982 |
self.sgprPool.checkIn(self.sgprPool.checkOutAligned(numSgprs, 2, preventOverflow=False)) |
| 11983 |
maxVgprsN, occupancy = self.setOccupancy(kernel) |
| 11984 |
if maxVgprs != maxVgprsN: |
| 11985 |
|
| 11986 |
return self.refineOccupancy(kernel, atomic, elements, actPCMaxTempSgpr, edgeI, gwvw, maxVgprsN, ss) |
| 11987 |
return numElementsPerBatch, nBatchesPerRow, numBatches, numSgprs |
| 11988 |
|
| 11989 |
|
| 11990 |
|
| 11991 |
|
| 11992 |
|
| 11993 |
def globalWriteElementBatch(self, kernel, tPA, tPB, activation, \ |
| 11994 |
applyAlpha, beta, edge, atomic, \ |
| 11995 |
vectorWidths, elements, activationLabelList, \ |
| 11996 |
tmpVgpr, cvtVgprStruct, activationSetPCStruct, activationEnumStrList, \ |
| 11997 |
actPCMaxTempSgpr, isInsertActFunctionCallAddrCalc, toActModuleList, \ |
| 11998 |
edgeModule, writeLabels, endLabel, \ |
| 11999 |
edge_mode_pos, currentInstLength, \ |
| 12000 |
idx0, idx1, idx2, idxMN, vectorDataTypes, factorDims): |
| 12001 |
factorDim = factorDims[idx2] |
| 12002 |
edgeModule.add(writeLabels[beta][edge][factorDim][idxMN]) |
| 12003 |
if idx2 == 0: |
| 12004 |
edge_mode_pos = len(edgeModule.items()) |
| 12005 |
|
| 12006 |
|
| 12007 |
if kernel["StoreRemapVectorWidth"] and not beta: |
| 12008 |
edgeI = False |
| 12009 |
else: |
| 12010 |
edgeI = edge |
| 12011 |
|
| 12012 |
gwvw = vectorWidths[edgeI] |
| 12013 |
|
| 12014 |
|
| 12015 |
|
| 12016 |
|
| 12017 |
|
| 12018 |
|
| 12019 |
self.vgprPool.resetOccupancyLimit() |
| 12020 |
self.sgprPool.resetOccupancyLimit() |
| 12021 |
|
| 12022 |
|
| 12023 |
sgprList = [] |
| 12024 |
if kernel["_GlobalAccumulation"] == 'MultipleBufferSingleKernel': |
| 12025 |
sgprList.append(self.sgprPool.checkOut(1, preventOverflow=False)) |
| 12026 |
sgprList.append(self.sgprPool.checkOut(1, preventOverflow=False)) |
| 12027 |
sgprList.append(self.sgprPool.checkOut(1, preventOverflow=False)) |
| 12028 |
sgprList.append(self.sgprPool.checkOutAligned(2, 2, preventOverflow=False)) |
| 12029 |
sgprList.append(self.sgprPool.checkOutAligned(2, 2, preventOverflow=False)) |
| 12030 |
sgprList.append(self.sgprPool.checkOutAligned(4, 4, preventOverflow=False)) |
| 12031 |
for s in sgprList: |
| 12032 |
self.sgprPool.checkIn(s) |
| 12033 |
if actPCMaxTempSgpr > 0: |
| 12034 |
self.sgprPool.checkIn(self.sgprPool.checkOutAligned(actPCMaxTempSgpr, 2 if actPCMaxTempSgpr > 1 else 1, preventOverflow=False)) |
| 12035 |
|
| 12036 |
tmpVgprDynamic = None |
| 12037 |
tmpVgprDynamicSize = 0 |
| 12038 |
tmpVgprDynamicAlign = 0 |
| 12039 |
if kernel["_GlobalAccumulation"] == 'MultipleBufferSingleKernel': |
| 12040 |
GSUTotal = self.getMBSKGSUTotal(kernel) |
| 12041 |
vgprMbsk = (GSUTotal-1) * gwvw * max(1, kernel["ProblemType"]["DestDataType"].numRegisters()) |
| 12042 |
tmpVgprDynamicSize = vgprMbsk |
| 12043 |
tmpVgprDynamicAlign = 4 |
| 12044 |
if tmpVgprDynamicSize > 0: |
| 12045 |
tmpVgprDynamic = ContinuousRegister(idx=self.vgprPool.checkOutAligned(tmpVgprDynamicSize, tmpVgprDynamicAlign), size=tmpVgprDynamicSize) |
| 12046 |
|
| 12047 |
maxVgprs, occupancy = self.setOccupancy(kernel) |
| 12048 |
|
| 12049 |
ss = StoreState(self, kernel, gwvw, edge, beta, atomic, elements[edgeI], vectorDataTypes, dim=factorDim) |
| 12050 |
|
| 12051 |
actPCMaxTempSgpr_ = None |
| 12052 |
if activationLabelList and isInsertActFunctionCallAddrCalc: |
| 12053 |
assert activationSetPCStruct, activationEnumStrList and activationLabelList and toActModuleList |
| 12054 |
actPCMaxTempSgpr_ = actPCMaxTempSgpr |
| 12055 |
|
| 12056 |
numElementsPerBatch, nBatchesPerRow, numBatches, numSgprs = self.refineOccupancy(kernel, atomic, elements, actPCMaxTempSgpr_, edgeI, gwvw, maxVgprs, ss) |
| 12057 |
|
| 12058 |
|
| 12059 |
atomicW = min(gwvw, self.getVectorAtomicWidth(kernel)) |
| 12060 |
|
| 12061 |
if activationLabelList and isInsertActFunctionCallAddrCalc: |
| 12062 |
edgeModule.add(self.insertActFunctionCallAddrCalc(activationSetPCStruct.sgprOffsetActivation, \ |
| 12063 |
gwvw, toActModuleList, activationEnumStrList, activationLabelList, \ |
| 12064 |
idx0, idx1)) |
| 12065 |
|
| 12066 |
if self.db["PrintStoreRegisterDb"]: |
| 12067 |
print("edgeI", edgeI, "NumBatches", numBatches, "NumElementsPerBatch", numElementsPerBatch, "numVgprsPerElement", ss.numVgprsPerElement, "len(elements[edgeI])", len(elements[edgeI])) |
| 12068 |
print ("numSgprs=", numSgprs, "sgprPool.size()=", self.sgprPool.size(), "numTempSgprPerBatch=", ss.cfg.numTempSgprPerBatch, |
| 12069 |
"numMaskSgprPerBatch=", ss.cfg.numMaskSgprPerBatch, "numMaskSgprPerElement=", ss.cfg.numMaskSgprPerElement) |
| 12070 |
print(self.sgprPool.state()) |
| 12071 |
edgeModule.addComment1("edge=%d, allocate %u sgpr. perBatchTmpS=%u perBatchMaskS=%u perElementMaskS=%u elementsPerBatch=%u" % |
| 12072 |
(edgeI, numSgprs, ss.cfg.numTempSgprPerBatch, ss.cfg.numMaskSgprPerBatch, ss.cfg.numMaskSgprPerElement, numElementsPerBatch)) |
| 12073 |
|
| 12074 |
|
| 12075 |
|
| 12076 |
|
| 12077 |
actLoopEndLabel, actLoopLabelModules, actLoopEnumStrList = self.initActivationLoop(kernel, beta, edge) |
| 12078 |
actLoopModuleList = [] |
| 12079 |
actLoopModuleCodeLength = [] |
| 12080 |
with self.allocTmpSgpr(numSgprs, 2) as tmpSgprRes: |
| 12081 |
for index, activationLabelModule in enumerate(actLoopLabelModules): |
| 12082 |
actLoopModule = Module("Activation Loop %s"%index) |
| 12083 |
activationTypeStr = actLoopEnumStrList[index] |
| 12084 |
if activationLabelModule: |
| 12085 |
actLoopModule.add(activationLabelModule) |
| 12086 |
|
| 12087 |
tmpSgpr = tmpSgprRes.idx |
| 12088 |
actTempSgpr = tmpSgpr |
| 12089 |
elementSgprs = tmpSgpr + ss.cfg.numTempSgprPerBatch |
| 12090 |
codeAccVgprRead = deepcopy(self.codes.accVgprRead) if self.states.serializedStore else None |
| 12091 |
mulAlpha = self.codes.mulAlphaMultipleBuffer if (kernel["_GlobalAccumulation"] == 'MultipleBuffer' or kernel["_GlobalAccumulation"] == 'MultipleBufferSingleKernel') else self.codes.mulAlphaOther |
| 12092 |
codeMulAlpha = deepcopy(mulAlpha) if self.states.serializedStore else None |
| 12093 |
|
| 12094 |
self.alphaBeforeLoadC = False |
| 12095 |
if kernel["MIArchVgpr"] and applyAlpha and not kernel["_GlobalAccumulation"] == 'MultipleBufferSingleKernel': |
| 12096 |
codeAccVgprRead = None |
| 12097 |
|
| 12098 |
|
| 12099 |
if (kernel["StorePriorityOpt"] or kernel["StoreSyncOpt"]) and beta: |
| 12100 |
self.alphaBeforeLoadC = True |
| 12101 |
|
| 12102 |
if (kernel["LocalSplitU"] > 1): |
| 12103 |
codeMulAlpha = None |
| 12104 |
else: |
| 12105 |
codeMulAlpha = None |
| 12106 |
|
| 12107 |
biasLocalBarrierInit = False |
| 12108 |
|
| 12109 |
|
| 12110 |
ss.lsuStartVgprOffset = 0 |
| 12111 |
for batchIdx in range(0, numBatches): |
| 12112 |
elementStartIdx = batchIdx * numElementsPerBatch |
| 12113 |
elementStopIdx = min( elementStartIdx + numElementsPerBatch, len(elements[edgeI]) ) |
| 12114 |
elementsThisBatch = elements[edgeI][elementStartIdx:elementStopIdx] |
| 12115 |
|
| 12116 |
|
| 12117 |
|
| 12118 |
|
| 12119 |
if kernel["StoreRemapVectorWidth"]: |
| 12120 |
|
| 12121 |
self.StoreRemapLastBatch = 1 if (batchIdx+1) % nBatchesPerRow == 0 else 0 |
| 12122 |
|
| 12123 |
actLoopModule.add(self.globalWriteBatch(kernel, tPA, tPB, activation, ss, batchIdx, \ |
| 12124 |
applyAlpha, beta, edge, atomic, gwvw, atomicW, \ |
| 12125 |
elementsThisBatch, self.vgprs.addrE, self.vgprs.addrD, self.vgprs.addrC, self.vgprs.addrBias, \ |
| 12126 |
self.vgprs.addrScaleAVec, self.vgprs.addrScaleBVec, self.vgprs.addrScaleAlphaVec, \ |
| 12127 |
biasLocalBarrierInit, tmpVgpr, tmpVgprDynamic, cvtVgprStruct, activationSetPCStruct, \ |
| 12128 |
activationTypeStr, elementSgprs, tmpSgpr, codeAccVgprRead, codeMulAlpha, factorDim)) |
| 12129 |
biasLocalBarrierInit = True |
| 12130 |
|
| 12131 |
ss.resetState() |
| 12132 |
actLoopModuleList.append(actLoopModule) |
| 12133 |
actLoopModuleCodeLength.append(countInstruction(actLoopModule)) |
| 12134 |
|
| 12135 |
|
| 12136 |
|
| 12137 |
if tmpVgprDynamic: |
| 12138 |
self.vgprPool.checkIn(tmpVgprDynamic.idx) |
| 12139 |
|
| 12140 |
if len(actLoopLabelModules) > 1: |
| 12141 |
actInstCounter = 0 |
| 12142 |
|
| 12143 |
for index, actLoopLabelModule in enumerate(actLoopLabelModules): |
| 12144 |
enumIndex = ActivationType.getEnumIndex(actLoopEnumStrList[index]) |
| 12145 |
|
| 12146 |
edgeModule.add(self.getSCMPKInstruction("EQU32", "ActivationType", enumIndex, comment="activationType == %u"%enumIndex)) |
| 12147 |
if actInstCounter >= 16384: |
| 12148 |
edgeModule.add(self.longBranchScc1(actLoopLabelModule, posNeg=1, comment="Branch if true")) |
| 12149 |
else: |
| 12150 |
edgeModule.add(SCBranchSCC1(actLoopLabelModule.getLabelName(), "Branch if true")) |
| 12151 |
actInstCounter += actLoopModuleCodeLength[index] |
| 12152 |
|
| 12153 |
for index, _ in enumerate(actLoopLabelModules): |
| 12154 |
actLoopModule = actLoopModuleList[index] |
| 12155 |
if (index < (len(actLoopLabelModules) - 1)): |
| 12156 |
if actInstCounter >= 16384: |
| 12157 |
with self.allocTmpSgpr(3) as tmpSgprInfo: |
| 12158 |
actLoopModule.add(SLongBranchPositive(actLoopEndLabel, tmpSgprInfo)) |
| 12159 |
else: |
| 12160 |
actLoopModule.add(SBranch(labelName=actLoopEndLabel.getLabelName())) |
| 12161 |
actInstCounter -= actLoopModuleCodeLength[index] |
| 12162 |
|
| 12163 |
|
| 12164 |
for actLoopModule in actLoopModuleList: |
| 12165 |
edgeModule.appendModule(actLoopModule) |
| 12166 |
|
| 12167 |
if len(actLoopLabelModules) > 1: |
| 12168 |
edgeModule.add(actLoopEndLabel) |
| 12169 |
|
| 12170 |
if len(factorDims) == 1: |
| 12171 |
if currentInstLength >= 16384: |
| 12172 |
with self.allocTmpSgpr(3) as tmpSgprInfo: |
| 12173 |
edgeModule.add(SLongBranchPositive(endLabel, tmpSgprInfo, comment="jump to end")) |
| 12174 |
else: |
| 12175 |
edgeModule.add(SBranch(labelName=endLabel.getLabelName(), comment="jump to end")) |
| 12176 |
else: |
| 12177 |
end_placeholder = Module("end_placeholder") |
| 12178 |
edgeModule.add(end_placeholder) |
| 12179 |
currentInstLength += countInstruction(edgeModule) |
| 12180 |
del ss |
| 12181 |
|
| 12182 |
return edge_mode_pos, currentInstLength, activationTypeStr |
| 12183 |
|
| 12184 |
|
| 12185 |
|
| 12186 |
|
| 12187 |
|
| 12188 |
|
| 12189 |
|
| 12190 |
|
| 12191 |
|
| 12192 |
def chooseGlobalRead(self, useBuffer, bpl, destVgpr, \ |
| 12193 |
addr0, addr1, soffset, offset, \ |
| 12194 |
glc=False, slc=False, nt=False, lds=False, \ |
| 12195 |
tr=False, hi16=0, comment="load C"): |
| 12196 |
|
| 12197 |
rpv = bpl/4.0 |
| 12198 |
|
| 12199 |
if useBuffer: |
| 12200 |
rv = Module("Global Read") |
| 12201 |
mubuf = MUBUFModifiers(offen=True, offset12=offset, glc=glc, slc=slc, nt=nt, lds=lds) |
| 12202 |
|
| 12203 |
|
| 12204 |
def bufferLoadImpl(soffset): |
| 12205 |
nonlocal rv |
| 12206 |
factor = max(1, 4//bpl) |
| 12207 |
dst = None if lds else vgpr(destVgpr, rpv*factor) |
| 12208 |
if bpl==1 and hi16: |
| 12209 |
rv.add(BufferLoadD16HIU8(dst=dst, vaddr=addr0, saddr=addr1, \ |
| 12210 |
soffset=soffset, mubuf=mubuf, comment=comment)) |
| 12211 |
return rv |
| 12212 |
elif bpl==1 and not hi16: |
| 12213 |
rv.add(BufferLoadD16U8(dst=dst, vaddr=addr0, saddr=addr1, \ |
| 12214 |
soffset=soffset, mubuf=mubuf, comment=comment)) |
| 12215 |
return rv |
| 12216 |
elif bpl==2 and hi16: |
| 12217 |
rv.add(BufferLoadD16HIB16(dst=dst, vaddr=addr0, saddr=addr1, \ |
| 12218 |
soffset=soffset, mubuf=mubuf, comment=comment)) |
| 12219 |
return rv |
| 12220 |
elif bpl==2 and not hi16: |
| 12221 |
rv.add(BufferLoadD16B16(dst=dst, vaddr=addr0, saddr=addr1, \ |
| 12222 |
soffset=soffset, mubuf=mubuf, comment=comment)) |
| 12223 |
return rv |
| 12224 |
elif bpl==4: |
| 12225 |
rv.add(BufferLoadB32(dst=dst, vaddr=addr0, saddr=addr1, \ |
| 12226 |
soffset=soffset, mubuf=mubuf, comment=comment)) |
| 12227 |
return rv |
| 12228 |
elif bpl==8: |
| 12229 |
rv.add(BufferLoadB64(dst=dst, vaddr=addr0, saddr=addr1, \ |
| 12230 |
soffset=soffset, mubuf=mubuf, comment=comment)) |
| 12231 |
return rv |
| 12232 |
elif bpl==16: |
| 12233 |
rv.add(BufferLoadB128(dst=dst, vaddr=addr0, saddr=addr1, \ |
| 12234 |
soffset=soffset, mubuf=mubuf, comment=comment)) |
| 12235 |
return rv |
| 12236 |
elif bpl==32: |
| 12237 |
|
| 12238 |
rv = Module("emulated _buffer_load_b256") |
| 12239 |
dst = None if lds else vgpr(destVgpr, rpv//2) |
| 12240 |
rv.add(BufferLoadB128(dst=dst, vaddr=addr0, saddr=addr1, \ |
| 12241 |
soffset=soffset, mubuf=mubuf, comment=comment)) |
| 12242 |
mubuf2 = MUBUFModifiers(offen=True, offset12=int(offset + bpl/2), glc=glc, slc=slc, nt=nt, lds=lds) |
| 12243 |
if isinstance(destVgpr, str): |
| 12244 |
dst2 = destVgpr + "+" + str(int(rpv//2)) |
| 12245 |
elif isinstance(destVgpr, int): |
| 12246 |
dst2 = int(destVgpr + int(rpv//2)) |
| 12247 |
dst = None if lds else vgpr(dst2, rpv//2) |
| 12248 |
rv.add(BufferLoadB128(dst=dst, vaddr=addr0, saddr=addr1, \ |
| 12249 |
soffset=soffset, mubuf=mubuf2, comment=comment)) |
| 12250 |
return rv |
| 12251 |
elif bpl==64 and not lds: |
| 12252 |
rv = Module("emulated _buffer_load_b512") |
| 12253 |
|
| 12254 |
dst = vgpr(destVgpr, rpv//4) |
| 12255 |
rv.add(BufferLoadB128(dst=dst, vaddr=addr0, saddr=addr1, \ |
| 12256 |
soffset=soffset, mubuf=mubuf, comment=comment)) |
| 12257 |
|
| 12258 |
mubuf2 = MUBUFModifiers(offen=True, offset12=int(offset + bpl/4), glc=glc, slc=slc, nt=nt, lds=lds) |
| 12259 |
dst2 = destVgpr + "+" + str(int(rpv//4)) if isinstance(destVgpr, str) else int(destVgpr + int(rpv//4)) |
| 12260 |
|
| 12261 |
dst = vgpr(dst2, rpv//4) |
| 12262 |
rv.add(BufferLoadB128(dst=dst, vaddr=addr0, saddr=addr1, \ |
| 12263 |
soffset=soffset, mubuf=mubuf2, comment=comment)) |
| 12264 |
|
| 12265 |
mubuf3 = MUBUFModifiers(offen=True, offset12=int(offset + bpl/2), glc=glc, slc=slc, nt=nt, lds=lds) |
| 12266 |
dst3 = destVgpr + "+" + str(int(rpv//2)) if isinstance(destVgpr, str) else int(destVgpr + int(rpv//2)) |
| 12267 |
|
| 12268 |
dst = vgpr(dst3, rpv//4) |
| 12269 |
rv.add(BufferLoadB128(dst=dst, vaddr=addr0, saddr=addr1, \ |
| 12270 |
soffset=soffset, mubuf=mubuf3, comment=comment)) |
| 12271 |
|
| 12272 |
mubuf4 = MUBUFModifiers(offen=True, offset12= int(offset + 3*bpl/4), glc=glc, slc=slc, nt=nt, lds=lds) |
| 12273 |
dst4 = destVgpr + "+" + str(3*int(rpv//4)) if isinstance(destVgpr, str) else int(destVgpr + 3*int(rpv//4)) |
| 12274 |
|
| 12275 |
dst = vgpr(dst4, rpv//4) |
| 12276 |
rv.add(BufferLoadB128(dst=dst, vaddr=addr0, saddr=addr1, \ |
| 12277 |
soffset=soffset, mubuf=mubuf4, comment=comment)) |
| 12278 |
else: |
| 12279 |
assert 0, "%s\nchooseGlobalRead: bad bpl %u"%(self.states.kernelName,bpl) |
| 12280 |
|
| 12281 |
|
| 12282 |
|
| 12283 |
if offset >= 4096: |
| 12284 |
if soffset in (0, "0"): |
| 12285 |
mubuf = MUBUFModifiers(offen=True, offset12=0, glc=glc, slc=slc, nt=nt, lds=lds) |
| 12286 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 12287 |
soffset = sgpr(tmpSgprInfo.idx) |
| 12288 |
rv.add(SMovB32(dst=soffset, src=offset, comment="large offset")) |
| 12289 |
bufferLoadImpl(soffset) |
| 12290 |
else: |
| 12291 |
assert 0, "offset too large and soffset set" |
| 12292 |
else: |
| 12293 |
bufferLoadImpl(soffset) |
| 12294 |
|
| 12295 |
return rv |
| 12296 |
elif tr: |
| 12297 |
|
| 12298 |
if offset >= 1<<24: |
| 12299 |
raise RuntimeError("chooseGlobalRead: offset too large for global_load %u"%(offset)) |
| 12300 |
|
| 12301 |
modifier = GLOBALModifiers(offset=offset) |
| 12302 |
|
| 12303 |
if bpl==8: |
| 12304 |
return GlobalLoadTR8B64(dst=vgpr(destVgpr, rpv), vaddr=addr0, saddr=addr1, modifier=modifier, comment=comment) |
| 12305 |
elif bpl==16: |
| 12306 |
return GlobalLoadTR16B128(dst=vgpr(destVgpr, rpv), vaddr=addr0, saddr=addr1, modifier=modifier, comment=comment) |
| 12307 |
else: |
| 12308 |
flat = FLATModifiers(glc=glc, slc=slc, lds=lds) |
| 12309 |
if bpl==2 and hi16: |
| 12310 |
return FlatLoadD16HIB16(dst=vgpr(destVgpr, rpv*2), vaddr=addr0, flat=flat, comment=comment) |
| 12311 |
elif bpl==2 and not hi16: |
| 12312 |
return FlatLoadD16B16(dst=vgpr(destVgpr, rpv*2), vaddr=addr0, flat=flat, comment=comment) |
| 12313 |
elif bpl==4: |
| 12314 |
return FlatLoadB32(dst=vgpr(destVgpr, rpv), vaddr=addr0, flat=flat, comment=comment) |
| 12315 |
elif bpl==8: |
| 12316 |
return FlatLoadB64(dst=vgpr(destVgpr, rpv), vaddr=addr0, flat=flat, comment=comment) |
| 12317 |
elif bpl==16: |
| 12318 |
return FlatLoadB128(dst=vgpr(destVgpr, rpv), vaddr=addr0, flat=flat, comment=comment) |
| 12319 |
else: |
| 12320 |
assert 0, "chooseGlobalRead: bad bpl" |
| 12321 |
|
| 12322 |
|
| 12323 |
def chooseGlobalWrite(self, useBuffer, bps, srcVgpr, rpv, \ |
| 12324 |
addr0, addr1, offset, soffset=0, \ |
| 12325 |
glc=False, slc=False, nt=False, hi16=0, comment="store"): |
| 12326 |
""" |
| 12327 |
create the store instruction for requested vector width and other parms |
| 12328 |
rpv = regs per vector |
| 12329 |
""" |
| 12330 |
|
| 12331 |
module = Module("chooseGlobalWrite %s -> %s (%s)"%(srcVgpr, addr0, addr1)) |
| 12332 |
|
| 12333 |
def bufferStoreImpl(tmpSgpr, mubuf): |
| 12334 |
if bps==1 and hi16: |
| 12335 |
module.add(BufferStoreD16HIB16(src=vgpr(srcVgpr, rpv*4), vaddr=addr0, \ |
| 12336 |
saddr=addr1, soffset=tmpSgpr, mubuf=mubuf, comment=comment)) |
| 12337 |
elif bps==1 and not hi16: |
| 12338 |
module.add(BufferStoreB8(src=vgpr(srcVgpr, rpv*4), vaddr=addr0, \ |
| 12339 |
saddr=addr1, soffset=tmpSgpr, mubuf=mubuf, comment=comment)) |
| 12340 |
elif bps==2 and hi16: |
| 12341 |
module.add(BufferStoreD16HIB16(src=vgpr(srcVgpr, rpv*2), vaddr=addr0, \ |
| 12342 |
saddr=addr1, soffset=tmpSgpr, mubuf=mubuf, comment=comment)) |
| 12343 |
elif bps==2 and not hi16: |
| 12344 |
module.add(BufferStoreB16(src=vgpr(srcVgpr, rpv*2), vaddr=addr0, \ |
| 12345 |
saddr=addr1, soffset=tmpSgpr, mubuf=mubuf, comment=comment)) |
| 12346 |
elif bps==4: |
| 12347 |
module.add(BufferStoreB32(src=vgpr(srcVgpr, rpv), vaddr=addr0, \ |
| 12348 |
saddr=addr1, soffset=tmpSgpr, mubuf=mubuf, comment=comment)) |
| 12349 |
elif bps==8: |
| 12350 |
module.add(BufferStoreB64(src=vgpr(srcVgpr, rpv), vaddr=addr0, \ |
| 12351 |
saddr=addr1, soffset=tmpSgpr, mubuf=mubuf, comment=comment)) |
| 12352 |
elif bps==16: |
| 12353 |
module.add(BufferStoreB128(src=vgpr(srcVgpr, rpv), vaddr=addr0, \ |
| 12354 |
saddr=addr1, soffset=tmpSgpr, mubuf=mubuf, comment=comment)) |
| 12355 |
|
| 12356 |
elif bps >= 32 and bps % 32 == 0: |
| 12357 |
|
| 12358 |
rounds = bps // 16 |
| 12359 |
shiftByte = bps // rounds |
| 12360 |
shiftRpv = rpv // rounds |
| 12361 |
module.add(BufferStoreB128(src=vgpr(srcVgpr, shiftRpv), vaddr=addr0, \ |
| 12362 |
saddr=addr1, soffset=tmpSgpr, mubuf=mubuf, comment=comment)) |
| 12363 |
for i in range(1, rounds): |
| 12364 |
offset2 = offset+shiftByte*i |
| 12365 |
if offset2 >= 4096: |
| 12366 |
module.add(SMovB32(dst=tmpSgpr, src=offset2, comment="large offset")) |
| 12367 |
offset2 = 0 |
| 12368 |
mubuf2 = MUBUFModifiers(offen=True, offset12=offset2, glc=glc, slc=slc, nt=nt, isStore=True) |
| 12369 |
vgprOff = int(srcVgpr + shiftRpv * i) if isinstance(srcVgpr, int) else f"{srcVgpr}+{int(shiftRpv * i)}" |
| 12370 |
module.add(BufferStoreB128(src=vgpr(vgprOff, shiftRpv), vaddr=addr0, \ |
| 12371 |
saddr=addr1, soffset=tmpSgpr, mubuf=mubuf2, comment=comment)) |
| 12372 |
else: |
| 12373 |
assert 0, "bad bps" |
| 12374 |
|
| 12375 |
if useBuffer: |
| 12376 |
mubuf = MUBUFModifiers(offen=True, offset12=offset, glc=glc, slc=slc, nt=nt, isStore=True) |
| 12377 |
if soffset != 0: |
| 12378 |
assert offset < 4096, "sgpr offset provided with large const offset" |
| 12379 |
|
| 12380 |
|
| 12381 |
maxShift = max(bps - 16, 0) |
| 12382 |
if (offset + maxShift) >= 4096: |
| 12383 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 12384 |
tmpSgpr = sgpr(tmpSgprInfo.idx) |
| 12385 |
if offset >= 4096: |
| 12386 |
module.add(SMovB32(dst=tmpSgpr, src=offset, comment="large offset")) |
| 12387 |
mubuf = MUBUFModifiers(offen=True, offset12=0, glc=glc, slc=slc, nt=nt, isStore=True) |
| 12388 |
bufferStoreImpl(tmpSgpr, mubuf) |
| 12389 |
else: |
| 12390 |
bufferStoreImpl(soffset, mubuf) |
| 12391 |
|
| 12392 |
else: |
| 12393 |
flat = FLATModifiers(glc=glc, slc=slc, isStore=True) |
| 12394 |
if bps==2 and hi16: |
| 12395 |
module.add(FlatStoreD16HIB16(vaddr=addr0, src=vgpr(srcVgpr*2), flat=flat, comment=comment)) |
| 12396 |
elif bps==2 and not hi16: |
| 12397 |
module.add(FlatStoreD16B16(vaddr=addr0, src=vgpr(srcVgpr, rpv*2), flat=flat, comment=comment)) |
| 12398 |
elif bps==4: |
| 12399 |
module.add(FlatStoreB32(vaddr=addr0, src=vgpr(srcVgpr, rpv), flat=flat, comment=comment)) |
| 12400 |
elif bps==8: |
| 12401 |
module.add(FlatStoreB64(vaddr=addr0, src=vgpr(srcVgpr, rpv), flat=flat, comment=comment)) |
| 12402 |
elif bps==16: |
| 12403 |
module.add(FlatStoreB128(vaddr=addr0, src=vgpr(srcVgpr, rpv), flat=flat, comment=comment)) |
| 12404 |
else: |
| 12405 |
assert 0, "bad bps" |
| 12406 |
|
| 12407 |
return module |
| 12408 |
|
| 12409 |
def addVecGlobalLoad(self, dataType, kernel, vecVgpr, addr0, addr1, offset, gwvw, comment=""): |
| 12410 |
""" |
| 12411 |
Add vec for the element with addrCalc, elementIdx, and vecVgpr. |
| 12412 |
vecVgpr is one or more vgpr :temp vGPR ( = gwvw * numbytes // 4 + 1 if cvt is needed) |
| 12413 |
""" |
| 12414 |
|
| 12415 |
module = Module(comment) |
| 12416 |
bps = dataType.numBytes() * gwvw |
| 12417 |
|
| 12418 |
useBuffer = kernel["BufferLoad"] |
| 12419 |
if dataType.isHalf() or dataType.isBFloat16(): |
| 12420 |
module.add(self.chooseGlobalRead(useBuffer, bps, vecVgpr, \ |
| 12421 |
addr0, addr1, soffset=0, offset=offset, hi16=0, comment=comment)) |
| 12422 |
elif dataType.isInt32() or dataType.isSingle(): |
| 12423 |
module.add(self.chooseGlobalRead(useBuffer, bps, vecVgpr, \ |
| 12424 |
addr0, addr1, soffset=0, offset=offset, comment=comment)) |
| 12425 |
elif dataType.isDouble() or dataType.isSingleComplex() : |
| 12426 |
module.add(self.chooseGlobalRead(useBuffer, bps, vecVgpr, \ |
| 12427 |
addr0, addr1, soffset=0, offset=offset, comment=comment)) |
| 12428 |
else: |
| 12429 |
printExit("Unsupported %s type %s."%(comment, str(dataType))) |
| 12430 |
return module |
| 12431 |
|
| 12432 |
|
| 12433 |
def addScaleVecLoad(self, kernel, ss, name: str, srdName: str, addrScaleVecVgpr, scaleVecVgpr, gwvw, scaleVecOffset, addVecPostfix = True): |
| 12434 |
""" |
| 12435 |
Add scaleAlphaVec for the element with addrCalc, elementIdx, and scaleVecVgpr. |
| 12436 |
scaleVecVgpr is one or more vgpr :temp vGPR ( = gwvw * numbytes // 4 + 1 if cvt is needed) |
| 12437 |
""" |
| 12438 |
module = Module("addScale%sVec"%srdName) |
| 12439 |
if kernel["ProblemType"]["UseScale%s"%name] and ((kernel["GlobalSplitU"] == 1 or kernel["GlobalSplitU"] == -1) or kernel["GlobalSplitUAlgorithm"] == 'MultipleBufferSingleKernel' or kernel["StreamK"] > 0): |
| 12440 |
bps = kernel["ProblemType"]["ComputeDataType"].numBytes() * gwvw |
| 12441 |
if kernel["BufferLoad"]: |
| 12442 |
addr0 = vgpr(addrScaleVecVgpr) |
| 12443 |
addr1 = sgpr("SrdScale%sVec"%srdName, 4) if addVecPostfix else sgpr("SrdScale%s"%srdName, 4) |
| 12444 |
else: |
| 12445 |
addr0 = vgpr(addrScaleVecVgpr,2) |
| 12446 |
addr1 = "" |
| 12447 |
|
| 12448 |
useBuffer = kernel["BufferLoad"] |
| 12449 |
|
| 12450 |
if kernel["ProblemType"]["ComputeDataType"].isHalf() or kernel["ProblemType"]["ComputeDataType"].isBFloat16(): |
| 12451 |
module.add(self.chooseGlobalRead(useBuffer, bps, scaleVecVgpr, \ |
| 12452 |
addr0, addr1, soffset=0, offset=scaleVecOffset, hi16=0, comment="load scale%sVecH"%srdName)) |
| 12453 |
elif kernel["ProblemType"]["ComputeDataType"].isInt32() or kernel["ProblemType"]["ComputeDataType"].isSingle(): |
| 12454 |
module.add(self.chooseGlobalRead(useBuffer, bps, scaleVecVgpr, \ |
| 12455 |
addr0, addr1, soffset=0, offset=scaleVecOffset, comment="load scale%sVecI"%srdName)) |
| 12456 |
elif kernel["ProblemType"]["ComputeDataType"].isDouble() or kernel["ProblemType"]["ComputeDataType"].isSingleComplex() : |
| 12457 |
module.add(self.chooseGlobalRead(useBuffer, bps, scaleVecVgpr, \ |
| 12458 |
addr0, addr1, soffset=0, offset=scaleVecOffset, comment="load scale%sVec"%srdName)) |
| 12459 |
else: |
| 12460 |
printExit("Unsupported scale%sVec type %s."%(srdName, str(kernel["ProblemType"]["ComputeDataType"]))) |
| 12461 |
|
| 12462 |
return module |
| 12463 |
|
| 12464 |
def addLdsLoad(self, dataType, dstVgpr, srcAddrVgpr, dsOffset, gwvw, comment=""): |
| 12465 |
module = Module(comment) |
| 12466 |
dst = vgpr(dstVgpr) |
| 12467 |
src = vgpr(srcAddrVgpr) |
| 12468 |
ds = DSModifiers(offset=dsOffset) |
| 12469 |
bpl = dataType.numBytes() * gwvw |
| 12470 |
if bpl==2: |
| 12471 |
module.add(DSLoadU16(dst=dst, src=src, ds=ds, comment=comment)) |
| 12472 |
elif bpl==4: |
| 12473 |
module.add(DSLoadB32(dst=dst, src=src, ds=ds, comment=comment)) |
| 12474 |
elif bpl==8: |
| 12475 |
module.add(DSLoadB64(dst=vgpr(dstVgpr, 2), src=src, ds=ds, comment=comment)) |
| 12476 |
elif bpl==16: |
| 12477 |
module.add(DSLoadB128(dst=vgpr(dstVgpr, 4), src=src, ds=ds, comment=comment)) |
| 12478 |
elif bpl==32: |
| 12479 |
module.add(DSLoadB128(dst=vgpr(dstVgpr, 4), src=src, ds=ds, comment=comment)) |
| 12480 |
ds = DSModifiers(offset=int(dsOffset+bpl/2)) |
| 12481 |
module.add(DSLoadB128(dst=vgpr(dstVgpr+4, 4), src=src, ds=ds, comment=comment)) |
| 12482 |
elif bpl==64: |
| 12483 |
module.add(DSLoadB128(dst=vgpr(dstVgpr, 4), src=src, ds=ds, comment=comment)) |
| 12484 |
ds = DSModifiers(offset=int(dsOffset+bpl/4)) |
| 12485 |
module.add(DSLoadB128(dst=vgpr(dstVgpr+4, 4), src=src, ds=ds, comment=comment)) |
| 12486 |
ds = DSModifiers(offset=int(dsOffset+2*bpl/4)) |
| 12487 |
module.add(DSLoadB128(dst=vgpr(dstVgpr+8, 4), src=src, ds=ds, comment=comment)) |
| 12488 |
ds = DSModifiers(offset=int(dsOffset+3*bpl/4)) |
| 12489 |
module.add(DSLoadB128(dst=vgpr(dstVgpr+12, 4), src=src, ds=ds, comment=comment)) |
| 12490 |
else: |
| 12491 |
assert 0, "bad bpl" |
| 12492 |
return module |
| 12493 |
|
| 12494 |
def addBiasLoad(self, dataType, kernel, gwvw, addrCalc, biasVgpr, factorDim, isLocal=False): |
| 12495 |
if isLocal and (self.states.useBias == DataDirection.READ): |
| 12496 |
return self.addLdsLoad(dataType, biasVgpr, addrCalc.addrBiasVgpr, addrCalc.biasOffset[factorDim], gwvw, comment="Load Bias") |
| 12497 |
|
| 12498 |
if self.states.useBias == DataDirection.READ: |
| 12499 |
if kernel["BufferLoad"]: |
| 12500 |
addr0 = vgpr(addrCalc.addrBiasVgpr) |
| 12501 |
addr1 = sgpr("SrdBias", 4) |
| 12502 |
else: |
| 12503 |
addr0 = vgpr(addrCalc.addrBiasVgpr,2) |
| 12504 |
addr1 = "" |
| 12505 |
else: |
| 12506 |
return Module("Empty load") |
| 12507 |
return self.addVecGlobalLoad(dataType, kernel, biasVgpr, addr0, addr1, addrCalc.biasOffset[factorDim], gwvw, comment="Load Bias") |
| 12508 |
|
| 12509 |
|
| 12510 |
def addStore(self, kernel, ss, tc: str, addrCalc, sumIdx, tmpS01, edge, wsOffset=0, comment="addStore"): |
| 12511 |
""" |
| 12512 |
Add stores for the element with addrCalc and sumIdx. |
| 12513 |
tmpS01 is a single :temp sGPR |
| 12514 |
""" |
| 12515 |
module = Module("addStore sumIdx %s"%(str(sumIdx))) |
| 12516 |
if self.do["GlobalWrite"]: |
| 12517 |
|
| 12518 |
|
| 12519 |
|
| 12520 |
isGlc = False |
| 12521 |
isSlc = False |
| 12522 |
isNT = False |
| 12523 |
|
| 12524 |
if tc == 'D': |
| 12525 |
isGlc = bool(kernel["NonTemporalD"] & 0x1) |
| 12526 |
isSlc = bool(kernel["NonTemporalD"] & 0x2) |
| 12527 |
isNT = bool(kernel["NonTemporalD"] & 0x4) |
| 12528 |
bps = self.states.bpeCexternal * ss.cfg.gwvw |
| 12529 |
rpv = self.states.bpeCexternal * ss.cfg.gwvw / self.states.bpr |
| 12530 |
|
| 12531 |
if kernel["BufferStore"]: |
| 12532 |
addr0 = vgpr(addrCalc.addrDVgpr) |
| 12533 |
addr1 = sgpr("SrdD", 4) |
| 12534 |
else: |
| 12535 |
addr0 = vgpr(addrCalc.addrDVgpr,2) |
| 12536 |
addr1 = "" |
| 12537 |
if ss.optSrdIncForRow and addrCalc.rowInc: |
| 12538 |
module.add(addrCalc.incrementToNextRow(kernel, "D", ss, tmpS01)) |
| 12539 |
|
| 12540 |
dataType = kernel["ProblemType"]["DestDataType"] |
| 12541 |
globalOffset = addrCalc.globalOffset |
| 12542 |
elif tc == 'TD': |
| 12543 |
isGlc = True |
| 12544 |
isSlc = True |
| 12545 |
|
| 12546 |
bps = kernel["ProblemType"]["DestDataType"].numBytes() * ss.cfg.gwvw |
| 12547 |
rpv = kernel["ProblemType"]["DestDataType"].numBytes() * ss.cfg.gwvw / self.states.bpr |
| 12548 |
if kernel["BufferStore"]: |
| 12549 |
addr0 = vgpr(addrCalc.addrGSUSyncVgprs) |
| 12550 |
addr1 = sgpr("SrdTD", 4) |
| 12551 |
else: |
| 12552 |
addr0 = vgpr(addrCalc.addrGSUSyncVgprs,2) |
| 12553 |
addr1 = "" |
| 12554 |
if ss.optSrdIncForRow and addrCalc.rowInc: |
| 12555 |
module.add(addrCalc.incrementToNextRow(kernel, "TD", ss, tmpS01)) |
| 12556 |
dataType = kernel["ProblemType"]["DestDataType"] |
| 12557 |
globalOffset = addrCalc.globalOffset |
| 12558 |
globalOffset = int((globalOffset/self.states.bpeCexternal) * self.states.bpr * kernel["ProblemType"]["DestDataType"].numRegisters()) |
| 12559 |
elif tc == 'WS': |
| 12560 |
isGlc = True |
| 12561 |
isSlc = True |
| 12562 |
isNT = bool(kernel["NonTemporalD"] & 0x4) |
| 12563 |
|
| 12564 |
bps = self.states.bpeCinternal * ss.cfg.gwvw |
| 12565 |
rpv = self.states.bpeCinternal * ss.cfg.gwvw / self.states.bpr |
| 12566 |
if kernel["BufferStore"]: |
| 12567 |
addr0 = vgpr(addrCalc.addrDVgpr) |
| 12568 |
addr1 = sgpr("SrdWS", 4) |
| 12569 |
else: |
| 12570 |
addr0 = vgpr(addrCalc.addrDVgpr,2) |
| 12571 |
addr1 = "" |
| 12572 |
dataType = kernel["ProblemType"]["ComputeDataType"] |
| 12573 |
globalOffset = 0 |
| 12574 |
elif tc == 'Bias': |
| 12575 |
bps = self.states.bpeCinternal * ss.cfg.gwvw |
| 12576 |
rpv = self.states.bpeCinternal * ss.cfg.gwvw / self.states.bpr |
| 12577 |
|
| 12578 |
if kernel["BufferStore"]: |
| 12579 |
addr0 = vgpr(addrCalc.addrBiasVgpr) |
| 12580 |
addr1 = sgpr("Srd%s"%tc, 4) |
| 12581 |
else: |
| 12582 |
addr0 = vgpr(addrCalc.addrBiasVgpr,2) |
| 12583 |
addr1 = "" |
| 12584 |
if ss.optSrdIncForRow and addrCalc.rowInc: |
| 12585 |
module.add(addrCalc.incrementToNextRow(kernel, tc, ss, tmpS01, bpeType=self.states.bpeCinternal)) |
| 12586 |
dataType = kernel["ProblemType"]["ComputeDataType"] |
| 12587 |
globalOffset = addrCalc.globalOffsetInternal |
| 12588 |
elif tc == 'E': |
| 12589 |
bps = self.states.bpeE * ss.cfg.gwvw |
| 12590 |
rpv = self.states.bpeE * ss.cfg.gwvw / self.states.bpr |
| 12591 |
|
| 12592 |
if kernel["BufferStore"]: |
| 12593 |
addr0 = vgpr(addrCalc.addrEVgpr) |
| 12594 |
addr1 = sgpr("Srd%s"%tc, 4) |
| 12595 |
else: |
| 12596 |
addr0 = vgpr(addrCalc.addrEVgpr,2) |
| 12597 |
addr1 = "" |
| 12598 |
if ss.optSrdIncForRow and addrCalc.rowInc: |
| 12599 |
module.add(addrCalc.incrementToNextRow(kernel, tc, ss, tmpS01, bpeType=self.states.bpeE)) |
| 12600 |
dataType = kernel["ProblemType"]["DataTypeE"] |
| 12601 |
globalOffset = addrCalc.globalOffsetE |
| 12602 |
else: |
| 12603 |
printExit("Unsupported store tc %s"%tc) |
| 12604 |
|
| 12605 |
useBuffer = kernel["BufferStore"] |
| 12606 |
if dataType.isHalf() or dataType.isBFloat16(): |
| 12607 |
if not kernel["ProblemType"]["HighPrecisionAccumulate"]: |
| 12608 |
|
| 12609 |
if self.states.asmCaps["HasWMMA_V1"] and kernel["EnableMatrixInstruction"]: |
| 12610 |
module.add(self.chooseGlobalWrite(useBuffer, bps, sumIdx, rpv, \ |
| 12611 |
addr0, addr1, globalOffset, soffset=wsOffset, \ |
| 12612 |
glc=isGlc, slc=isSlc, nt=isNT, hi16=0, comment=comment)) |
| 12613 |
else: |
| 12614 |
module.add(self.chooseGlobalWrite(useBuffer, bps, sumIdx//2, rpv, \ |
| 12615 |
addr0, addr1, globalOffset, soffset=wsOffset, \ |
| 12616 |
glc=isGlc, slc=isSlc, nt=isNT, hi16=sumIdx%2, comment=comment)) |
| 12617 |
else: |
| 12618 |
|
| 12619 |
|
| 12620 |
|
| 12621 |
module.add(self.chooseGlobalWrite(useBuffer, bps, sumIdx, rpv, \ |
| 12622 |
addr0, addr1, globalOffset, soffset=wsOffset, \ |
| 12623 |
glc=isGlc, slc=isSlc, nt=isNT, hi16=0, comment=comment)) |
| 12624 |
elif dataType.isInt32() or dataType.isSingle(): |
| 12625 |
module.add(self.chooseGlobalWrite(useBuffer, bps, sumIdx, rpv, \ |
| 12626 |
addr0, addr1, globalOffset, soffset=wsOffset, \ |
| 12627 |
glc=isGlc, slc=isSlc, nt=isNT, comment=comment)) |
| 12628 |
elif dataType.isDouble() or dataType.isSingleComplex(): |
| 12629 |
module.add(self.chooseGlobalWrite(useBuffer, bps, sumIdx*2, rpv, \ |
| 12630 |
addr0, addr1, globalOffset, soffset=wsOffset, \ |
| 12631 |
glc=isGlc, slc=isSlc, nt=isNT, comment=comment)) |
| 12632 |
elif dataType.isDoubleComplex(): |
| 12633 |
rps = dataType.numRegisters() |
| 12634 |
module.add(self.chooseGlobalWrite(useBuffer, bps, sumIdx*rps, rpv, \ |
| 12635 |
addr0, addr1, globalOffset, soffset=wsOffset, \ |
| 12636 |
glc=isGlc, slc=isSlc, nt=isNT, comment=comment)) |
| 12637 |
elif dataType.isInt8() or dataType.isAnyFloat8() or dataType.isAnyBFloat8() or dataType.isAnyFloat8BFloat8() or dataType.isAnyBFloat8Float8(): |
| 12638 |
if kernel["ProblemType"]["HighPrecisionAccumulate"]: |
| 12639 |
module.add(self.chooseGlobalWrite(useBuffer, bps, sumIdx, rpv, \ |
| 12640 |
addr0, addr1, globalOffset, soffset=wsOffset, \ |
| 12641 |
glc=isGlc, slc=isSlc, nt=isNT, comment=comment)) |
| 12642 |
return module |
| 12643 |
|
| 12644 |
|
| 12645 |
|
| 12646 |
|
| 12647 |
def readInput(self, kernel, ss, tc: str, dataType, addrCalc, vc0, data, gwvw, addr, tmpS01): |
| 12648 |
module = Module("read%sInput"%tc) |
| 12649 |
bps = dataType.numBytes() * gwvw |
| 12650 |
useBuffer = kernel["BufferStore"] |
| 12651 |
|
| 12652 |
if kernel["BufferStore"]: |
| 12653 |
addr0 = vgpr(addr) |
| 12654 |
addr1 = sgpr("Srd%s"%tc, 4) |
| 12655 |
else: |
| 12656 |
addr0 = vgpr(addr,2) |
| 12657 |
addr1 = "" |
| 12658 |
|
| 12659 |
isGlc = bool(kernel["NonTemporal%s"%tc] & 0x1) |
| 12660 |
isSlc = bool(kernel["NonTemporal%s"%tc] & 0x2) |
| 12661 |
isNT = bool(kernel["NonTemporal%s"%tc] & 0x4) |
| 12662 |
|
| 12663 |
soffset = 0 |
| 12664 |
if tc == 'E': |
| 12665 |
globalOffset = addrCalc.globalOffsetE |
| 12666 |
bpeType = self.states.bpeE |
| 12667 |
elif tc == 'WS': |
| 12668 |
soffset = tmpS01 |
| 12669 |
globalOffset = 0 |
| 12670 |
bpeType = self.states.bpeCinternal |
| 12671 |
else: |
| 12672 |
if dataType == kernel["ProblemType"]["ComputeDataType"]: |
| 12673 |
globalOffset = addrCalc.globalOffsetInternal |
| 12674 |
bpeType = self.states.bpeCinternal |
| 12675 |
else: |
| 12676 |
globalOffset = addrCalc.globalOffset |
| 12677 |
bpeType = self.states.bpeCexternal |
| 12678 |
if tc == 'C': |
| 12679 |
if kernel["_GlobalAccumulation"] == "MultipleBufferSingleKernel": |
| 12680 |
globalOffset = int((globalOffset/self.states.bpeCexternal) * self.states.bpr * kernel["ProblemType"]["DestDataType"].numRegisters()) |
| 12681 |
|
| 12682 |
isWorkspace = tc == 'WS' |
| 12683 |
if ss.optSrdIncForRow and addrCalc.rowInc and not isWorkspace: |
| 12684 |
module.add(addrCalc.incrementToNextRow(kernel, tc, ss, tmpS01, bpeType=bpeType)) |
| 12685 |
|
| 12686 |
if dataType.isHalf(): |
| 12687 |
hi16 = 0 if self.states.HHH_WMMA else (vc0 % 2) |
| 12688 |
module.add(self.chooseGlobalRead(useBuffer, bps, data, \ |
| 12689 |
addr0, addr1, soffset=soffset, offset=globalOffset, \ |
| 12690 |
glc=isGlc, slc=isSlc, nt=isNT, lds=False, hi16=hi16, \ |
| 12691 |
comment="load %s"%tc)) |
| 12692 |
elif dataType.isInt8() or dataType.is8bitFloat(): |
| 12693 |
module.add(self.chooseGlobalRead(useBuffer, bps, data, \ |
| 12694 |
addr0, addr1, soffset=soffset, offset=globalOffset, \ |
| 12695 |
glc=isGlc, slc=isSlc, nt=isNT, lds=False, \ |
| 12696 |
|
| 12697 |
comment="load %s"%tc)) |
| 12698 |
elif dataType.isBFloat16() or \ |
| 12699 |
dataType.isInt32() or \ |
| 12700 |
dataType.isSingle() or \ |
| 12701 |
dataType.isDouble() or \ |
| 12702 |
dataType.isSingleComplex() or \ |
| 12703 |
dataType.isDoubleComplex(): |
| 12704 |
module.add(self.chooseGlobalRead(useBuffer, bps, data, \ |
| 12705 |
addr0, addr1, soffset=soffset, offset=globalOffset, \ |
| 12706 |
glc=isGlc, slc=isSlc, nt=isNT, lds=False, \ |
| 12707 |
comment="load %s"%tc)) |
| 12708 |
|
| 12709 |
return module |
| 12710 |
|
| 12711 |
|
| 12712 |
|
| 12713 |
|
| 12714 |
def globalWriteBatch(self, kernel, tPA, tPB, activation, ss: StoreState, batchIdx, \ |
| 12715 |
applyAlpha, beta, edge, atomic, gwvw, atomicW, \ |
| 12716 |
batchElements, addrE, addrD, addrC, addrBias, \ |
| 12717 |
addrScaleAVec, addrScaleBVec, addrScaleAlphaVec, biasLocalBarrierInit: bool, \ |
| 12718 |
tmpVgpr, tmpVgprDynamic, cvtVgprStruct, activationSetPCStruct, activationTypeStr, \ |
| 12719 |
batchElementSgprs, tmpSgpr, codeAccVgprRead, codeMulAlpha, factorDim) -> Module: |
| 12720 |
packdata = Component.PackData.find(self) |
| 12721 |
gwriter = Component.GlobalWriteComponents.find(self) |
| 12722 |
return gwriter(kernel, tPA, tPB, activation, ss, \ |
| 12723 |
batchIdx, applyAlpha, beta, edge, atomic, gwvw, atomicW, \ |
| 12724 |
batchElements, addrE, addrD, addrC, addrBias, \ |
| 12725 |
addrScaleAVec, addrScaleBVec, addrScaleAlphaVec, biasLocalBarrierInit, \ |
| 12726 |
tmpVgpr, tmpVgprDynamic, cvtVgprStruct, activationSetPCStruct, activationTypeStr, \ |
| 12727 |
batchElementSgprs, tmpSgpr, codeAccVgprRead, codeMulAlpha, packdata, self, factorDim, \ |
| 12728 |
self.assembler.version) |
| 12729 |
|
| 12730 |
|
| 12731 |
def openPrefetchGlobalRead2(self, kernel): |
| 12732 |
imod = Module() |
| 12733 |
loopCounter = self.loopCounter(kernel, self.states.unrollIdx) |
| 12734 |
imod.add(SCmpEQU32(src0=loopCounter, src1=hex(1), comment="PGR=2 but only 1 loop")) |
| 12735 |
skipPGR2 = Label(self.labels.getName("skipPGR2"), "") |
| 12736 |
imod.add(SCBranchSCC1(labelName=skipPGR2.getLabelName(), comment="PGR=2 but only 1 loop")) |
| 12737 |
return imod |
| 12738 |
|
| 12739 |
def closePrefetchGlobalRead2(self): |
| 12740 |
imod = Module() |
| 12741 |
skipPGR2 = Label(self.labels.getName("skipPGR2"), "") |
| 12742 |
imod.add(skipPGR2) |
| 12743 |
return imod |
| 12744 |
|
| 12745 |
|
| 12746 |
|
| 12747 |
|
| 12748 |
def calculateVectorGlobalOffset(self, kernel, offsetVgpr, tmpSgpr, dim): |
| 12749 |
module = Module("") |
| 12750 |
def calculateVectorGlobalOffsetCommon(s): |
| 12751 |
module.add(SMulI32(dst=sgpr(s), src0=kernel["MacroTile%d"%dim], src1=sgpr("WorkGroup%d"%dim), comment="wgp%d * MT%d"%(dim, dim))) |
| 12752 |
module.add(VAddU32(dst=vgpr(offsetVgpr), src0=sgpr(s), src1=vgpr("Serial"), comment=f"coord {dim} = wgp{dim} * MT{dim} + thread offset")) |
| 12753 |
if tmpSgpr: |
| 12754 |
calculateVectorGlobalOffsetCommon(tmpSgpr) |
| 12755 |
else: |
| 12756 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 12757 |
tmpSgpr = tmpSgprInfo.idx |
| 12758 |
calculateVectorGlobalOffsetCommon(tmpSgpr) |
| 12759 |
return module |
| 12760 |
|
| 12761 |
def calculateVectorGlobalStride(self, offsetInVgpr, offsetOutVgpr, tmpSgpr, dim, strideName:str): |
| 12762 |
module = Module("") |
| 12763 |
def calculateVectorGlobalStrideCommon(s): |
| 12764 |
module.add(SMulI32(dst=sgpr(s), src0=sgpr(strideName), src1=sgpr("WorkGroup2"), comment="Stride * WG")) |
| 12765 |
module.add(VAddU32(dst=vgpr(offsetOutVgpr), src0=sgpr(s), src1=vgpr(offsetInVgpr), comment=f"coord {dim} = wgp{dim} * MT{dim} + thread offset + Stride * WG")) |
| 12766 |
if tmpSgpr: |
| 12767 |
calculateVectorGlobalStrideCommon(tmpSgpr) |
| 12768 |
else: |
| 12769 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 12770 |
tmpSgpr = tmpSgprInfo.idx |
| 12771 |
calculateVectorGlobalStrideCommon(tmpSgpr) |
| 12772 |
return module |
| 12773 |
|
| 12774 |
def getGlobalShiftOffset(self, kernel, dataType, gwvw): |
| 12775 |
numVgprs = int(ceil(dataType.numRegisters() * gwvw)) |
| 12776 |
reg = dataType.numRegisters() if dataType.numRegisters() >= kernel["ProblemType"]["ComputeDataType"].numRegisters() \ |
| 12777 |
else kernel["ProblemType"]["ComputeDataType"].numRegisters() |
| 12778 |
return gwvw * reg - numVgprs |
| 12779 |
|
| 12780 |
def getTurn(self, kernel, gwvw, dim): |
| 12781 |
divisor = kernel["SubGroup0"] * kernel["SubGroup1"] |
| 12782 |
turn = ceil(kernel["MacroTile%d"%dim] / (divisor * gwvw)) |
| 12783 |
return turn, divisor |
| 12784 |
|
| 12785 |
def addVectorGlobalLoad(self, kernel, srdName: str, offsetVgpr, shiftOffset, dataType, bpe, gwvw, tmpVgpr1Res: ContinuousRegister, dstOffset, dim): |
| 12786 |
module = Module("") |
| 12787 |
tmpVgpr1 = tmpVgpr1Res.idx + dstOffset |
| 12788 |
turn, divisor = self.getTurn(kernel, gwvw, dim) |
| 12789 |
addr0 = vgpr(offsetVgpr) |
| 12790 |
addr1 = sgpr("Srd%s"%srdName, 4) |
| 12791 |
offset = (divisor * gwvw) * bpe |
| 12792 |
|
| 12793 |
for i in range(turn): |
| 12794 |
if i != 0: |
| 12795 |
module.add(VAddU32(dst=vgpr(offsetVgpr), src0=offset, src1=vgpr(offsetVgpr), comment="add subgroup offset")) |
| 12796 |
module.add(self.addVecGlobalLoad(dataType, kernel, tmpVgpr1 + shiftOffset, addr0, addr1, 0, gwvw, comment="Load %s"%srdName)) |
| 12797 |
|
| 12798 |
tmpVgpr1 += 1 |
| 12799 |
return module |
| 12800 |
|
| 12801 |
def addVectorLocalStore(self, kernel, addressStr: str, offsetVgpr, shiftOffset, dataType, gwvw, tmpVgpr1Res: ContinuousRegister, srcOffset, subGroupOffset, dim, setToOne=False, comment=""): |
| 12802 |
module = Module("") |
| 12803 |
tmpVgpr1 = tmpVgpr1Res.idx + srcOffset |
| 12804 |
turn, divisor = self.getTurn(kernel, gwvw, dim) |
| 12805 |
offset = (divisor * gwvw) * self.states.bpeCinternal |
| 12806 |
|
| 12807 |
if setToOne: |
| 12808 |
module.add(VCmpGtU32(dst=sgpr("Address%s"%addressStr, self.states.laneSGPRCount), src0=sgpr("Srd%s+2"%addressStr), src1=0, comment=" == 0 ?")) |
| 12809 |
|
| 12810 |
if kernel["ProblemType"]["ComputeDataType"].isSingle(): |
| 12811 |
maskConst = 1.0 |
| 12812 |
elif kernel["ProblemType"]["ComputeDataType"].isInt32(): |
| 12813 |
maskConst = 1 |
| 12814 |
|
| 12815 |
turnOffset = 0 |
| 12816 |
for i in range(turn): |
| 12817 |
if i != 0: |
| 12818 |
turnOffset += offset |
| 12819 |
bps = kernel["ProblemType"]["ComputeDataType"].numBytes() * gwvw |
| 12820 |
ds = DSModifiers(offset=(subGroupOffset[0] + turnOffset)) |
| 12821 |
dst = vgpr(offsetVgpr) |
| 12822 |
for vi in range(gwvw): |
| 12823 |
|
| 12824 |
shiftOffset2 = shiftOffset + int(vi * dataType.numRegisters()) |
| 12825 |
if kernel["ProblemType"]["ComputeDataType"].isSingle(): |
| 12826 |
if dataType.isHalf(): |
| 12827 |
module.add(VCvtF16toF32(dst=vgpr(tmpVgpr1 + vi + i * gwvw), src=vgpr(tmpVgpr1 + shiftOffset2+ i * gwvw), comment="convert to FP32")) |
| 12828 |
elif dataType.isBFloat16(): |
| 12829 |
module.add(VCvtBF16toFP32(dst=vgpr(tmpVgpr1 + vi + i * gwvw), src=vgpr(tmpVgpr1 + shiftOffset2+ i * gwvw), vgprMask=None, vi=0)) |
| 12830 |
elif dataType == kernel["ProblemType"]["ComputeDataType"]: |
| 12831 |
pass |
| 12832 |
else: |
| 12833 |
printExit("[Compute fp32] Unrecognized data type %s."%str(dataType)) |
| 12834 |
elif kernel["ProblemType"]["ComputeDataType"].isInt32(): |
| 12835 |
if dataType == kernel["ProblemType"]["ComputeDataType"]: |
| 12836 |
pass |
| 12837 |
else: |
| 12838 |
printExit("[Compute int32] Unrecognized data type %s."%str(dataType)) |
| 12839 |
else: |
| 12840 |
printExit("Does not support ComputeDataType == %s"%str(kernel["ProblemType"]["ComputeDataType"])) |
| 12841 |
if setToOne: |
| 12842 |
module.add(VCndMaskB32( |
| 12843 |
dst=vgpr(tmpVgpr1 + i * gwvw), \ |
| 12844 |
src1=vgpr(tmpVgpr1 + i * gwvw), \ |
| 12845 |
src0=maskConst, \ |
| 12846 |
src2=sgpr("Address%s"%addressStr, self.states.laneSGPRCount), \ |
| 12847 |
comment="1. mul 1 if 0")) |
| 12848 |
if bps==2: |
| 12849 |
module.add(DSStoreB16(dstAddr=dst, src=vgpr(tmpVgpr1 + i * gwvw), ds=ds, comment=comment)) |
| 12850 |
elif bps==4: |
| 12851 |
module.add(DSStoreB32(dstAddr=dst, src=vgpr(tmpVgpr1 + i * gwvw), ds=ds, comment=comment)) |
| 12852 |
elif bps==8: |
| 12853 |
module.add(DSStoreB64(dstAddr=dst, src=vgpr(tmpVgpr1 + i * gwvw * 2, 2), ds=ds, comment=comment)) |
| 12854 |
else: |
| 12855 |
assert 0 |
| 12856 |
return module |
| 12857 |
|
| 12858 |
def getNumOfTempVgprs(self, vectorDataTypes: VectorDataTypes, kernel, gwvw, dim): |
| 12859 |
biasDataType = vectorDataTypes.bias(dim).dataType |
| 12860 |
scaleADataType = vectorDataTypes.scaleA.dataType |
| 12861 |
scaleBDataType = vectorDataTypes.scaleB.dataType |
| 12862 |
scaleAlphaDataType = vectorDataTypes.scaleAlpha(dim).dataType |
| 12863 |
|
| 12864 |
|
| 12865 |
totalReg = 0 |
| 12866 |
regPerVec = gwvw * kernel["ProblemType"]["ComputeDataType"].numRegisters() |
| 12867 |
if biasDataType: |
| 12868 |
vectorDataTypes.bias(dim).dstVgpr = totalReg |
| 12869 |
vectorDataTypes.bias(dim).turn = self.getTurn(kernel, gwvw, dim)[0] |
| 12870 |
totalReg = totalReg + (self.getTurn(kernel, gwvw, dim)[0] * regPerVec) |
| 12871 |
if scaleAlphaDataType: |
| 12872 |
vectorDataTypes.scaleAlpha(dim).dstVgpr = totalReg |
| 12873 |
vectorDataTypes.scaleAlpha(dim).turn = self.getTurn(kernel, gwvw, dim)[0] |
| 12874 |
totalReg = totalReg + (self.getTurn(kernel, gwvw, dim)[0] * regPerVec) |
| 12875 |
if scaleADataType: |
| 12876 |
vectorDataTypes.scaleA.dstVgpr = totalReg |
| 12877 |
vectorDataTypes.scaleA.turn = self.getTurn(kernel, gwvw, 0)[0] |
| 12878 |
totalReg = totalReg + (self.getTurn(kernel, gwvw, 0)[0] * regPerVec) |
| 12879 |
if scaleBDataType: |
| 12880 |
vectorDataTypes.scaleB.dstVgpr = totalReg |
| 12881 |
vectorDataTypes.scaleB.turn = self.getTurn(kernel, gwvw, 1)[0] |
| 12882 |
totalReg = totalReg + (self.getTurn(kernel, gwvw, 1)[0] * regPerVec) |
| 12883 |
|
| 12884 |
|
| 12885 |
tmpVgprNum = 0 |
| 12886 |
offsetVgprStart = totalReg |
| 12887 |
|
| 12888 |
dimKey = {} |
| 12889 |
if biasDataType: |
| 12890 |
vectorDataTypes.bias(dim).offsetVgpr = offsetVgprStart |
| 12891 |
tmpVgprNum = tmpVgprNum + 1 |
| 12892 |
if scaleAlphaDataType: |
| 12893 |
if (scaleAlphaDataType, 1) in dimKey: |
| 12894 |
vectorDataTypes.scaleAlpha(dim).offsetVgpr = dimKey[(scaleAlphaDataType, dim)] |
| 12895 |
else: |
| 12896 |
vectorDataTypes.scaleAlpha(dim).offsetVgpr = offsetVgprStart + tmpVgprNum |
| 12897 |
tmpVgprNum = tmpVgprNum + 1 |
| 12898 |
if scaleADataType: |
| 12899 |
if (scaleADataType, 0) in dimKey: |
| 12900 |
vectorDataTypes.scaleA.offsetVgpr = dimKey[(scaleADataType, dim)] |
| 12901 |
else: |
| 12902 |
vectorDataTypes.scaleA.offsetVgpr = offsetVgprStart + tmpVgprNum |
| 12903 |
tmpVgprNum = tmpVgprNum + 1 |
| 12904 |
if scaleBDataType: |
| 12905 |
if (scaleBDataType, 1) in dimKey: |
| 12906 |
vectorDataTypes.scaleB.offsetVgpr = dimKey[(scaleBDataType, dim)] |
| 12907 |
else: |
| 12908 |
vectorDataTypes.scaleB.offsetVgpr = offsetVgprStart + tmpVgprNum |
| 12909 |
tmpVgprNum = tmpVgprNum + 1 |
| 12910 |
return totalReg + tmpVgprNum |
| 12911 |
|
| 12912 |
def readVectorToLDS(self, vectorDataTypes: VectorDataTypes, kernel, gwvw, offsetVgpr, tmpVgpr1Res: ContinuousRegister, dim, tmpSgpr = None): |
| 12913 |
assert gwvw == 1 |
| 12914 |
|
| 12915 |
biasDataType = vectorDataTypes.bias(dim).dataType |
| 12916 |
scaleADataType = vectorDataTypes.scaleA.dataType |
| 12917 |
scaleBDataType = vectorDataTypes.scaleB.dataType |
| 12918 |
scaleAlphaDataType = vectorDataTypes.scaleAlpha(dim).dataType |
| 12919 |
biasBpe = int(self.states.bpr * biasDataType.numRegisters()) if biasDataType else 0 |
| 12920 |
scaleABpe = int(self.states.bpr * scaleADataType.numRegisters()) if scaleADataType else 0 |
| 12921 |
scaleBBpe = int(self.states.bpr * scaleBDataType.numRegisters()) if scaleBDataType else 0 |
| 12922 |
scaleAlphaBpe = int(self.states.bpr * scaleAlphaDataType.numRegisters()) if scaleAlphaDataType else 0 |
| 12923 |
biasDstVgpr = vectorDataTypes.bias(dim).dstVgpr |
| 12924 |
scaleADstVgpr = vectorDataTypes.scaleA.dstVgpr |
| 12925 |
scaleBDstVgpr = vectorDataTypes.scaleB.dstVgpr |
| 12926 |
scaleAlphaDstVgpr = vectorDataTypes.scaleAlpha(dim).dstVgpr |
| 12927 |
biasOffsetVgpr = vectorDataTypes.bias(dim).offsetVgpr + tmpVgpr1Res.idx |
| 12928 |
scaleAOffsetVgpr = vectorDataTypes.scaleA.offsetVgpr + tmpVgpr1Res.idx |
| 12929 |
scaleBOffsetVgpr = vectorDataTypes.scaleB.offsetVgpr + tmpVgpr1Res.idx |
| 12930 |
scaleAlphaOffsetVgpr = vectorDataTypes.scaleAlpha(dim).offsetVgpr + tmpVgpr1Res.idx |
| 12931 |
|
| 12932 |
module = Module("ReadVecToLds") |
| 12933 |
module.addComment2("Read vector to LDS") |
| 12934 |
|
| 12935 |
|
| 12936 |
module.addModuleAsFlatItems(self.calculateVectorGlobalOffset(kernel, offsetVgpr, tmpSgpr, dim)) |
| 12937 |
|
| 12938 |
offsetIsInit = {} |
| 12939 |
if biasDataType: |
| 12940 |
|
| 12941 |
module.add(SMulI32(dst=sgpr("SrdBias+2"), src0=hex(biasBpe), src1=sgpr("SrdBias+2"), comment="scaled by BPE")) |
| 12942 |
if biasOffsetVgpr not in offsetIsInit: |
| 12943 |
offsetIsInit[biasOffsetVgpr] = 1 |
| 12944 |
module.addModuleAsFlatItems(self.calculateVectorGlobalStride(offsetVgpr, biasOffsetVgpr, tmpSgpr, dim, "BiasStride")) |
| 12945 |
module.add(VLShiftLeftB32(dst=vgpr(biasOffsetVgpr), \ |
| 12946 |
shiftHex=hex(log2(biasBpe)), \ |
| 12947 |
src=vgpr(biasOffsetVgpr), \ |
| 12948 |
comment="Global bias address scaled by BPE")) |
| 12949 |
offsetSequences = [] |
| 12950 |
if dim == 0: |
| 12951 |
offsetSequences.append([scaleAlphaDataType, scaleAlphaOffsetVgpr, scaleAlphaBpe, "scaleAlpha", dim]) |
| 12952 |
offsetSequences.append([scaleADataType, scaleAOffsetVgpr, scaleABpe, "scaleA", 0]) |
| 12953 |
offsetSequences.append([scaleBDataType, scaleBOffsetVgpr, scaleBBpe, "scaleB", 1]) |
| 12954 |
else: |
| 12955 |
offsetSequences.append([scaleAlphaDataType, scaleAlphaOffsetVgpr, scaleAlphaBpe, "scaleAlpha", dim]) |
| 12956 |
offsetSequences.append([scaleBDataType, scaleBOffsetVgpr, scaleBBpe, "scaleB", 1]) |
| 12957 |
offsetSequences.append([scaleADataType, scaleAOffsetVgpr, scaleABpe, "scaleA", 0]) |
| 12958 |
for index, offsetSequence in enumerate(offsetSequences): |
| 12959 |
if offsetSequence[4] != dim: |
| 12960 |
dimAnother = 1 if dim == 0 else 0 |
| 12961 |
module.addModuleAsFlatItems(self.calculateVectorGlobalOffset(kernel, offsetVgpr, tmpSgpr, dimAnother)) |
| 12962 |
if offsetSequence[0] and (offsetSequence[1] not in offsetIsInit): |
| 12963 |
offsetIsInit[offsetSequence[1]] = 1 |
| 12964 |
module.add(VLShiftLeftB32(dst=vgpr(offsetSequence[1]), \ |
| 12965 |
shiftHex=hex(log2(offsetSequence[2])), \ |
| 12966 |
src=vgpr(offsetVgpr), \ |
| 12967 |
comment="Global %s address scaled by BPE"%offsetSequence[3])) |
| 12968 |
|
| 12969 |
|
| 12970 |
globalLoadsModule = Module("Global Loads") |
| 12971 |
if biasDataType: |
| 12972 |
biasShiftOffset = self.getGlobalShiftOffset(kernel, biasDataType, gwvw) |
| 12973 |
globalLoadsModule.addModuleAsFlatItems(self.addVectorGlobalLoad(kernel, "Bias", biasOffsetVgpr, biasShiftOffset, biasDataType, biasBpe, gwvw, tmpVgpr1Res, biasDstVgpr, dim)) |
| 12974 |
if scaleAlphaDataType: |
| 12975 |
scaleAlphaShiftOffset = self.getGlobalShiftOffset(kernel, scaleAlphaDataType, gwvw) |
| 12976 |
globalLoadsModule.addModuleAsFlatItems(self.addVectorGlobalLoad(kernel, "ScaleAlphaVec", scaleAlphaOffsetVgpr, scaleAlphaShiftOffset, scaleAlphaDataType, scaleAlphaBpe, gwvw, tmpVgpr1Res, scaleAlphaDstVgpr, dim)) |
| 12977 |
if scaleADataType: |
| 12978 |
scaleAShiftOffset = self.getGlobalShiftOffset(kernel, scaleADataType, gwvw) |
| 12979 |
globalLoadsModule.addModuleAsFlatItems(self.addVectorGlobalLoad(kernel, "ScaleA", scaleAOffsetVgpr, scaleAShiftOffset, scaleADataType, scaleABpe, gwvw, tmpVgpr1Res, scaleADstVgpr, 0)) |
| 12980 |
if scaleBDataType: |
| 12981 |
scaleBShiftOffset = self.getGlobalShiftOffset(kernel, scaleBDataType, gwvw) |
| 12982 |
globalLoadsModule.addModuleAsFlatItems(self.addVectorGlobalLoad(kernel, "ScaleB", scaleBOffsetVgpr, scaleBShiftOffset, scaleBDataType, scaleBBpe, gwvw, tmpVgpr1Res, scaleBDstVgpr, 1)) |
| 12983 |
|
| 12984 |
vlcnt = 0 |
| 12985 |
for item in globalLoadsModule.items(): |
| 12986 |
if isinstance(item, MUBUFReadInstruction): |
| 12987 |
vlcnt = vlcnt + 1 |
| 12988 |
module.add(globalLoadsModule) |
| 12989 |
assert vlcnt > 0 |
| 12990 |
|
| 12991 |
|
| 12992 |
|
| 12993 |
module.add(VLShiftLeftB32(dst=vgpr(offsetVgpr), \ |
| 12994 |
shiftHex=hex(log2(self.states.bpeCinternal)), \ |
| 12995 |
src=vgpr("Serial"), \ |
| 12996 |
comment="Local address scaled by BPE")) |
| 12997 |
if kernel["LdsOffsetBias"] != 0: |
| 12998 |
module.add(VAddU32(dst=vgpr(offsetVgpr), \ |
| 12999 |
src0=(kernel["LdsOffsetBias"]), \ |
| 13000 |
src1=vgpr(offsetVgpr), \ |
| 13001 |
comment="add lds offset")) |
| 13002 |
|
| 13003 |
|
| 13004 |
storeModules = Module("Store") |
| 13005 |
subGroupOffset = [0] |
| 13006 |
if biasDataType: |
| 13007 |
vectorDataTypes.bias(dim).ldsOffset = subGroupOffset[0] |
| 13008 |
storeModules.add(self.addVectorLocalStore(kernel, "Bias", offsetVgpr, biasShiftOffset, biasDataType, gwvw, tmpVgpr1Res, biasDstVgpr, subGroupOffset, dim, comment="store bias")) |
| 13009 |
subGroupOffset[0] += kernel["NumThreads"] * kernel["ProblemType"]["ComputeDataType"].numBytes() * vectorDataTypes.bias(dim).turn |
| 13010 |
if scaleAlphaDataType: |
| 13011 |
vectorDataTypes.scaleAlpha(dim).ldsOffset = subGroupOffset[0] |
| 13012 |
storeModules.add(self.addVectorLocalStore(kernel, "ScaleAlphaVec", offsetVgpr, scaleAlphaShiftOffset, scaleAlphaDataType, gwvw, tmpVgpr1Res, scaleAlphaDstVgpr, subGroupOffset, dim, setToOne=True, comment="store scaleAlpha")) |
| 13013 |
subGroupOffset[0] += kernel["NumThreads"] * kernel["ProblemType"]["ComputeDataType"].numBytes() * vectorDataTypes.scaleAlpha(dim).turn |
| 13014 |
if scaleADataType: |
| 13015 |
vectorDataTypes.scaleA.ldsOffset = subGroupOffset[0] |
| 13016 |
storeModules.add(self.addVectorLocalStore(kernel, "ScaleA", offsetVgpr, scaleAShiftOffset, scaleADataType, gwvw, tmpVgpr1Res, scaleADstVgpr, subGroupOffset, 0, setToOne=True, comment="store scaleA")) |
| 13017 |
subGroupOffset[0] += kernel["NumThreads"] * kernel["ProblemType"]["ComputeDataType"].numBytes() * vectorDataTypes.scaleA.turn |
| 13018 |
if scaleBDataType: |
| 13019 |
vectorDataTypes.scaleB.ldsOffset = subGroupOffset[0] |
| 13020 |
storeModules.add(self.addVectorLocalStore(kernel, "ScaleB", offsetVgpr, scaleBShiftOffset, scaleBDataType, gwvw, tmpVgpr1Res, scaleBDstVgpr, subGroupOffset, 1, setToOne=True, comment="store scaleB")) |
| 13021 |
subGroupOffset[0] += kernel["NumThreads"] * kernel["ProblemType"]["ComputeDataType"].numBytes() * vectorDataTypes.scaleB.turn |
| 13022 |
|
| 13023 |
if kernel["LdsOffsetBias"] == 0: |
| 13024 |
module.add(SBarrier(comment="wait for all global loads.")) |
| 13025 |
|
| 13026 |
|
| 13027 |
for storeModule in storeModules.items(): |
| 13028 |
isAdded = False |
| 13029 |
if isinstance(storeModule, Module): |
| 13030 |
for item in storeModule.items(): |
| 13031 |
if (not isAdded) and isinstance(item, (VCvtInstruction, DSStoreInstruction, VCndMaskB32, VLShiftLeftB32, VAndB32)): |
| 13032 |
vlcnt = vlcnt - 1 |
| 13033 |
module.add(SWaitCnt(vlcnt=(vlcnt), comment="wait for global load")) |
| 13034 |
module.add(item) |
| 13035 |
isAdded = True |
| 13036 |
else: |
| 13037 |
module.add(item) |
| 13038 |
|
| 13039 |
if isinstance(item, DSStoreInstruction): |
| 13040 |
isAdded = False |
| 13041 |
else: |
| 13042 |
module.add(storeModule) |
| 13043 |
|
| 13044 |
return module |
| 13045 |
|
| 13046 |
''' |
| 13047 |
Read reduction results from LDS |
| 13048 |
In edge case the gwvw will be set to 1. |
| 13049 |
''' |
| 13050 |
def writeBiasToBlobalLdsRead(self, kernel, offsetVgpr, gwvw, maxKId, outVgpr): |
| 13051 |
module = Module("WriteBiasToGlobalLdsRead") |
| 13052 |
module.add(VLShiftLeftB32(dst=vgpr(offsetVgpr), \ |
| 13053 |
shiftHex=hex(log2(self.states.bpeCinternal)), \ |
| 13054 |
src=vgpr(offsetVgpr), \ |
| 13055 |
comment="Local bias address scaled by BPE")) |
| 13056 |
|
| 13057 |
if kernel["LdsOffsetBias"] != 0: |
| 13058 |
module.add(VAddU32(dst=vgpr(offsetVgpr), \ |
| 13059 |
src0=(kernel["LdsOffsetBias"]), \ |
| 13060 |
src1=vgpr(offsetVgpr), \ |
| 13061 |
comment="add bias lds offset")) |
| 13062 |
|
| 13063 |
srcAddr = vgpr(offsetVgpr) |
| 13064 |
outVgprN = outVgpr |
| 13065 |
if maxKId == 1: |
| 13066 |
gwvwK = 1 |
| 13067 |
bps = kernel["ProblemType"]["ComputeDataType"].numBytes() * gwvw |
| 13068 |
ds = DSModifiers(offset=0) |
| 13069 |
if bps==2: |
| 13070 |
module.add(DSLoadB16(dst=vgpr(outVgprN), src=srcAddr, ds=ds, comment="load bias")) |
| 13071 |
elif bps==4: |
| 13072 |
module.add(DSLoadB32(dst=vgpr(outVgprN), src=srcAddr, ds=ds, comment="load bias")) |
| 13073 |
elif bps==8: |
| 13074 |
gwvwK = 2 |
| 13075 |
module.add(DSLoadB64(dst=vgpr(outVgprN, 2), src=srcAddr, ds=ds, comment="load bias")) |
| 13076 |
else: |
| 13077 |
assert 0 |
| 13078 |
outVgprN += gwvwK |
| 13079 |
else: |
| 13080 |
dsOffset = 0 |
| 13081 |
for _ in range(0, gwvw): |
| 13082 |
gwvwK = 1 |
| 13083 |
idx = 0 |
| 13084 |
while idx < maxKId: |
| 13085 |
gwvwK = 2 if (idx + 1 < maxKId * gwvw) else 1 |
| 13086 |
bps = kernel["ProblemType"]["ComputeDataType"].numBytes() * gwvwK |
| 13087 |
ds = DSModifiers(offset=dsOffset) |
| 13088 |
if bps==2: |
| 13089 |
module.add(DSLoadB16(dst=vgpr(outVgprN), src=srcAddr, ds=ds, comment="load bias")) |
| 13090 |
elif bps==4: |
| 13091 |
module.add(DSLoadB32(dst=vgpr(outVgprN), src=srcAddr, ds=ds, comment="load bias")) |
| 13092 |
elif bps==8: |
| 13093 |
module.add(DSLoadB64(dst=vgpr(outVgprN, 2), src=srcAddr, ds=ds, comment="load bias")) |
| 13094 |
else: |
| 13095 |
assert 0 |
| 13096 |
outVgprN += gwvwK |
| 13097 |
idx += gwvwK |
| 13098 |
dsOffset += bps |
| 13099 |
return module, outVgprN |
| 13100 |
|
| 13101 |
''' |
| 13102 |
This is a tiny version of globalWriteElements for Bias reduction. |
| 13103 |
Every MT saves MTx1 bias data to global. |
| 13104 |
|
| 13105 |
When GSU = 1, the address is set to the bias pointer. |
| 13106 |
When GSU > 1 in multiple buffer mode, the address is set to the work space pointer. |
| 13107 |
|
| 13108 |
Wider DS load is always enabled. |
| 13109 |
Wider global store only enables when freeElementMultiple % gwvw == 0 since each thread only stores 1, 2 elements. |
| 13110 |
''' |
| 13111 |
def writeBiasToGlobal(self, biasDataType, kernel, tP, gwvw, offsetVgpr, tmpSgprRes, tmpVgpr1Res: ContinuousRegister): |
| 13112 |
tile01 = tP["tile01Idx"] |
| 13113 |
mt = kernel["MacroTile%u" % tile01] |
| 13114 |
maxKId = self.states.lraTileProperties[tile01].maxKId |
| 13115 |
assert tmpSgprRes.size >= 1 |
| 13116 |
assert tmpVgpr1Res.size >= kernel["VectorWidthA"] * kernel["ProblemType"]["ComputeDataType"].numRegisters() * maxKId |
| 13117 |
|
| 13118 |
|
| 13119 |
assert gwvw % 2 == 0 or gwvw == 1 |
| 13120 |
|
| 13121 |
|
| 13122 |
biasBpe = int(self.states.bpr * biasDataType.numRegisters()) |
| 13123 |
module = Module("WriteBiasToGlobal") |
| 13124 |
module.addComment2("Write Bias to Global") |
| 13125 |
module.add(SBarrier(comment="wait for bias lds store.")) |
| 13126 |
|
| 13127 |
|
| 13128 |
gsuComponent = Component.GSU.find(self) |
| 13129 |
module.add(gsuComponent.writeBiasToGlobal(self, kernel, biasDataType, tP, tmpSgprRes, biasBpe)) |
| 13130 |
|
| 13131 |
|
| 13132 |
module.add(SMulI32(dst=sgpr("SrdBias+2"), src0=hex(biasBpe), src1=sgpr("SrdBias+2"), comment="scaled by BPE")) |
| 13133 |
|
| 13134 |
|
| 13135 |
|
| 13136 |
module.add(vectorStaticMultiply(vgpr(offsetVgpr), vgpr("Serial"), maxKId, tmpSgprRes, \ |
| 13137 |
"offset = serial * maxKId")) |
| 13138 |
module.add(vectorStaticMultiply(vgpr(offsetVgpr), vgpr(offsetVgpr), gwvw, tmpSgprRes, \ |
| 13139 |
"apply VectorWidth: offset = bnOffset * vw(%u)" % gwvw)) |
| 13140 |
|
| 13141 |
enableEdge = False |
| 13142 |
serialOffsetVgpr = tmpVgpr1Res.idx |
| 13143 |
serialOffsetVgpr2 = tmpVgpr1Res.idx + 1 |
| 13144 |
|
| 13145 |
if not (kernel["BufferLoad"] and kernel["GuaranteeNoPartialA"]) and (kernel["ProblemType"]["BiasSrc"] == "A"): |
| 13146 |
enableEdge = True |
| 13147 |
if not (kernel["BufferLoad"] and kernel["GuaranteeNoPartialB"]) and (kernel["ProblemType"]["BiasSrc"] == "B"): |
| 13148 |
enableEdge = True |
| 13149 |
if kernel["EdgeType"] == "ShiftPtr" and enableEdge: |
| 13150 |
jumpLabel = Label(self.labels.getNameInc("ShiftPtrSkip"), comment="Skip shift ptr") |
| 13151 |
jumpLabelEnd = Label(self.labels.getNameInc("ShiftPtrEnd"), comment="Skip shift ptr end") |
| 13152 |
assert tmpSgprRes.size >= 5 |
| 13153 |
assert tP["glvw"] % gwvw == 0 |
| 13154 |
tmpSgpr = tmpSgprRes.idx |
| 13155 |
tmpSgprShift = ContinuousRegister(idx=tmpSgprRes.idx+2, size=3) |
| 13156 |
margin = tP["glvw"] if tP["rtv"] else 1 |
| 13157 |
module.add(SMulI32(dst=sgpr(tmpSgpr), src0=sgpr(tP["wg"]), src1=kernel[tP["mt"]], comment="WorkGroup[01] * MT")) |
| 13158 |
module.add(SSubU32(dst=sgpr(tmpSgpr), src0=self.sizeRef(tP["idx"]), src1=sgpr(tmpSgpr), \ |
| 13159 |
comment="edge = Size%s - WG*MT"%(tP["tileChar"]))) |
| 13160 |
module.add(SCmpGeU32(src0=sgpr(tmpSgpr), src1=kernel[tP["mt"]], comment="skip shift ptr if edge >= MT")) |
| 13161 |
module.add(SCBranchSCC1(labelName=jumpLabel.getLabelName(), comment="" )) |
| 13162 |
module.add(scalarStaticRemainder(tmpSgpr+1, tmpSgpr+1, tmpSgpr, tP["glvw"], tmpSgprShift, comment="remainder = edge %% glvw(%d)"%tP["glvw"])) |
| 13163 |
module.add(SSubU32(dst=sgpr(tmpSgpr+1), src0=hex(tP["glvw"]), src1=sgpr(tmpSgpr+1), comment="shift = glvw(%d) - remainder"%tP["glvw"])) |
| 13164 |
|
| 13165 |
module.add(self.getSCMPKInstruction("EQU32", tmpSgpr+1, tP["glvw"], comment="if shift == glvw(%d)?"%tP["glvw"])) |
| 13166 |
module.add(SCMovB32(dst=sgpr(tmpSgpr+1), src=0, comment="shift = glvw(%d) ? 0 : NOP"%(tP["glvw"]))) |
| 13167 |
module.add(SSubU32(dst=sgpr(tmpSgpr), src0=sgpr(tmpSgpr), src1=margin, comment="edge -= margin(%u)"%(margin))) |
| 13168 |
module.add(SCMovB32(dst=sgpr(tmpSgpr), src=0, comment="edge = edge < 0 ? 0 : edge")) |
| 13169 |
if gwvw != 1: |
| 13170 |
''' |
| 13171 |
Edge case, the gwvw is set to 1 instead, do boundary check on every element. |
| 13172 |
|
| 13173 |
if gwvw > 1: |
| 13174 |
for g in rangw(gwvw): |
| 13175 |
if(out_of_bound(g)) |
| 13176 |
offset_at_g += shit_offset |
| 13177 |
ds_read vpgr_at_g, offset_at_g |
| 13178 |
else: |
| 13179 |
if(out_of_bound(g)) |
| 13180 |
offset_at_g += shit_offset |
| 13181 |
ds_read vpgr_at_g, offset_at_g |
| 13182 |
''' |
| 13183 |
module.add(vectorStaticMultiply(vgpr(serialOffsetVgpr), vgpr("Serial"), gwvw, tmpSgprShift, comment="serial = serial * gwvw")) |
| 13184 |
for g in range(gwvw): |
| 13185 |
if g != 0: |
| 13186 |
module.add(VAddU32(dst=vgpr(offsetVgpr+g), src0=vgpr(offsetVgpr+(g-1)), src1=maxKId, comment="new offset = offset + maxKId")) |
| 13187 |
module.add(VMulLOU32(dst=vgpr(serialOffsetVgpr2), src0=hex(maxKId), src1=sgpr(tmpSgpr+1), comment="ds_offset = K * offset")) |
| 13188 |
for g in range(gwvw): |
| 13189 |
module.add(VCmpXGeU32(dst=EXEC(), src0=vgpr(serialOffsetVgpr), src1=sgpr(tmpSgpr), comment="needs shift if serial > edge")) |
| 13190 |
module.add(VAddU32(dst=vgpr(offsetVgpr+g), src0=vgpr(offsetVgpr+g), src1=vgpr(serialOffsetVgpr2), comment="real offset = offset + ds_offset")) |
| 13191 |
if g < gwvw - 1: |
| 13192 |
module.add(VAddU32(dst=vgpr(serialOffsetVgpr), src0=1, src1=vgpr(serialOffsetVgpr), comment="inc += 1")) |
| 13193 |
else: |
| 13194 |
module.add(VCmpXGeU32(dst=EXEC(), src0=vgpr("Serial"), src1=sgpr(tmpSgpr), comment="needs shift if serial > edge")) |
| 13195 |
module.add(VMulLOU32(dst=vgpr(serialOffsetVgpr), src0=hex(maxKId), src1=sgpr(tmpSgpr+1), comment="ds_offset = K * offset")) |
| 13196 |
module.add(VAddU32(dst=vgpr(offsetVgpr), src0=vgpr(offsetVgpr), src1=vgpr(serialOffsetVgpr), comment="real offset = offset + ds_offset")) |
| 13197 |
module.add(SMovB64(dst=EXEC(), src=-1, comment="reset mask")) |
| 13198 |
outVgprNext = tmpVgpr1Res.idx |
| 13199 |
|
| 13200 |
for g in range(gwvw): |
| 13201 |
wb2blr, outVgprNext = self.writeBiasToBlobalLdsRead(kernel, offsetVgpr + g, 1, maxKId, outVgprNext) |
| 13202 |
module.add(wb2blr) |
| 13203 |
module.add(SBranch(labelName=jumpLabelEnd.getLabelName(), comment="")) |
| 13204 |
module.add(jumpLabel) |
| 13205 |
|
| 13206 |
wb2blrGwvw, _ = self.writeBiasToBlobalLdsRead(kernel, offsetVgpr, gwvw, maxKId, tmpVgpr1Res.idx) |
| 13207 |
module.add(wb2blrGwvw) |
| 13208 |
module.add(jumpLabelEnd) |
| 13209 |
else: |
| 13210 |
|
| 13211 |
wb2blrGwvw, _ = self.writeBiasToBlobalLdsRead(kernel, offsetVgpr, gwvw, maxKId, tmpVgpr1Res.idx) |
| 13212 |
module.add(wb2blrGwvw) |
| 13213 |
|
| 13214 |
|
| 13215 |
module.add(SWaitCnt(dscnt=0, comment="wait for bias lds load")) |
| 13216 |
|
| 13217 |
''' |
| 13218 |
The vgprs are rearranged in this step. |
| 13219 |
For example, gwvw = 2, k = 2, we have [v6, v7] [v8, v9] |
| 13220 |
v6 = v6 + v7 |
| 13221 |
v7 = v8 + v9 |
| 13222 |
''' |
| 13223 |
tmpVgpr1 = tmpVgpr1Res.idx |
| 13224 |
tmpVgprN = tmpVgpr1 + 1 |
| 13225 |
if maxKId != 1: |
| 13226 |
for gidx in range(0, gwvw): |
| 13227 |
tmpVgprAccum = tmpVgpr1 + gidx |
| 13228 |
if gidx != 0: |
| 13229 |
module.add(VMovB32(dst=vgpr(tmpVgprAccum), src=vgpr(tmpVgprN), comment="Copy address")) |
| 13230 |
tmpVgprN += 1 |
| 13231 |
for idx in range(1, maxKId): |
| 13232 |
if kernel["ProblemType"]["ComputeDataType"].isSingle(): |
| 13233 |
module.add(VAddF32(dst=vgpr(tmpVgprAccum), src0=vgpr(tmpVgprN), src1=vgpr(tmpVgprAccum), comment="Sum K")) |
| 13234 |
else: |
| 13235 |
assert 0 |
| 13236 |
tmpVgprN += 1 |
| 13237 |
|
| 13238 |
freeElementMultiple = kernel["AssertFree%dElementMultiple"%tile01] |
| 13239 |
enablePack = True if (freeElementMultiple % gwvw == 0) else False |
| 13240 |
tmpVgprN = tmpVgpr1 |
| 13241 |
if biasDataType != kernel["ProblemType"]["ComputeDataType"]: |
| 13242 |
bf16CVTVgprStruct = None |
| 13243 |
bf16CVTVgpr = None |
| 13244 |
if biasDataType.isBFloat16(): |
| 13245 |
bf16CVTVgpr = self.vgprPool.checkOut(4) |
| 13246 |
bf16CVTVgprStruct = self.BF16CVTVgprStruct(vgprBf16Temp=bf16CVTVgpr, vgprBf16Mask=(bf16CVTVgpr+1), \ |
| 13247 |
vgprFp32Nan=(bf16CVTVgpr+2), vgprBf16Inc=(bf16CVTVgpr+3)) |
| 13248 |
module.add(VMovB32(vgpr(bf16CVTVgprStruct.vgprBf16Mask), "0xffff0000", comment="mask for pack two bfloat16 element to 32bit" )) |
| 13249 |
module.add(VMovB32(vgpr(bf16CVTVgprStruct.vgprFp32Nan), "0x7fff0000", comment="fp32 Nan" )) |
| 13250 |
module.add(VMovB32(vgpr(bf16CVTVgprStruct.vgprBf16Inc), "0x7fff", comment="rounding bias for bfloat16" )) |
| 13251 |
for vi in range(gwvw): |
| 13252 |
|
| 13253 |
if kernel["ProblemType"]["ComputeDataType"].isSingle(): |
| 13254 |
if biasDataType.isHalf(): |
| 13255 |
module.add(VCvtF32toF16(dst=vgpr(tmpVgprN), src=vgpr(tmpVgprN), comment="convert to FP16")) |
| 13256 |
if vi % 2 == 1 and enablePack: |
| 13257 |
module.add(VPackF16toB32(dst=vgpr(tmpVgprN - 1), src0=vgpr(tmpVgprN - 1), src1=vgpr(tmpVgprN), \ |
| 13258 |
comment="Pack with neighbor")) |
| 13259 |
elif biasDataType.isBFloat16(): |
| 13260 |
module.add(VCmpUF32(dst=sgpr(tmpSgprRes.idx,2), src0=vgpr(tmpVgprN), src1=vgpr(tmpVgprN), comment="check Nan")) |
| 13261 |
module.add(VBfeU32(dst=vgpr(bf16CVTVgprStruct.vgprBf16Temp), src0=vgpr(tmpVgprN), src1=16, src2=1, \ |
| 13262 |
comment="Non-Nan case: store lsb of bf16" )) |
| 13263 |
module.add(VAdd3U32(dst=vgpr(bf16CVTVgprStruct.vgprBf16Temp), src0=vgpr(tmpVgprN), src1=vgpr(bf16CVTVgprStruct.vgprBf16Temp), \ |
| 13264 |
src2=vgpr(bf16CVTVgprStruct.vgprBf16Inc), comment="Non-Nan case: add lsb and the increment for rounding" )) |
| 13265 |
module.add(VCndMaskB32(dst=vgpr(tmpVgprN), src0=vgpr(bf16CVTVgprStruct.vgprBf16Temp), \ |
| 13266 |
src1=vgpr(bf16CVTVgprStruct.vgprFp32Nan), src2=sgpr(tmpSgprRes.idx,2))) |
| 13267 |
module.add(VLShiftRightB32(dst=vgpr(tmpVgprN), shiftHex=16, src=vgpr(tmpVgprN), comment="convert to bf16")) |
| 13268 |
if vi % 2 == 1 and enablePack: |
| 13269 |
module.add(VPackF16toB32(dst=vgpr(tmpVgprN - 1), src0=vgpr(tmpVgprN - 1), src1=vgpr(tmpVgprN), \ |
| 13270 |
comment="Pack with neighbor")) |
| 13271 |
elif biasDataType == kernel["ProblemType"]["ComputeDataType"]: |
| 13272 |
pass |
| 13273 |
else: |
| 13274 |
printExit("Unrecognized bias type %s."%str(biasDataType)) |
| 13275 |
tmpVgprN += 1 |
| 13276 |
else: |
| 13277 |
printExit("Does not support ComputeDataType != float") |
| 13278 |
if bf16CVTVgpr != None: |
| 13279 |
self.vgprPool.checkIn(bf16CVTVgpr) |
| 13280 |
|
| 13281 |
|
| 13282 |
tmpSgpr = tmpSgprRes.idx |
| 13283 |
module.add(SMulI32(dst=sgpr(tmpSgpr), src0=mt, src1=sgpr("WorkGroup%u" % tile01), comment="wgp * MT")) |
| 13284 |
module.add(vectorStaticMultiply(vgpr(offsetVgpr), vgpr("Serial"), gwvw, tmpSgprRes, \ |
| 13285 |
"apply VectorWidth: offset = serial * vw(%u)" % gwvw)) |
| 13286 |
module.add(VAddU32(dst=vgpr(offsetVgpr), src0=sgpr(tmpSgpr), src1=vgpr(offsetVgpr), comment="coord = wgp * MT + thread offset")) |
| 13287 |
module.add(VLShiftLeftB32(dst=vgpr(offsetVgpr), \ |
| 13288 |
shiftHex=hex(log2(biasBpe)), \ |
| 13289 |
src=vgpr(offsetVgpr), \ |
| 13290 |
comment="Global bias address scaled by BPE")) |
| 13291 |
with self.allocTmpSgpr(1, 1) as tmpSgprRes: |
| 13292 |
module.add(SMovB32(dst=sgpr(tmpSgprRes.idx), src=hex(mt//gwvw), comment="%d=%d//%d"%(mt//gwvw, mt, gwvw))) |
| 13293 |
module.add(VCmpXLtU32(dst=EXEC(), src0=vgpr("Serial"), src1=sgpr(tmpSgprRes.idx), comment="if serial < MacroTile%d/gwvw"%tile01)) |
| 13294 |
addr0 = vgpr(offsetVgpr) |
| 13295 |
addr1 = sgpr("SrdBias", 4) |
| 13296 |
dataType = biasDataType |
| 13297 |
useBuffer = kernel["BufferLoad"] |
| 13298 |
tmpVgprN = tmpVgpr1 |
| 13299 |
if enablePack: |
| 13300 |
bps = biasDataType.numBytes() * gwvw |
| 13301 |
rpe = biasDataType.numBytes() / self.states.bpr |
| 13302 |
rpv = rpe * gwvw |
| 13303 |
if dataType.isHalf() or dataType.isBFloat16(): |
| 13304 |
module.add(self.chooseGlobalWrite(useBuffer, bps, tmpVgprN, rpv, \ |
| 13305 |
addr0, addr1, offset=0, hi16=0, comment="global store bias")) |
| 13306 |
elif dataType.isInt32() or dataType.isSingle(): |
| 13307 |
module.add(self.chooseGlobalWrite(useBuffer, bps, tmpVgprN, rpv, \ |
| 13308 |
addr0, addr1, offset=0, comment="global store bias")) |
| 13309 |
elif dataType.isDouble() or dataType.isSingleComplex() : |
| 13310 |
module.add(self.chooseGlobalWrite(useBuffer, bps, tmpVgprN, rpv, \ |
| 13311 |
addr0, addr1, offset=0, comment="global store bias")) |
| 13312 |
else: |
| 13313 |
tmpVgprNStep = max(1, biasDataType.numRegisters()) |
| 13314 |
globalOffset = 0 |
| 13315 |
for gidx in range(0, gwvw): |
| 13316 |
bps = biasDataType.numBytes() |
| 13317 |
rpe = biasDataType.numBytes() / self.states.bpr |
| 13318 |
rpv = rpe |
| 13319 |
if dataType.isHalf() or dataType.isBFloat16(): |
| 13320 |
module.add(self.chooseGlobalWrite(useBuffer, bps, tmpVgprN, rpv, \ |
| 13321 |
addr0, addr1, offset=globalOffset, hi16=0, comment="global store bias")) |
| 13322 |
elif dataType.isInt32() or dataType.isSingle(): |
| 13323 |
module.add(self.chooseGlobalWrite(useBuffer, bps, tmpVgprN, rpv, \ |
| 13324 |
addr0, addr1, offset=globalOffset, comment="global store bias")) |
| 13325 |
elif dataType.isDouble() or dataType.isSingleComplex() : |
| 13326 |
module.add(self.chooseGlobalWrite(useBuffer, bps, tmpVgprN, rpv, \ |
| 13327 |
addr0, addr1, offset=globalOffset, comment="global store bias")) |
| 13328 |
tmpVgprN += tmpVgprNStep |
| 13329 |
globalOffset += biasBpe |
| 13330 |
module.add(SMovB64(dst=EXEC(), src=-1, comment="Reset exec mask")) |
| 13331 |
return module |
| 13332 |
|
| 13333 |
|
| 13334 |
|
| 13335 |
|
| 13336 |
def amax_define_load_res(self) -> Module: |
| 13337 |
module = Module("AmaxD Set and Load") |
| 13338 |
module.addComment0("AmaxD Set and Load") |
| 13339 |
|
| 13340 |
self.amaxVgprIdxVec = self.defineMultiVgprIndex(self.amaxVgprNames, self.amaxVgprSizes, align=1) |
| 13341 |
for i in range(0, len(self.amaxVgprNames)): |
| 13342 |
name = self.amaxVgprNames[i] |
| 13343 |
idx = self.amaxVgprIdxVec[i] |
| 13344 |
module.add(RegSet("v", "vgpr"+name, idx)) |
| 13345 |
|
| 13346 |
module.addSpaceLine() |
| 13347 |
module.add(self.defineSgpr("Src", 4, 4)) |
| 13348 |
module.add(self.defineSgpr("Dst", 4, 4)) |
| 13349 |
module.add(self.defineSgpr("Offset", 1)) |
| 13350 |
module.add(self.defineSgpr("Tmp", 6, 2)) |
| 13351 |
module.add(self.defineSgpr("NumGroup", 1)) |
| 13352 |
module.add(self.defineSgpr("WGIdx", 1)) |
| 13353 |
module.addSpaceLine() |
| 13354 |
|
| 13355 |
|
| 13356 |
self.amaxSgprIdxVec = self.defineMultiSgprIndex(self.amaxSgprArgNames, self.amaxSgprArgSizes, align=4) |
| 13357 |
for name in self.amaxSgprArgNames: |
| 13358 |
module.add(RegSet("s", "sgpr"+name, self.sgprs[name])) |
| 13359 |
module.addSpaceLine() |
| 13360 |
|
| 13361 |
|
| 13362 |
argOffset = self.argLoader.getOffset() |
| 13363 |
argOffset += (self.states.numStoreSgprToLoad + self.states.numStoreSgprToLoad2) * 4 |
| 13364 |
module.add(self.argLoader.loadKernArg("AddrAmaxOut", "KernArgAddress", sgprOffset=hex(argOffset), dword=4)) |
| 13365 |
argOffset += 16 |
| 13366 |
module.add(self.argLoader.loadKernArg("AddressSy", "KernArgAddress", sgprOffset=hex(argOffset), dword=2)) |
| 13367 |
module.add(SMulI32(sgpr("NumGroup"), sgpr("NumWorkGroups0"), sgpr("NumWorkGroups1"), "get total num_wgs")) |
| 13368 |
module.add(SMulI32(sgpr("WGIdx"), sgpr("WorkGroup1"), sgpr("NumWorkGroups0"), "wgId = wg1 * numWG0")) |
| 13369 |
module.add(SAddI32(sgpr("WGIdx"), sgpr("WGIdx"), sgpr("WorkGroup0"), "wgId += wg0")) |
| 13370 |
module.addSpaceLine() |
| 13371 |
module.add(SWaitCnt(kmcnt=0)) |
| 13372 |
module.addSpaceLine() |
| 13373 |
|
| 13374 |
return module |
| 13375 |
|
| 13376 |
def amax_intra_wave_reduction(self, kernel, postfix) -> Module: |
| 13377 |
wave_size = kernel["WavefrontSize"] |
| 13378 |
label = Label(f"permute_{postfix}", f"permute_{postfix}") |
| 13379 |
|
| 13380 |
mod = Module("intra_wave_reduction") |
| 13381 |
mod.addComment0("intra_wave_reduction") |
| 13382 |
|
| 13383 |
mod.add(SMovB32(sgpr("Tmp"), 1)) |
| 13384 |
mod.add(label) |
| 13385 |
mod.addSpaceLine() |
| 13386 |
mod.add(VAddU32(vgpr("Tmp"), sgpr("Tmp"), vgpr("Serial"))) |
| 13387 |
mod.add(VAndB32(vgpr("Tmp"), wave_size-1, vgpr("Tmp"))) |
| 13388 |
mod.add(VLShiftLeftB32(vgpr("Tmp"), 0x2, vgpr("Tmp"))) |
| 13389 |
mod.addSpaceLine() |
| 13390 |
mod.add(DSBPermuteB32(vgpr("AmaxOutB"), vgpr("Tmp"), vgpr("AmaxOut"))) |
| 13391 |
mod.add(SWaitCnt(dscnt=0)) |
| 13392 |
mod.addSpaceLine() |
| 13393 |
|
| 13394 |
mod.add(VMaxF32(vgpr("AmaxOut"), vgpr("AmaxOut"), vgpr("AmaxOutB"))) |
| 13395 |
mod.add(SLShiftLeftB32(sgpr("Tmp"), 1, sgpr("Tmp"))) |
| 13396 |
mod.add(SCmpLtU32(sgpr("Tmp"), wave_size)) |
| 13397 |
mod.add(SCBranchSCC1(label.getLabelName())) |
| 13398 |
mod.addSpaceLine() |
| 13399 |
return mod |
| 13400 |
|
| 13401 |
def amax_inter_wave_reduction(self, kernel) -> Module: |
| 13402 |
wave_size = kernel["WavefrontSize"] |
| 13403 |
numWorkItems = kernel["NumThreads"] |
| 13404 |
amaxOutType = kernel["ProblemType"]["DataTypeAmaxD"] |
| 13405 |
amax_lds_start = kernel["LdsBytesNoAmax"] |
| 13406 |
|
| 13407 |
label_wave_inter = Label("wave_inter", 'wave_inter') |
| 13408 |
label_wave_upper = Label("wave_upper", 'wave_upper') |
| 13409 |
label_wave_lower = Label("wave_lower", 'wave_lower') |
| 13410 |
label_wave_empty = Label("wave_empty", 'wave_empty') |
| 13411 |
label_wave_end = Label("wave_end", 'wave_end') |
| 13412 |
|
| 13413 |
mod = Module("inter_wave_reduction") |
| 13414 |
mod.addComment0("inter_wave_reduction") |
| 13415 |
|
| 13416 |
mod.add(VLShiftRightB32(vgpr("Widx"), int(log2(wave_size)), vgpr("Serial"))) |
| 13417 |
mod.add(SMovB32(sgpr("Offset"), numWorkItems // wave_size)) |
| 13418 |
mod.add(label_wave_inter) |
| 13419 |
mod.add(SLShiftRightB32(sgpr("Offset"), 1, sgpr("Offset"))) |
| 13420 |
mod.add(SCmpEQU32(sgpr("Offset"), 0)) |
| 13421 |
mod.add(SCBranchSCC1(label_wave_end.getLabelName())) |
| 13422 |
mod.add(SLShiftLeftB32(sgpr("Tmp"), 1, sgpr("Offset"))) |
| 13423 |
mod.add(VCmpLtU32(sgpr("Tmp+2",2), vgpr("Widx"), sgpr("Tmp"))) |
| 13424 |
mod.add(VCmpGEU32(sgpr("Tmp+4",2), vgpr("Widx"), sgpr("Offset"))) |
| 13425 |
mod.add(SAndB64(VCC(), sgpr("Tmp+2",2), sgpr("Tmp+4",2))) |
| 13426 |
mod.add(SCBranchVCCNZ(label_wave_upper.getLabelName())) |
| 13427 |
mod.add(VCmpLtU32(VCC(), vgpr("Widx"), sgpr("Offset"))) |
| 13428 |
mod.add(SCBranchVCCNZ(label_wave_lower.getLabelName())) |
| 13429 |
mod.add(SBranch(label_wave_empty.getLabelName())) |
| 13430 |
|
| 13431 |
mod.add(label_wave_upper) |
| 13432 |
mod.add(VSubU32(vgpr("Tmp"), vgpr("Widx"), sgpr("Offset"))) |
| 13433 |
mod.add(VLShiftLeftB32(vgpr("Tmp"), int(log2(amaxOutType.numBytes())), vgpr("Tmp"))) |
| 13434 |
|
| 13435 |
|
| 13436 |
ds = DSModifiers(offset=amax_lds_start) |
| 13437 |
mod.add(DSStoreB32(vgpr("Tmp"), vgpr("AmaxOut"), ds)) |
| 13438 |
mod.add(SWaitCnt(dscnt=0)) |
| 13439 |
mod.add(SBarrier()) |
| 13440 |
mod.add(SBranch(label_wave_inter.getLabelName())) |
| 13441 |
mod.add(label_wave_lower) |
| 13442 |
mod.add(SBarrier()) |
| 13443 |
mod.add(VLShiftLeftB32(vgpr("Tmp"), int(log2(amaxOutType.numBytes())), vgpr("Widx"))) |
| 13444 |
|
| 13445 |
|
| 13446 |
mod.add(DSLoadB32(vgpr("AmaxOutB"), vgpr("Tmp"), ds)) |
| 13447 |
mod.add(SWaitCnt(dscnt=0)) |
| 13448 |
|
| 13449 |
mod.add(VMaxF32(vgpr("AmaxOut"), vgpr("AmaxOut"), vgpr("AmaxOutB"))) |
| 13450 |
mod.add(SBranch(label_wave_inter.getLabelName())) |
| 13451 |
mod.add(label_wave_empty) |
| 13452 |
mod.add(SBarrier()) |
| 13453 |
mod.add(SBranch(label_wave_inter.getLabelName())) |
| 13454 |
mod.add(label_wave_end) |
| 13455 |
mod.addSpaceLine() |
| 13456 |
return mod |
| 13457 |
|
| 13458 |
def amax_broadcast(self, kernel) -> Module: |
| 13459 |
amax_lds_start = kernel["LdsBytesNoAmax"] |
| 13460 |
|
| 13461 |
label_lower = Label("broadcast_lower", f'broadcast_lower') |
| 13462 |
label_end = Label("broadcast_end", f'broadcast_end') |
| 13463 |
|
| 13464 |
mod = Module("broadcast") |
| 13465 |
mod.addComment0("broadcast") |
| 13466 |
mod.add(VCmpEQU32(VCC(), vgpr("Widx"), 0)) |
| 13467 |
mod.add(SCBranchVCCZ(label_lower.getLabelName())) |
| 13468 |
|
| 13469 |
|
| 13470 |
ds = DSModifiers(offset=amax_lds_start) |
| 13471 |
mod.add(DSStoreB32(vgpr("Widx"), vgpr("AmaxOut"), ds)) |
| 13472 |
mod.add(SWaitCnt(dscnt=0)) |
| 13473 |
mod.add(SBarrier()) |
| 13474 |
mod.add(SBranch(label_end.getLabelName())) |
| 13475 |
mod.add(label_lower) |
| 13476 |
mod.add(SBarrier()) |
| 13477 |
mod.add(VMovB32(vgpr("Tmp"), 0)) |
| 13478 |
|
| 13479 |
|
| 13480 |
mod.add(DSLoadB32(vgpr("AmaxOut"), vgpr("Tmp"), ds)) |
| 13481 |
mod.add(SWaitCnt(dscnt=0)) |
| 13482 |
mod.add(label_end) |
| 13483 |
mod.addSpaceLine() |
| 13484 |
mod.addSpaceLine() |
| 13485 |
return mod |
| 13486 |
|
| 13487 |
def amax_output_result(self, kernel) -> Module: |
| 13488 |
wave_size = kernel["WavefrontSize"] |
| 13489 |
amaxInType = kernel["ProblemType"]["ComputeDataType"] |
| 13490 |
amaxOutType = kernel["ProblemType"]["DataTypeAmaxD"] |
| 13491 |
|
| 13492 |
mod = Module("output_result") |
| 13493 |
mod.addComment0("output_result") |
| 13494 |
|
| 13495 |
label_end = Label("end", 'end') |
| 13496 |
label_final_loop = Label("final_loop", 'final_loop') |
| 13497 |
label_final_output = Label("final_output", 'final_output') |
| 13498 |
mod.addSpaceLine() |
| 13499 |
|
| 13500 |
mod.add(VReadfirstlaneB32(sgpr("Tmp"), vgpr("Serial"))) |
| 13501 |
mod.add(SCmpEQU32(sgpr("Tmp"), 0)) |
| 13502 |
mod.add(SCBranchSCC0(label_end.getLabelName())) |
| 13503 |
mod.addSpaceLine() |
| 13504 |
|
| 13505 |
|
| 13506 |
mod.addSpaceLine() |
| 13507 |
mod.add(SCmpEQU32(sgpr("NumGroup"), 1)) |
| 13508 |
mod.add(SCBranchSCC1(label_final_output.getLabelName())) |
| 13509 |
|
| 13510 |
mod.add(SLShiftLeftB32(sgpr("Tmp"), int(log2(amaxInType.numBytes())), sgpr("NumGroup"))) |
| 13511 |
mod.add(SMovB32(sgpr("Dst+0"), sgpr("AddressWk+0"))) |
| 13512 |
mod.add(SMovB32(sgpr("Dst+1"), sgpr("AddressWk+1"))) |
| 13513 |
mod.add(SMovB32(sgpr("Dst+2"), sgpr("Tmp"))) |
| 13514 |
mod.add(SMovB32(sgpr("Dst+3"), "Srd127_96")) |
| 13515 |
|
| 13516 |
mod.add(SLShiftLeftB32(sgpr("Offset"), int(log2(amaxInType.numBytes())), sgpr("WGIdx"))) |
| 13517 |
mod.add(VMovB32(vgpr("Offset"), 0)) |
| 13518 |
|
| 13519 |
|
| 13520 |
mod.add(BufferStoreB32(vgpr("AmaxOut"), vgpr("Offset"), sgpr("Dst",4), sgpr("Offset"), MUBUFModifiers(offen=True, glc=True, slc=True))) |
| 13521 |
mod.add(SWaitCnt(vlcnt=0)) |
| 13522 |
mod.addSpaceLine() |
| 13523 |
|
| 13524 |
mod.add(SSubI32(sgpr("Tmp"), sgpr("NumGroup"), 1)) |
| 13525 |
mod.add(SAtomicDec(sgpr("Tmp"), sgpr("AddressSy",2), SMEMModifiers(glc=True))) |
| 13526 |
mod.add(SWaitCnt(vlcnt=0, kmcnt=0)) |
| 13527 |
mod.add(SCmpEQU32(sgpr("Tmp"), 1)) |
| 13528 |
mod.add(SCBranchSCC0(label_end.getLabelName())) |
| 13529 |
mod.addSpaceLine() |
| 13530 |
|
| 13531 |
mod.add(SLShiftLeftB32(sgpr("Tmp"), int(log2(amaxInType.numBytes())), sgpr("NumGroup"))) |
| 13532 |
mod.add(SMovB32(sgpr("Src+0"), sgpr("AddressWk+0"))) |
| 13533 |
mod.add(SMovB32(sgpr("Src+1"), sgpr("AddressWk+1"))) |
| 13534 |
mod.add(SMovB32(sgpr("Src+2"), sgpr("Tmp"))) |
| 13535 |
mod.add(SMovB32(sgpr("Src+3"), "Srd127_96")) |
| 13536 |
mod.addSpaceLine() |
| 13537 |
|
| 13538 |
mod.add(VLShiftLeftB32(vgpr("Offset"), int(log2(amaxOutType.numBytes())), vgpr("Serial"))) |
| 13539 |
mod.addSpaceLine() |
| 13540 |
|
| 13541 |
mod.add(VMovB32(vgpr("AmaxOut"), "0")) |
| 13542 |
mod.addSpaceLine() |
| 13543 |
mod.add(label_final_loop) |
| 13544 |
|
| 13545 |
|
| 13546 |
mod.add(BufferLoadB32(vgpr(f"Value"), vgpr("Offset"), sgpr("Src",4), 0, MUBUFModifiers(offen=True, glc=True, slc=True))) |
| 13547 |
mod.add(SWaitCnt(vlcnt=0)) |
| 13548 |
mod.addSpaceLine() |
| 13549 |
|
| 13550 |
|
| 13551 |
mod.add(VMaxF32(vgpr("AmaxOut"), vgpr("AmaxOut"), vgpr("Value", isAbs=True))) |
| 13552 |
mod.addSpaceLine() |
| 13553 |
|
| 13554 |
mod.add(SMovB32(sgpr("Tmp"), wave_size * amaxInType.numBytes())) |
| 13555 |
mod.add(VAddU32(vgpr("Offset"), vgpr("Offset"), sgpr("Tmp"))) |
| 13556 |
mod.addSpaceLine() |
| 13557 |
|
| 13558 |
mod.add(SSubI32(sgpr("NumGroup"), sgpr("NumGroup"), wave_size)) |
| 13559 |
mod.add(SCmpGtI32(sgpr("NumGroup"), 0)) |
| 13560 |
mod.add(SCBranchSCC1(label_final_loop.getLabelName())) |
| 13561 |
mod.addSpaceLine() |
| 13562 |
|
| 13563 |
mod.add(self.amax_intra_wave_reduction(kernel, "final")) |
| 13564 |
mod.addSpaceLine() |
| 13565 |
mod.add(label_final_output) |
| 13566 |
|
| 13567 |
mod.add(SMovB32(sgpr("Dst+0"), sgpr("AddrAmaxOut+0"))) |
| 13568 |
mod.add(SMovB32(sgpr("Dst+1"), sgpr("AddrAmaxOut+1"))) |
| 13569 |
mod.add(SMovB32(sgpr("Dst+2"), amaxOutType.numBytes())) |
| 13570 |
mod.add(SMovB32(sgpr("Dst+3"), "Srd127_96")) |
| 13571 |
mod.addSpaceLine() |
| 13572 |
|
| 13573 |
mod.add(VMovB32(vgpr("Offset"), 0)) |
| 13574 |
|
| 13575 |
|
| 13576 |
mod.add(BufferStoreB32(vgpr("AmaxOut"), vgpr("Offset"), sgpr("Dst",4), 0, MUBUFModifiers(offen=True))) |
| 13577 |
mod.addSpaceLine() |
| 13578 |
mod.add(label_end) |
| 13579 |
mod.addSpaceLine() |
| 13580 |
|
| 13581 |
return mod |
| 13582 |
|
| 13583 |
def insertAmaxD(self, kernel): |
| 13584 |
module = Module("AmaxD Output") |
| 13585 |
module.addComment0("AmaxD Output") |
| 13586 |
|
| 13587 |
self.amaxVgprNames = ["Widx", "Offset", "Tmp", "Value"] |
| 13588 |
self.amaxVgprSizes = [1, 1, 1, 1] |
| 13589 |
self.amaxSgprArgNames = ["AddrAmaxOut", "AddressWk", "AddressSy"] |
| 13590 |
self.amaxSgprArgSizes = [2, 2, 2] |
| 13591 |
|
| 13592 |
module.addSpaceLine() |
| 13593 |
module.add(SBarrier()) |
| 13594 |
module.add(self.amax_define_load_res()) |
| 13595 |
module.add(self.amax_intra_wave_reduction(kernel, "middle")) |
| 13596 |
module.add(self.amax_inter_wave_reduction(kernel)) |
| 13597 |
module.add(self.amax_broadcast(kernel)) |
| 13598 |
module.add(self.amax_output_result(kernel)) |
| 13599 |
|
| 13600 |
for i in self.amaxVgprIdxVec: |
| 13601 |
self.vgprPool.checkIn(i) |
| 13602 |
for i in self.amaxSgprIdxVec: |
| 13603 |
self.sgprPool.checkIn(i) |
| 13604 |
|
| 13605 |
return module |
| 13606 |
|
| 13607 |
|
| 13608 |
|
| 13609 |
|
| 13610 |
def initActivationLoop(self, kernel, beta, edge): |
| 13611 |
|
| 13612 |
activationLabelSuffix = self.labels.getNameInc( \ |
| 13613 |
"%s%s"%("_Beta" if beta else "", "_Edge" if edge else "")) |
| 13614 |
activationCDataType = kernel["ProblemType"]["ActivationComputeDataType"] |
| 13615 |
activationType = kernel["ProblemType"]["ActivationType"] |
| 13616 |
activationEndLabel = Label("Activation_End%s"%activationLabelSuffix, "") |
| 13617 |
activationLabelModules = [] |
| 13618 |
activationEnumStrList = [] |
| 13619 |
if kernel["ActivationFuncCall"]: |
| 13620 |
activationLabelModules.append("") |
| 13621 |
activationEnumStrList.append("none") |
| 13622 |
elif (((kernel["GlobalSplitU"] == 1 or kernel["GlobalSplitU"] == -1) or kernel["GlobalSplitUAlgorithm"] == 'MultipleBufferSingleKernel' or kernel["StreamK"] > 0) and kernel["ActivationFused"]) and \ |
| 13623 |
(activationType != 'none'): |
| 13624 |
if activationType in ['all', 'hipblaslt_all']: |
| 13625 |
exportType = ActivationType.Export.GRADONLY if kernel["ProblemType"]["Gradient"] else ActivationType.Export.NORMAL |
| 13626 |
supportedBy = ActivationType.SupportedBy.ALL if activationType == 'all' else ActivationType.SupportedBy.HIPBLASLT |
| 13627 |
activationEnumStrList = ActivationType.getEnumStrList(activationCDataType, supportedBy, exportType=exportType) |
| 13628 |
for _, enumStr in enumerate(activationEnumStrList): |
| 13629 |
activationLabelModule = Label("Activation_%s%s"% (enumStr.capitalize(), activationLabelSuffix), "") |
| 13630 |
activationLabelModules.append(activationLabelModule) |
| 13631 |
else: |
| 13632 |
activationEnumStrList.append(str(kernel["ProblemType"]["ActivationType"]).lower()) |
| 13633 |
else: |
| 13634 |
activationLabelModules.append("") |
| 13635 |
activationEnumStrList.append("none") |
| 13636 |
return activationEndLabel, activationLabelModules, activationEnumStrList |
| 13637 |
|
| 13638 |
def insertActFunctionCallAddrCalc(self, sgprOffset, gwvw, \ |
| 13639 |
toActModuleList, activationEnumStrList, activationLabelList, \ |
| 13640 |
betaIdx = -1, edgeIdx = -1): |
| 13641 |
activationLabelModules = activationLabelList[gwvw] |
| 13642 |
module = Module(getActFuncBranchModuleName()) |
| 13643 |
setAddrEndLabel = Label(self.labels.getNameInc("ActivationSetPCAddrEnd"), "") |
| 13644 |
toActModules = deepcopy(toActModuleList[gwvw]) |
| 13645 |
for index, toActModule in enumerate(toActModules): |
| 13646 |
if betaIdx >= 0 and edgeIdx >= 0: |
| 13647 |
toActModule.label = self.labels.getNameInc(toActModule.label + "_beta_%u_edge_%u"%(betaIdx, edgeIdx)) |
| 13648 |
if index != 0: |
| 13649 |
enumIndex = ActivationType.getEnumIndex(activationEnumStrList[index]) |
| 13650 |
|
| 13651 |
module.add(self.getSCMPKInstruction("EQU32", "ActivationType", enumIndex, comment="activationType == %u"%enumIndex)) |
| 13652 |
module.add(SCBranchSCC1(toActModule.getLabelName(), "Branch if true")) |
| 13653 |
for index, activationLabelModule in enumerate(activationLabelModules): |
| 13654 |
toActModule = toActModules[index] |
| 13655 |
module.add(toActModule) |
| 13656 |
with self.allocTmpSgpr(1) as tmpSgprInfo: |
| 13657 |
module.addModuleAsFlatItems(SGetPositivePCOffset(sgprOffset, activationLabelModule, tmpSgprInfo)) |
| 13658 |
module.add(SBranch(setAddrEndLabel.getLabelName())) |
| 13659 |
module.add(setAddrEndLabel) |
| 13660 |
return module |
| 13661 |
|
| 13662 |
def insertActivationAfterPacked(self, kernel, activationTypeStr): |
| 13663 |
result = False |
| 13664 |
if kernel["ProblemType"]["UseScaleCD"] and ((kernel["GlobalSplitU"] == 1 or kernel["GlobalSplitU"] == -1) or kernel["StreamK"] > 0): |
| 13665 |
return result |
| 13666 |
elif ((kernel["ProblemType"]["ActivationType"] != 'none') and \ |
| 13667 |
((kernel["GlobalSplitU"] == 1 or kernel["GlobalSplitU"] == -1) or kernel["GlobalSplitUAlgorithm"] == 'MultipleBufferSingleKernel' or kernel["StreamK"] > 0) and kernel["ActivationFused"]): |
| 13668 |
if kernel["ActivationFuncCall"]: |
| 13669 |
return (kernel["ProblemType"]["ActivationComputeDataType"] == kernel["ProblemType"]["DestDataType"]) |
| 13670 |
elif kernel["ProblemType"]["DestDataType"].isBFloat16() and (activationTypeStr == 'abs'): |
| 13671 |
result = True |
| 13672 |
elif kernel["ProblemType"]["DestDataType"].isHalf() and \ |
| 13673 |
((activationTypeStr == 'abs') or (activationTypeStr == 'relu')): |
| 13674 |
result = True |
| 13675 |
elif kernel["ProblemType"]["ActivationComputeDataType"] == kernel["ProblemType"]["DestDataType"]: |
| 13676 |
result = True |
| 13677 |
return result |
| 13678 |
|
| 13679 |
def getActivationDestDataType(self, kernel, activation, activationTypeStr: str, gwvw, \ |
| 13680 |
elementSumIdxIn, elementSumIdxOut, tmpVgpr, tmpSgpr): |
| 13681 |
module = Module("ActivationAfterPack") |
| 13682 |
for vi in range(0, gwvw): |
| 13683 |
sumIdxVIn = elementSumIdxIn + vi |
| 13684 |
sumIdxVOut = elementSumIdxOut + vi |
| 13685 |
if kernel["ProblemType"]["DestDataType"].isHalf() or \ |
| 13686 |
kernel["ProblemType"]["DestDataType"].isBFloat16(): |
| 13687 |
if kernel["ProblemType"]["HighPrecisionAccumulate"]: |
| 13688 |
|
| 13689 |
if ((vi + 1) == gwvw) and ((gwvw % 2) == 1): |
| 13690 |
activation.setUsePK(False) |
| 13691 |
|
| 13692 |
elif vi%2 == 1: |
| 13693 |
assert (gwvw % 2 == 0) |
| 13694 |
else: |
| 13695 |
continue |
| 13696 |
vgprIn = elementSumIdxIn + vi//2 |
| 13697 |
vgprOut = elementSumIdxOut + vi//2 |
| 13698 |
|
| 13699 |
else: |
| 13700 |
if (sumIdxVIn % 2 != 0): |
| 13701 |
continue |
| 13702 |
vgprIn = sumIdxVIn // 2 |
| 13703 |
vgprOut = sumIdxVOut // 2 |
| 13704 |
elif kernel["ProblemType"]["DestDataType"].isSingle(): |
| 13705 |
vgprIn = sumIdxVIn |
| 13706 |
vgprOut = sumIdxVOut |
| 13707 |
elif kernel["ProblemType"]["DestDataType"].isDouble(): |
| 13708 |
vgprIn = sumIdxVIn * 2 |
| 13709 |
vgprOut = sumIdxVOut * 2 |
| 13710 |
elif kernel["ProblemType"]["DestDataType"].isInt32(): |
| 13711 |
vgprIn = sumIdxVIn |
| 13712 |
vgprOut = sumIdxVOut |
| 13713 |
else: |
| 13714 |
raise RuntimeError("Unsupported data type %s for activation vgpr index."%str(self.states.kernel["ProblemType"]["DestDataType"])) |
| 13715 |
|
| 13716 |
actModule = activation.getModule(self.states.kernel["ProblemType"]["DestDataType"], activationTypeStr, vgprIn, vgprOut) |
| 13717 |
module.add(activation.assignGpr(actModule, tmpVgpr, tmpSgpr)) |
| 13718 |
activation.setUsePK(True) |
| 13719 |
return module |
| 13720 |
|
| 13721 |
def getActivationActivationComputeType(self, kernel, activation, activationTypeStr: str, gwvw, \ |
| 13722 |
elementSumIdxIn, elementSumIdxOut, tmpVgpr, tmpSgpr, satInt8=False, enableValuCPrefix=False): |
| 13723 |
module = Module("ActivationBeforePack") |
| 13724 |
if satInt8: |
| 13725 |
activation.setSaturationForInt8(True) |
| 13726 |
if enableValuCPrefix: |
| 13727 |
|
| 13728 |
activation.setVgprPrefixFormat("ValuC+%u") |
| 13729 |
for vi in range(0, gwvw): |
| 13730 |
vgprIn = elementSumIdxIn + vi |
| 13731 |
vgprOut = elementSumIdxOut + vi |
| 13732 |
actModule = activation.getModule(kernel["ProblemType"]["ActivationComputeDataType"], activationTypeStr, vgprIn, vgprOut) |
| 13733 |
module.add(activation.assignGpr(actModule, tmpVgpr, tmpSgpr)) |
| 13734 |
activation.setSaturationForInt8(False) |
| 13735 |
activation.setVgprPrefixFormat("") |
| 13736 |
return module |
| 13737 |
|
| 13738 |
|
| 13739 |
|
| 13740 |
|
| 13741 |
def functionEnd(self, kernel, addLabel=True): |
| 13742 |
imod = Module() |
| 13743 |
loopComponent = Component.PersistentLoop.find(self) |
| 13744 |
imod.add(loopComponent.closePersistentLoop(self, kernel)) |
| 13745 |
if addLabel: |
| 13746 |
imod.add(Label("KernelEnd", "")) |
| 13747 |
|
| 13748 |
|
| 13749 |
if kernel["ProblemType"]["OutputAmaxD"]: |
| 13750 |
imod.add(self.insertAmaxD(kernel)) |
| 13751 |
|
| 13752 |
imod.add(SEndpgm(comment="Kernel End")) |
| 13753 |
return imod |
| 13754 |
|
| 13755 |
|
| 13756 |
|
| 13757 |
|
| 13758 |
def getWaitcntCodeForDirectToVgpr(self, kernel, tensorParametersA, tensorParametersB, localWriteEndIter, u, isNLL=False, beforeBarrier=False, NLLlast=False, oddLast=False): |
| 13759 |
module = Module("DTV wait") |
| 13760 |
|
| 13761 |
|
| 13762 |
if kernel["DirectToVgprA"] and kernel["DirectToVgprB"]: |
| 13763 |
needToWait = 0 |
| 13764 |
waitComment = "global read wait for DirectToVgpr" |
| 13765 |
module.add(self.getWaitcntCodeForDTVSub(kernel, tensorParametersA, tensorParametersB, needToWait, waitComment)) |
| 13766 |
elif kernel["DirectToVgprA"] ^ kernel["DirectToVgprB"]: |
| 13767 |
numGlobalReadA = kernel["NumLoadsPerpendicularA"] * kernel["NumLoadsCoalescedA"] |
| 13768 |
numGlobalReadB = kernel["NumLoadsPerpendicularB"] * kernel["NumLoadsCoalescedB"] |
| 13769 |
numReadsIterCoalesced = self.states.numReadsIterCoalescedA if kernel["DirectToVgprA"] else self.states.numReadsIterCoalescedB |
| 13770 |
numReadsIterCoalesced *= kernel["NumLoadsCoalescedA"] if kernel["reorderGRInstForDTVA"] and \ |
| 13771 |
kernel["NumLoadsCoalescedA"] % 2 == 0 else 1 |
| 13772 |
numReadsIterCoalesced *= kernel["NumLoadsCoalescedB"] if kernel["reorderGRInstForDTVB"] and \ |
| 13773 |
kernel["NumLoadsCoalescedB"] % 2 == 0 else 1 |
| 13774 |
|
| 13775 |
numGlobalReadNonDTV = 0 |
| 13776 |
if not kernel["DirectToVgprA"]: |
| 13777 |
numGlobalReadNonDTV += numGlobalReadA |
| 13778 |
if not kernel["DirectToVgprB"]: |
| 13779 |
numGlobalReadNonDTV += numGlobalReadB |
| 13780 |
numGlobalReadDTV = numGlobalReadA + numGlobalReadB - numGlobalReadNonDTV |
| 13781 |
waitComment = "global read wait for DirectToVgpr" |
| 13782 |
|
| 13783 |
numRegsIn1set = (numGlobalReadDTV * numReadsIterCoalesced) // kernel["LoopIters"] |
| 13784 |
numSet = (u + numReadsIterCoalesced) // numReadsIterCoalesced |
| 13785 |
numSetMod = (u + numReadsIterCoalesced) % numReadsIterCoalesced |
| 13786 |
if (not beforeBarrier) and numSetMod > 0: |
| 13787 |
|
| 13788 |
return module |
| 13789 |
needToWait = numGlobalReadDTV - numSet * numRegsIn1set |
| 13790 |
if isNLL: |
| 13791 |
|
| 13792 |
numGlobalReadNonDTV = 0 |
| 13793 |
if kernel["PrefetchGlobalRead"] == 2: |
| 13794 |
|
| 13795 |
needToWait += numGlobalReadNonDTV |
| 13796 |
if u > 0: |
| 13797 |
|
| 13798 |
count = 0 |
| 13799 |
for i in range(u): |
| 13800 |
globalReadStr = ' '.join([str(x) for x in self.codes.perIterGlobalRead[i].flatitems()]) |
| 13801 |
count += countGlobalRead(self.codes.perIterGlobalRead[i]) |
| 13802 |
|
| 13803 |
count += countGlobalRead(self.codes.perIterLocalWrite[i][1]) |
| 13804 |
needToWait += count |
| 13805 |
if u == localWriteEndIter + 1 and beforeBarrier: |
| 13806 |
|
| 13807 |
needToWait -= numGlobalReadNonDTV |
| 13808 |
|
| 13809 |
|
| 13810 |
if oddLast or kernel["ScheduleIterAlg"] < 3: |
| 13811 |
needToWait = 0 |
| 13812 |
|
| 13813 |
|
| 13814 |
module.add(self.getWaitcntCodeForDTVSub(kernel, tensorParametersA, tensorParametersB, needToWait, waitComment)) |
| 13815 |
return module |
| 13816 |
|
| 13817 |
|
| 13818 |
|
| 13819 |
|
| 13820 |
def getWaitcntCodeForPGR(self, kernel, tensorParametersA, tensorParametersB, comment): |
| 13821 |
module = Module("PGR wait") |
| 13822 |
|
| 13823 |
if (kernel["DirectToVgprA"] and kernel["DirectToVgprB"]): |
| 13824 |
|
| 13825 |
return module |
| 13826 |
count = 0 |
| 13827 |
if kernel["DirectToVgprA"]: |
| 13828 |
|
| 13829 |
count += kernel["NumLoadsPerpendicularA"] * kernel["NumLoadsCoalescedA"] |
| 13830 |
if kernel["DirectToVgprB"]: |
| 13831 |
|
| 13832 |
count += kernel["NumLoadsPerpendicularB"] * kernel["NumLoadsCoalescedB"] |
| 13833 |
|
| 13834 |
module.add(self.getWaitcntCodeForDTVSub(kernel, tensorParametersA, tensorParametersB, count, comment)) |
| 13835 |
return module |
| 13836 |
|
| 13837 |
|
| 13838 |
|
| 13839 |
|
| 13840 |
def getWaitcntCodeForDTVSub(self, kernel, tensorParametersA, tensorParametersB, count, waitComment): |
| 13841 |
return self._wait(kernel, tensorParametersA, tensorParametersB, 0, -1, -1, waitComment, skipGlobalReadInst = count) |
| 13842 |
|
| 13843 |
|
| 13844 |
|
| 13845 |
|
| 13846 |
def isSwapGlobalReadOrderForDtvOrDtl(self, kernel, prefetch1=False): |
| 13847 |
|
| 13848 |
if kernel["DirectToVgprA"] and kernel["DirectToVgprB"]: |
| 13849 |
|
| 13850 |
return False |
| 13851 |
elif kernel["DirectToVgprA"]: |
| 13852 |
|
| 13853 |
|
| 13854 |
|
| 13855 |
if kernel["PrefetchGlobalRead"] == 2 and prefetch1: |
| 13856 |
return True |
| 13857 |
elif kernel["PrefetchGlobalRead"] == 1: |
| 13858 |
return True |
| 13859 |
elif kernel["DirectToVgprB"]: |
| 13860 |
|
| 13861 |
|
| 13862 |
if kernel["PrefetchGlobalRead"] == 2 and (not prefetch1): |
| 13863 |
return True |
| 13864 |
elif (not kernel["DirectToLdsA"]) and kernel["DirectToLdsB"]: |
| 13865 |
|
| 13866 |
return True |
| 13867 |
elif kernel["SwapGlobalReadOrder"] == True: |
| 13868 |
return True |
| 13869 |
return False |
| 13870 |
|
| 13871 |
|
| 13872 |
|
| 13873 |
|
| 13874 |
def waveSplitKReduction(self, kernel): |
| 13875 |
module = Module("WaveSplitK Reduction") |
| 13876 |
module.addComment2("WaveSplitK Reduction") |
| 13877 |
|
| 13878 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 13879 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 13880 |
accK = 1 |
| 13881 |
while accK < kernel["NumWaveSplitK"]: |
| 13882 |
|
| 13883 |
if (kernel["ThreadTile1"] == 1 and kernel["ThreadTile0"] == 1) and accK != 1: |
| 13884 |
module.add(SNop(waitState=0, comment="1 wait states")) |
| 13885 |
for block1 in range(0, kernel["ThreadTile1"]): |
| 13886 |
for block0 in range(0, kernel["ThreadTile0"]): |
| 13887 |
vgprIdx = block0 + block1 * kernel["ThreadTile0"] |
| 13888 |
if accK <= 8: |
| 13889 |
module.add(VAddF32(dst=vgpr("ValuC+%d"%vgprIdx), src0=vgpr("ValuC+%d"%vgprIdx), src1=vgpr("ValuC+%d"%vgprIdx), dpp=DPPModifiers(bound_ctrl=0, row_shr=accK), comment="Reduce %s elements"%(accK*2) )) |
| 13890 |
else: |
| 13891 |
module.add(VAddF32(dst=vgpr("ValuC+%d"%vgprIdx), src0=vgpr("ValuC+%d"%vgprIdx), src1=vgpr("ValuC+%d"%vgprIdx), dpp=DPPModifiers(bound_ctrl=0, row_bcast=accK-1), comment="Reduce %s elements"%(accK*2) )) |
| 13892 |
accK *= 2 |
| 13893 |
|
| 13894 |
module.addSpaceLine() |
| 13895 |
return module |
| 13896 |
|
| 13897 |
def simdSpecDispatch(self, kernel, numCodePath): |
| 13898 |
module = Module() |
| 13899 |
|
| 13900 |
loopLabelBegin = [] |
| 13901 |
loopLabelSkipBegin = [] |
| 13902 |
|
| 13903 |
loopChar = self.states.indexChars[kernel["ProblemType"]["IndicesSummation"][self.states.unrollIdx]] |
| 13904 |
|
| 13905 |
if numCodePath == 1: |
| 13906 |
module.add(TextBlock("MAINLOOP 0\n")) |
| 13907 |
module.add(SCBranchSCC0(labelName="label_LoopBegin%s"%(loopChar), comment="" )) |
| 13908 |
module.add(Label("LoopEnd%s"%(loopChar), "" )) |
| 13909 |
return module |
| 13910 |
|
| 13911 |
|
| 13912 |
module.addComment0("SIMD specialized dispatch") |
| 13913 |
|
| 13914 |
for l in range(numCodePath): |
| 13915 |
loopLabelBegin.append(Label("LoopBegin%s_%u"%(loopChar, l), "" )) |
| 13916 |
loopLabelSkipBegin.append(Label("LoopSkipBegin%s_%u"%(loopChar, l), "" )) |
| 13917 |
loopLabelEnd = Label("LoopEnd%s"%(loopChar), "" ) |
| 13918 |
|
| 13919 |
tmpSgpr = self.sgprPool.checkOut(1) |
| 13920 |
module.add(SGetRegB32(dst=sgpr(tmpSgpr), src="hwreg(HW_REG_HW_ID, 4, 1)")) |
| 13921 |
|
| 13922 |
module.add(SWaitCnt(dscnt=0, vlcnt=-1, vscnt=-1, comment="Wait for all PGR to complete")) |
| 13923 |
tmpSgpr1 = self.sgprPool.checkOutAligned(3, 2) |
| 13924 |
sgprPC = ContinuousRegister(tmpSgpr1, 3) |
| 13925 |
for l in range(numCodePath): |
| 13926 |
module.addComment0("Branch to code-path %u logic"%l) |
| 13927 |
module.add(SCmpEQU32(src0=sgpr(tmpSgpr), src1=hex(l), comment="" )) |
| 13928 |
|
| 13929 |
module.add(SCLongBranchScc1(loopLabelBegin[l], sgprPC, \ |
| 13930 |
loopLabelSkipBegin[l].getLabelName(), \ |
| 13931 |
loopLabelBegin[l].getLabelName())) |
| 13932 |
self.sgprPool.checkIn(tmpSgpr1) |
| 13933 |
self.sgprPool.checkIn(tmpSgpr) |
| 13934 |
|
| 13935 |
for l in range(numCodePath): |
| 13936 |
module.addComment0("SIMD %u code-path"%l) |
| 13937 |
module.add(loopLabelBegin[l]) |
| 13938 |
module.add(TextBlock("MAINLOOP %u\n"%l)) |
| 13939 |
module.add(SCBranchSCC0(labelName=loopLabelBegin[l].getLabelName(), comment="" )) |
| 13940 |
tmpSgpr1 = self.sgprPool.checkOutAligned(2, 2) |
| 13941 |
sgprPC = ContinuousRegister(tmpSgpr1, 3) |
| 13942 |
|
| 13943 |
module.add(SLongBranchPositive(loopLabelEnd, sgprPC)) |
| 13944 |
self.sgprPool.checkIn(tmpSgpr1) |
| 13945 |
|
| 13946 |
|
| 13947 |
endStr = "" |
| 13948 |
module.addComment2("Unrolled Loop - End%s"%(endStr)) |
| 13949 |
module.add(loopLabelEnd) |
| 13950 |
|
| 13951 |
return module |
| 13952 |
|
| 13953 |
|
| 13954 |
|
| 13955 |
|
| 13956 |
|
| 13957 |
|
| 13958 |
|
| 13959 |
|
| 13960 |
|
| 13961 |
|
| 13962 |
def longBranchScc0(self, label: Label, posNeg: int=0, tmpSgprInfo: Optional[ContinuousRegister]=None, comment=""): |
| 13963 |
if tmpSgprInfo: |
| 13964 |
return SCLongBranchScc0(label, tmpSgprInfo, \ |
| 13965 |
self.labels.getUniqueNamePrefix("NoBranch"), \ |
| 13966 |
self.labels.getUniqueNamePrefix("Positive"), |
| 13967 |
posNeg, comment) |
| 13968 |
else: |
| 13969 |
with self.allocTmpSgpr(3) as tmpSgprInfo: |
| 13970 |
return SCLongBranchScc0(label, tmpSgprInfo, \ |
| 13971 |
self.labels.getUniqueNamePrefix("NoBranch"), \ |
| 13972 |
self.labels.getUniqueNamePrefix("Positive"), |
| 13973 |
posNeg, comment) |
| 13974 |
|
| 13975 |
|
| 13976 |
|
| 13977 |
|
| 13978 |
|
| 13979 |
|
| 13980 |
def longBranchScc1(self, label: Label, posNeg: int=0, tmpSgprInfo: Optional[ContinuousRegister]=None, comment=""): |
| 13981 |
if tmpSgprInfo: |
| 13982 |
return SCLongBranchScc1(label, tmpSgprInfo, \ |
| 13983 |
self.labels.getUniqueNamePrefix("NoBranch"), \ |
| 13984 |
self.labels.getUniqueNamePrefix("Positive"), |
| 13985 |
posNeg, comment) |
| 13986 |
else: |
| 13987 |
with self.allocTmpSgpr(3) as tmpSgprInfo: |
| 13988 |
return SCLongBranchScc1(label, tmpSgprInfo, \ |
| 13989 |
self.labels.getUniqueNamePrefix("NoBranch"), \ |
| 13990 |
self.labels.getUniqueNamePrefix("Positive"), |
| 13991 |
posNeg, comment) |
| 13992 |
|
| 13993 |
|
| 13994 |
|
| 13995 |
|
| 13996 |
|
| 13997 |
|
| 13998 |
def longBranchVccnz(self, label: Label, posNeg: int=0, tmpSgprInfo: Optional[ContinuousRegister]=None, comment=""): |
| 13999 |
if tmpSgprInfo: |
| 14000 |
return SCLongBranchVccnz(label, tmpSgprInfo, \ |
| 14001 |
self.labels.getUniqueNamePrefix("NoBranch"), \ |
| 14002 |
self.labels.getUniqueNamePrefix("Positive"), |
| 14003 |
posNeg, comment) |
| 14004 |
else: |
| 14005 |
with self.allocTmpSgpr(3) as tmpSgprInfo: |
| 14006 |
return SCLongBranchVccnz(label, tmpSgprInfo, \ |
| 14007 |
self.labels.getUniqueNamePrefix("NoBranch"), \ |
| 14008 |
self.labels.getUniqueNamePrefix("Positive"), |
| 14009 |
posNeg, comment) |
| 14010 |
|
| 14011 |
def sMagicDivWrapper(self, dest, dividend, magicNumber, magicShift): |
| 14012 |
tmpVgpr = self.vgprPool.checkOut(2) |
| 14013 |
tmpVgprRes = ContinuousRegister(tmpVgpr, 2) |
| 14014 |
module = sMagicDiv(dest, self.states.asmCaps["HasSMulHi"], dividend, magicNumber, magicShift, tmpVgprRes) |
| 14015 |
self.vgprPool.checkIn(tmpVgpr) |
| 14016 |
return module |
| 14017 |
|
| 14018 |
def s_mul_u64_u32 (self, dst0, dst1, src0, src1, tmpVgpr=None, comment=""): |
| 14019 |
vtmp0 = self.vgprPool.checkOut(2) if tmpVgpr == None else tmpVgpr |
| 14020 |
tmpVgprRes = ContinuousRegister(vtmp0, 2) |
| 14021 |
module = SMulInt64to32(dst0, dst1, src0, src1, tmpVgprRes, bool(self.states.asmCaps["HasSMulHi"]), False, comment) |
| 14022 |
if tmpVgpr == None: |
| 14023 |
self.vgprPool.checkIn(vtmp0) |
| 14024 |
return module |
| 14025 |
|
| 14026 |
def s_mul_i64_i32 (self, dst0, dst1, src0, src1, comment): |
| 14027 |
vtmp0 = self.vgprPool.checkOut(2) |
| 14028 |
tmpVgprRes = ContinuousRegister(vtmp0, 2) |
| 14029 |
module = SMulInt64to32(dst0, dst1, src0, src1, tmpVgprRes, bool(self.states.asmCaps["HasSMulHi"]), True, comment) |
| 14030 |
self.vgprPool.checkIn(vtmp0) |
| 14031 |
return module |
| 14032 |
|
| 14033 |
def s_mul_i64_i32_u32 (self, dst0, dst1, src0, src1, comment): |
| 14034 |
module = Module("S_MUL_I64_I32_U32") |
| 14035 |
vtmp0 = self.vgprPool.checkOut(2) |
| 14036 |
negativeLabel = Label((self.labels.getUniqueNamePrefix("Negative")), comment="") |
| 14037 |
multiplydoneLabel = Label((self.labels.getUniqueNamePrefix("MultiplyDone")), comment="") |
| 14038 |
module.add(SCmpGeI32(src0, 0)) |
| 14039 |
module.add(SCBranchSCC0(labelName=negativeLabel.getLabelName(), comment="")) |
| 14040 |
tmpVgprRes = ContinuousRegister(vtmp0, 2) |
| 14041 |
module.add(SMulInt64to32(dst0, dst1, src0, src1, tmpVgprRes, bool(self.states.asmCaps["HasSMulHi"]), False, comment)) |
| 14042 |
module.add(SBranch(labelName=multiplydoneLabel.getLabelName(), comment="")) |
| 14043 |
module.add(negativeLabel) |
| 14044 |
module.add(SAbsI32(src0, src0, comment="")) |
| 14045 |
module.add(SMulInt64to32(dst0, dst1, src0, src1, tmpVgprRes, bool(self.states.asmCaps["HasSMulHi"]), False, comment)) |
| 14046 |
module.add(SXorB32(dst0, dst0, hex(0xFFFFFFFF), comment="")) |
| 14047 |
module.add(SXorB32(dst1, dst1, hex(0xFFFFFFFF), comment="")) |
| 14048 |
module.add(SAddU32(dst0, dst0, hex(0x1), comment="")) |
| 14049 |
module.add(SAddCU32(dst1, dst1, 0, comment="")) |
| 14050 |
module.add(multiplydoneLabel) |
| 14051 |
self.vgprPool.checkIn(vtmp0) |
| 14052 |
return module |
| 14053 |
|
| 14054 |
def getCmpAssert(self, function, val0, val1, cookie=-1): |
| 14055 |
scratchVgpr = self.vgprPool.checkOut(2) |
| 14056 |
function(val0, val1, scratchVgpr, cookie) |
| 14057 |
self.vgprPool.checkIn(scratchVgpr) |
| 14058 |
|
| 14059 |
def getMultipleB32Assert(self, sval, multiple2, cookie=-1): |
| 14060 |
scratchVgpr = self.vgprPool.checkOut(2) |
| 14061 |
self.asmAssert.multiple_b32(sval, multiple2, scratchVgpr, cookie) |
| 14062 |
self.vgprPool.checkIn(scratchVgpr) |
| 14063 |
|
| 14064 |
def getVectorDiffAssert(self, v0, v1, expectedScalarDiff, cookie=-1): |
| 14065 |
cmpvtmp = self.vgprPool.checkOut(1) |
| 14066 |
vtmp = self.vgprPool.checkOut(2) |
| 14067 |
self.asmAssert.assert_vector_diff(v0, v1, expectedScalarDiff, cmpvtmp, vtmp, cookie) |
| 14068 |
self.vgprPool.checkIn(vtmp) |
| 14069 |
self.vgprPool.checkIn(cmpvtmp) |
| 14070 |
|
| 14071 |
|
| 14072 |
def getSCMPKInstruction(self, instOP, s0, simm16, comment="") -> Module: |
| 14073 |
imodscmpk = Module() |
| 14074 |
Inst0 = SCmpKEQU32 |
| 14075 |
Inst1 = SCmpEQU32 |
| 14076 |
if instOP == "EQU32": |
| 14077 |
Inst0 = SCmpKEQU32 |
| 14078 |
Inst1 = SCmpEQU32 |
| 14079 |
elif instOP == "LGU32": |
| 14080 |
Inst0 = SCmpKLGU32 |
| 14081 |
Inst1 = SCmpLgU32 |
| 14082 |
elif instOP == "GEU32": |
| 14083 |
Inst0 = SCmpKGeU32 |
| 14084 |
Inst1 = SCmpGeU32 |
| 14085 |
elif instOP == "GTU32": |
| 14086 |
Inst0 = SCmpKGtU32 |
| 14087 |
Inst1 = SCmpGtU32 |
| 14088 |
else: |
| 14089 |
assert 0,"getSCMPKInstruction failed" |
| 14090 |
if self.states.asmCaps["HasSCMPK"]: |
| 14091 |
imodscmpk.add(Inst0(sgpr(s0), simm16, comment=comment)) |
| 14092 |
else: |
| 14093 |
tmpScmp = self.sgprPool.checkOut(1, preventOverflow=False) |
| 14094 |
imodscmpk.add(SMovB32(sgpr(tmpScmp), simm16)) |
| 14095 |
imodscmpk.add(Inst1(src0=sgpr(s0), src1=sgpr(tmpScmp), comment=comment)) |
| 14096 |
self.sgprPool.checkIn(tmpScmp) |
| 14097 |
return imodscmpk |
| 14098 |
|
| 14099 |
def _getEccOffset(totalWidth, bpr, bpe, glvw, idx, numVgprG2L): |
| 14100 |
if totalWidth < 1: |
| 14101 |
modVal = int(bpr / (bpe * glvw)) |
| 14102 |
left = idx % modVal |
| 14103 |
return numVgprG2L * left |
| 14104 |
else: |
| 14105 |
return 0 |